p2p_connector.wifi_direct_socket

This module provides an abstract class for implementing a TCP server socket, which can be used to implement an application which is communicating with an android phone using WIFI-direct.

  1"""
  2This module provides an abstract class for implementing a TCP server socket, which can be used to implement an
  3application which is communicating with an android phone using WIFI-direct.
  4"""
  5import socket
  6import threading
  7from abc import ABC, abstractmethod
  8from _socket import timeout
  9
 10
 11class WifiDirectSocket(ABC):
 12    """
 13    This abstract class can be used as parent class for implementing an application which can communicate with an
 14    Android phone using WIFI-direct.
 15    """
 16    connection: socket
 17    receive_messages_thread: threading.Thread
 18    run_receiving_thread: bool = False
 19
 20    def __init__(self, host: str, port: int):
 21        """
 22        constructor
 23        @param host: IP-address/hostname for the server socket
 24        @param port: port of the server socket
 25        """
 26        self.host: str = host
 27        self.port: int = port
 28
 29    def start_server_socket(self):
 30        """
 31        Initializes the server socket and waits for an incoming connection. After connection to a client was
 32        established the private method '__start_receive_thread' is called. This method starts a new thread which
 33        listens for incoming messages. If message was received the abstract method 'on_receive_message' will be called.
 34        """
 35        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 36        server_socket.bind((self.host, self.port))
 37        server_socket.listen()
 38        self.connection, addr = server_socket.accept()
 39        self.connection.settimeout(1.0)
 40        print(f'client connected: {addr}')
 41        self.__start_receive_thread()
 42
 43    def __start_receive_thread(self):
 44        """
 45        Starts a new thread which is waiting for incoming messages. The method is called by 'start_server_socket'.
 46        """
 47        self.run_receiving_thread = True
 48        self.__check_connection()
 49        self.on_client_connected()
 50
 51        def receive_loop():
 52            while self.run_receiving_thread:
 53                try:
 54                    data: bytes = self.connection.recv(1024)
 55                    if not data:
 56                        self.on_client_disconnected()
 57                        return
 58                    else:
 59                        print(data)
 60                        self.on_receive_message(data)
 61                except timeout:
 62                    continue
 63
 64        self.receive_messages_thread = threading.Thread(target=receive_loop)
 65        self.receive_messages_thread.start()
 66
 67    def send_message_to_client(self, message: bytes):
 68        """
 69        Sends a message to the client which is connected to the server-socket provided by this application.
 70        @param message: message which should be sent to the client (bytes)
 71        """
 72        self.__check_connection()
 73        self.connection.send(message)
 74
 75    def __check_connection(self):
 76        """
 77        Checks whether a client is already connected to the server-socket
 78        @raise ConnectionError: raises a ConnectionError if no client is connected
 79        """
 80
 81    def stop_receive_thread(self):
 82        """
 83        Stops the thread which is receiving incoming messages.
 84        """
 85        self.run_receiving_thread = False
 86        self.receive_messages_thread.join()
 87
 88    @abstractmethod
 89    def on_receive_message(self, message: bytes):
 90        """
 91        Is called when a new message was received.
 92        @param message: received message (bytes)
 93        """
 94
 95    @abstractmethod
 96    def on_client_connected(self):
 97        """
 98        Is called when a client has connected to the server socket.
 99        """
100
101    @abstractmethod
102    def on_client_disconnected(self):
103        """
104        is called if the client has been disconnected from the server socket.
105        """
class WifiDirectSocket(abc.ABC):
 12class WifiDirectSocket(ABC):
 13    """
 14    This abstract class can be used as parent class for implementing an application which can communicate with an
 15    Android phone using WIFI-direct.
 16    """
 17    connection: socket
 18    receive_messages_thread: threading.Thread
 19    run_receiving_thread: bool = False
 20
 21    def __init__(self, host: str, port: int):
 22        """
 23        constructor
 24        @param host: IP-address/hostname for the server socket
 25        @param port: port of the server socket
 26        """
 27        self.host: str = host
 28        self.port: int = port
 29
 30    def start_server_socket(self):
 31        """
 32        Initializes the server socket and waits for an incoming connection. After connection to a client was
 33        established the private method '__start_receive_thread' is called. This method starts a new thread which
 34        listens for incoming messages. If message was received the abstract method 'on_receive_message' will be called.
 35        """
 36        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 37        server_socket.bind((self.host, self.port))
 38        server_socket.listen()
 39        self.connection, addr = server_socket.accept()
 40        self.connection.settimeout(1.0)
 41        print(f'client connected: {addr}')
 42        self.__start_receive_thread()
 43
 44    def __start_receive_thread(self):
 45        """
 46        Starts a new thread which is waiting for incoming messages. The method is called by 'start_server_socket'.
 47        """
 48        self.run_receiving_thread = True
 49        self.__check_connection()
 50        self.on_client_connected()
 51
 52        def receive_loop():
 53            while self.run_receiving_thread:
 54                try:
 55                    data: bytes = self.connection.recv(1024)
 56                    if not data:
 57                        self.on_client_disconnected()
 58                        return
 59                    else:
 60                        print(data)
 61                        self.on_receive_message(data)
 62                except timeout:
 63                    continue
 64
 65        self.receive_messages_thread = threading.Thread(target=receive_loop)
 66        self.receive_messages_thread.start()
 67
 68    def send_message_to_client(self, message: bytes):
 69        """
 70        Sends a message to the client which is connected to the server-socket provided by this application.
 71        @param message: message which should be sent to the client (bytes)
 72        """
 73        self.__check_connection()
 74        self.connection.send(message)
 75
 76    def __check_connection(self):
 77        """
 78        Checks whether a client is already connected to the server-socket
 79        @raise ConnectionError: raises a ConnectionError if no client is connected
 80        """
 81
 82    def stop_receive_thread(self):
 83        """
 84        Stops the thread which is receiving incoming messages.
 85        """
 86        self.run_receiving_thread = False
 87        self.receive_messages_thread.join()
 88
 89    @abstractmethod
 90    def on_receive_message(self, message: bytes):
 91        """
 92        Is called when a new message was received.
 93        @param message: received message (bytes)
 94        """
 95
 96    @abstractmethod
 97    def on_client_connected(self):
 98        """
 99        Is called when a client has connected to the server socket.
100        """
101
102    @abstractmethod
103    def on_client_disconnected(self):
104        """
105        is called if the client has been disconnected from the server socket.
106        """

This abstract class can be used as parent class for implementing an application which can communicate with an Android phone using WIFI-direct.

WifiDirectSocket(host: str, port: int)
21    def __init__(self, host: str, port: int):
22        """
23        constructor
24        @param host: IP-address/hostname for the server socket
25        @param port: port of the server socket
26        """
27        self.host: str = host
28        self.port: int = port

constructor @param host: IP-address/hostname for the server socket @param port: port of the server socket

def start_server_socket(self):
30    def start_server_socket(self):
31        """
32        Initializes the server socket and waits for an incoming connection. After connection to a client was
33        established the private method '__start_receive_thread' is called. This method starts a new thread which
34        listens for incoming messages. If message was received the abstract method 'on_receive_message' will be called.
35        """
36        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37        server_socket.bind((self.host, self.port))
38        server_socket.listen()
39        self.connection, addr = server_socket.accept()
40        self.connection.settimeout(1.0)
41        print(f'client connected: {addr}')
42        self.__start_receive_thread()

Initializes the server socket and waits for an incoming connection. After connection to a client was established the private method '__start_receive_thread' is called. This method starts a new thread which listens for incoming messages. If message was received the abstract method 'on_receive_message' will be called.

def send_message_to_client(self, message: bytes):
68    def send_message_to_client(self, message: bytes):
69        """
70        Sends a message to the client which is connected to the server-socket provided by this application.
71        @param message: message which should be sent to the client (bytes)
72        """
73        self.__check_connection()
74        self.connection.send(message)

Sends a message to the client which is connected to the server-socket provided by this application. @param message: message which should be sent to the client (bytes)

def stop_receive_thread(self):
82    def stop_receive_thread(self):
83        """
84        Stops the thread which is receiving incoming messages.
85        """
86        self.run_receiving_thread = False
87        self.receive_messages_thread.join()

Stops the thread which is receiving incoming messages.

@abstractmethod
def on_receive_message(self, message: bytes):
89    @abstractmethod
90    def on_receive_message(self, message: bytes):
91        """
92        Is called when a new message was received.
93        @param message: received message (bytes)
94        """

Is called when a new message was received. @param message: received message (bytes)

@abstractmethod
def on_client_connected(self):
 96    @abstractmethod
 97    def on_client_connected(self):
 98        """
 99        Is called when a client has connected to the server socket.
100        """

Is called when a client has connected to the server socket.

@abstractmethod
def on_client_disconnected(self):
102    @abstractmethod
103    def on_client_disconnected(self):
104        """
105        is called if the client has been disconnected from the server socket.
106        """

is called if the client has been disconnected from the server socket.