Skip to content

Commit

Permalink
UPD: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
desty2k committed Jun 10, 2021
1 parent 717edc2 commit 7882290
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
25 changes: 17 additions & 8 deletions examples/balanced_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import traceback

from QtPyNetwork.server import QBalancedServer
from QtPyNetwork.models import Device

IP = "127.0.0.1"
PORT = 7890
KEY = b""


class Main(QObject):
Expand All @@ -21,17 +21,26 @@ def setup(self):
self.logger = logging.getLogger(self.__class__.__name__)

self.srv = QBalancedServer()
self.srv.disconnected.connect(self.close)
self.srv.connected.connect(lambda device_id, ip, port: self.srv.write(int(device_id), {"id": device_id,
"ip": ip,
"port": port,
"data": "Hello world!"}))
self.srv.start(IP, PORT, KEY)
self.srv.connected.connect(lambda device, ip, port: device.write(b"Some important data"))
self.srv.disconnected.connect(self.on_disconnected)
self.srv.message.connect(self.on_message)
self.srv.start(IP, PORT)

@Slot(Device, bytes)
def on_message(self, device, message: bytes):
self.logger.info("Received {}: {}".format(device.get_id(), message))
if message.decode() == "Kick me plz":
device.kick()

@Slot(Device)
def on_disconnected(self, device):
self.logger.info("Disconnected: {}; Connected: {}".format(device.get_id(), device.is_connected()))
self.close()

@Slot()
def close(self):
self.srv.close()
while self.srv.isRunning():
while self.srv.is_running():
self.srv.wait()
QApplication.instance().quit()

Expand Down
14 changes: 6 additions & 8 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

IP = "127.0.0.1"
PORT = 7890
KEY = b""


class Main(QObject):
Expand All @@ -23,18 +22,17 @@ def setup(self):
self.cln.message.connect(self.client_data_received)
self.cln.failed_to_connect.connect(self.close)
self.cln.disconnected.connect(self.close)
self.cln.start(IP, PORT, KEY)
self.cln.start(IP, PORT)

@Slot(dict)
def client_data_received(self, data: dict):
if data.get("data") == "Hello world!":
self.logger.info("Received Hello world!, closing client!")
self.close()
@Slot(bytes)
def client_data_received(self, data: bytes):
self.logger.info(data)
self.cln.write(b"Kick me plz")

@Slot()
def close(self):
self.cln.close()
while self.cln.isRunning():
while self.cln.is_running():
self.cln.wait()
QApplication.instance().quit()

Expand Down
25 changes: 17 additions & 8 deletions examples/threaded_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import traceback

from QtPyNetwork.server import QThreadedServer
from QtPyNetwork.models import Device

IP = "127.0.0.1"
PORT = 7890
KEY = b""


class Main(QObject):
Expand All @@ -21,17 +21,26 @@ def setup(self):
self.logger = logging.getLogger(self.__class__.__name__)

self.srv = QThreadedServer()
self.srv.disconnected.connect(self.close)
self.srv.connected.connect(lambda device_id, ip, port: self.srv.write(device_id, {"id": device_id,
"ip": ip,
"port": port,
"data": "Hello world!"}))
self.srv.start(IP, PORT, KEY)
self.srv.connected.connect(lambda device, ip, port: device.write(b"Some important data"))
self.srv.disconnected.connect(self.on_disconnected)
self.srv.message.connect(self.on_message)
self.srv.start(IP, PORT)

@Slot(Device, bytes)
def on_message(self, device, message: bytes):
self.logger.info("Received {}: {}".format(device.get_id(), message))
if message.decode() == "Kick me plz":
device.kick()

@Slot(Device)
def on_disconnected(self, device):
self.logger.info("Disconnected: {}; Connected: {}".format(device.get_id(), device.is_connected()))
self.close()

@Slot()
def close(self):
self.srv.close()
while self.srv.isRunning():
while self.srv.is_running():
self.srv.wait()
QApplication.instance().quit()

Expand Down

0 comments on commit 7882290

Please sign in to comment.