Module CoffeeChat.coffeechat.Server

Configuração do servidor

Expand source code
"""Configuração do servidor"""
import os
import socket
import threading
from coffeechat.Server.ServerSocket import *


class Server(threading.Thread):
        """
        Suporta gerenciamento de conexões de servidor.

        Attributes:
                connections (list): Lista de objetos ServerSocket que representam as conexões ativas.
                host (str): Endereço IP do socket de escuta.
                port (int): Número da porta do socket de escuta.
        """
        def __init__(self, host, port):
                super().__init__()
                # list of server sockets objects representing active client connections
                self.connections = []
                self.host = host
                self.port = port

        def run(self):
                """
                Cria o socket de escuta. O socket de escuta usará a opção SO_REUSEADDR para
                permitir a ligação a um endereço de socket usado anteriormente. Este é um aplicativo de pequena escala que
                suporta apenas uma conexão em espera por vez.
                Para cada nova conexão, um thread ServerSocket é iniciado para facilitar a comunicação com
                aquele cliente específico. Todos os objetos ServerSocket são armazenados no atributo connections.
                """
                # AF_INET: address family, for IP networking
                # SOCK_STREAM: socket type, for reliable flow-controlled data stream
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                sock.bind((self.host, self.port))

                sock.listen(1)
                print(f'Ouvindo em {sock.getsockname()}')

                # listen for new client connections
                while True:
                        # new connection
                        sc, sockname = sock.accept()
                        print(
                            f'Nova conexao de {sc.getpeername()} para {sc.getsockname()}'
                        )

                        # new thread
                        server_socket = ServerSocket(sc=sc, sockname=sockname, server=self)
                        # start thread
                        server_socket.start()

                        # add thread to active connections
                        self.connections.append(server_socket)
                        print(f'Pronto para receber mensagens de {sc.getpeername()}')

        def broadcast(self, message, source):
                """
                Envia uma mensagem para todos os clientes conectados,
                exceto a origem da mensagem.
                
                Args:
                        message (str): Mensagem a ser transmitida.
                        source (tuple): Endereço de socket do cliente de origem.
                """
                for connection in self.connections:
                        if connection.sockname != source:
                                connection.send(message)

        def remove_connection(self, connection):
                """
                Remove uma thread ServerSocket do atributo connections.
                
                Args:
                        connection (ServerSocket): Thread ServerSocket a ser removida.
                """
                self.connections.remove(connection)

Sub-modules

CoffeeChat.coffeechat.Server.ServerSocket

Configuração do socket do servidor

Classes

class Server (host, port)

Suporta gerenciamento de conexões de servidor.

Attributes

connections : list
Lista de objetos ServerSocket que representam as conexões ativas.
host : str
Endereço IP do socket de escuta.
port : int
Número da porta do socket de escuta.

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.

Expand source code
class Server(threading.Thread):
        """
        Suporta gerenciamento de conexões de servidor.

        Attributes:
                connections (list): Lista de objetos ServerSocket que representam as conexões ativas.
                host (str): Endereço IP do socket de escuta.
                port (int): Número da porta do socket de escuta.
        """
        def __init__(self, host, port):
                super().__init__()
                # list of server sockets objects representing active client connections
                self.connections = []
                self.host = host
                self.port = port

        def run(self):
                """
                Cria o socket de escuta. O socket de escuta usará a opção SO_REUSEADDR para
                permitir a ligação a um endereço de socket usado anteriormente. Este é um aplicativo de pequena escala que
                suporta apenas uma conexão em espera por vez.
                Para cada nova conexão, um thread ServerSocket é iniciado para facilitar a comunicação com
                aquele cliente específico. Todos os objetos ServerSocket são armazenados no atributo connections.
                """
                # AF_INET: address family, for IP networking
                # SOCK_STREAM: socket type, for reliable flow-controlled data stream
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
                sock.bind((self.host, self.port))

                sock.listen(1)
                print(f'Ouvindo em {sock.getsockname()}')

                # listen for new client connections
                while True:
                        # new connection
                        sc, sockname = sock.accept()
                        print(
                            f'Nova conexao de {sc.getpeername()} para {sc.getsockname()}'
                        )

                        # new thread
                        server_socket = ServerSocket(sc=sc, sockname=sockname, server=self)
                        # start thread
                        server_socket.start()

                        # add thread to active connections
                        self.connections.append(server_socket)
                        print(f'Pronto para receber mensagens de {sc.getpeername()}')

        def broadcast(self, message, source):
                """
                Envia uma mensagem para todos os clientes conectados,
                exceto a origem da mensagem.
                
                Args:
                        message (str): Mensagem a ser transmitida.
                        source (tuple): Endereço de socket do cliente de origem.
                """
                for connection in self.connections:
                        if connection.sockname != source:
                                connection.send(message)

        def remove_connection(self, connection):
                """
                Remove uma thread ServerSocket do atributo connections.
                
                Args:
                        connection (ServerSocket): Thread ServerSocket a ser removida.
                """
                self.connections.remove(connection)

Ancestors

  • threading.Thread

Methods

def broadcast(self, message, source)

Envia uma mensagem para todos os clientes conectados, exceto a origem da mensagem.

Args

message : str
Mensagem a ser transmitida.
source : tuple
Endereço de socket do cliente de origem.
Expand source code
def broadcast(self, message, source):
        """
        Envia uma mensagem para todos os clientes conectados,
        exceto a origem da mensagem.
        
        Args:
                message (str): Mensagem a ser transmitida.
                source (tuple): Endereço de socket do cliente de origem.
        """
        for connection in self.connections:
                if connection.sockname != source:
                        connection.send(message)
def remove_connection(self, connection)

Remove uma thread ServerSocket do atributo connections.

Args

connection : CoffeeChat.coffeechat.Server.ServerSocket
Thread ServerSocket a ser removida.
Expand source code
def remove_connection(self, connection):
        """
        Remove uma thread ServerSocket do atributo connections.
        
        Args:
                connection (ServerSocket): Thread ServerSocket a ser removida.
        """
        self.connections.remove(connection)
def run(self)

Cria o socket de escuta. O socket de escuta usará a opção SO_REUSEADDR para permitir a ligação a um endereço de socket usado anteriormente. Este é um aplicativo de pequena escala que suporta apenas uma conexão em espera por vez. Para cada nova conexão, um thread ServerSocket é iniciado para facilitar a comunicação com aquele cliente específico. Todos os objetos ServerSocket são armazenados no atributo connections.

Expand source code
def run(self):
        """
        Cria o socket de escuta. O socket de escuta usará a opção SO_REUSEADDR para
        permitir a ligação a um endereço de socket usado anteriormente. Este é um aplicativo de pequena escala que
        suporta apenas uma conexão em espera por vez.
        Para cada nova conexão, um thread ServerSocket é iniciado para facilitar a comunicação com
        aquele cliente específico. Todos os objetos ServerSocket são armazenados no atributo connections.
        """
        # AF_INET: address family, for IP networking
        # SOCK_STREAM: socket type, for reliable flow-controlled data stream
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.host, self.port))

        sock.listen(1)
        print(f'Ouvindo em {sock.getsockname()}')

        # listen for new client connections
        while True:
                # new connection
                sc, sockname = sock.accept()
                print(
                    f'Nova conexao de {sc.getpeername()} para {sc.getsockname()}'
                )

                # new thread
                server_socket = ServerSocket(sc=sc, sockname=sockname, server=self)
                # start thread
                server_socket.start()

                # add thread to active connections
                self.connections.append(server_socket)
                print(f'Pronto para receber mensagens de {sc.getpeername()}')