Skip to content

Commit

Permalink
UPD: make Device variables protected
Browse files Browse the repository at this point in the history
ADD: getters for ip, port, etc. in Device model
ADD: positional argument server in Device class
  • Loading branch information
desty2k committed Jun 16, 2021
1 parent 48620e2 commit 85d0106
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
48 changes: 27 additions & 21 deletions QtPyNetwork/models/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

class Device(QObject):
"""Represents psychical device connected to server."""
_write = Signal(bytes)
_kick = Signal()

def __init__(self, device_id: int, ip: str, port: int):
def __init__(self, server, device_id: int, ip: str, port: int):
super(Device, self).__init__(None)
try:
ipaddress.ip_address(ip)
Expand All @@ -20,33 +18,41 @@ def __init__(self, device_id: int, ip: str, port: int):
if not 1 <= port <= 65535:
raise ValueError("Port must be in range 1, 65535")

self.ip = ip
self.port = port
self.id = device_id
self.connected = True
self.__ip = ip
self.__port = port
self.__id = device_id
self.__connected = True
self.__server = server

@Slot()
def get_id(self):
return self.id
def server(self):
return self.__server

@Slot()
def id(self):
return self.__id

@Slot()
def ip(self):
return self.__ip

@Slot()
def port(self):
return self.__port

@Slot(bool)
def set_connected(self, value: bool):
self.connected = value
self.__connected = value

@Slot()
def is_connected(self):
return self.connected
def is_connected(self) -> bool:
return self.__connected

@Slot()
def kick(self):
if self.connected:
self._kick.emit()
else:
raise NotConnectedError("Client is not connected")
self.server().kick(self)

@Slot(bytes)
def write(self, message):
if self.connected:
self._write.emit(message)
else:
raise NotConnectedError("Client is not connected")
def write(self, message: bytes):
self.server().write(message)

12 changes: 5 additions & 7 deletions QtPyNetwork/server/BaseServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def __setup_server(self):
@Slot(int, str, int)
def __on_handler_successful_connection(self, device_id, ip, port):
"""When client connects to server successfully."""
device = self.__deviceModel(device_id, ip, port)
device._write.connect(lambda data: self.write(device, data))
device._kick.connect(lambda: self.kick(device))
device = self.__deviceModel(self, device_id, ip, port)
self.__devices.append(device)
self.__logger.info("Added new CLIENT-{} with address {}:{}".format(device_id, ip, port))
self.on_connected(device, ip, port)
Expand Down Expand Up @@ -137,7 +135,7 @@ def write(self, device: Device, data: bytes):
raise ServerNotRunning("Server is not running")
if not device.is_connected():
raise NotConnectedError("Client is not connected")
self.__handler.write.emit(device.get_id(), data)
self.__handler.write.emit(device.id(), data)

@Slot(bytes)
def write_all(self, data: bytes):
Expand All @@ -153,7 +151,7 @@ def kick(self, device: Device):
raise ServerNotRunning("Server is not running")
if not device.is_connected():
raise NotConnectedError("Client is not connected")
self.__handler.kick.emit(device.get_id())
self.__handler.kick.emit(device.id())

@Slot()
def close(self):
Expand All @@ -178,7 +176,7 @@ def set_device_model(self, model):
raise ValueError("Model should be subclassing Device class.")

try:
model(0, "127.0.0.1", 5000)
model(QBaseServer(), 0, "127.0.0.1", 5000)
except TypeError as e:
raise TypeError("Model is not valid class! Exception: {}".format(e))

Expand All @@ -204,7 +202,7 @@ def get_device_by_id(self, device_id: int) -> Device:
device_id (int): Device ID.
"""
for device in self.__devices:
if device.get_id() == device_id:
if device.id() == device_id:
return device
raise Exception("CLIENT-{} not found".format(device_id))

Expand Down

0 comments on commit 85d0106

Please sign in to comment.