-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
75 lines (47 loc) · 2.59 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import Callable
import tkinter as tk
from . import connection
from .events.event import Event
from .events.eventHandler import EventHandler
import socket
class Client:
def __init__(self, serverIp: str, eventPort: int, requestSendPort: int, requestReceivePort: int):
self.eventConnection = connection.EventReceiver(serverIp, eventPort)
self.requestSendConnection = connection.RequestSender(serverIp, requestSendPort)
self.requestReceiveConnection = connection.RequestReceiver(requestReceivePort)
hostName = socket.gethostname()
clientIp = socket.gethostbyname(hostName)
joinGroupEvent = Event("join group")
groupName = "main"
clientName = "client"
self.eventHandler = EventHandler()
self.eventConnection.SetCallback(lambda _, data: self.eventHandler.handleEvent(data[0], data[1]))
self.eventHandler.addEventLoop("main")
self.eventHandler.startLoop("main")
self.requestHandler = EventHandler()
self.requestReceiveConnection.SetCallback(self.requestHandler.handleEvent)
self.requestHandler.addEventLoop("main")
self.requestHandler.startLoop("main")
eventData = groupName, clientName, clientIp, requestReceivePort
self.requestSendConnection.SendMessage(joinGroupEvent, eventData)
def addRequestType(self, requestType: str):
self.requestHandler.addEvent(Event(requestType))
def setRequestHandler(self, eventType: str, requestHandler: Callable, loopName: str = "main"):
self.requestHandler.setEventHandler(loopName, Event(eventType), requestHandler)
def createRequestLoop(self, name: str, timeout: float = 1) -> None:
self.requestHandler.addEventLoop(name, timeout)
def createTkinterRequestLoop(self, name: str, app: tk.Tk, timeout: float = 1) -> None:
self.requestHandler.addTkinterEventLoop(name, app, timeout)
self.requestHandler.startLoop(name)
def addEventType(self, eventType: str):
self.eventHandler.addEvent(Event(eventType))
def setEventHandler(self, eventType: str, eventHandler: Callable, loopName: str = "main"):
self.eventHandler.setEventHandler(loopName, Event(eventType), eventHandler)
def createEventLoop(self, name: str, timeout: float = 1) -> None:
self.eventHandler.addEventLoop(name, timeout)
def SendRequest(self, requestType: str, data):
event = Event(requestType)
reply = self.requestSendConnection.SendMessage(event, data)
return reply
def Subscribe(self, topic: str):
self.eventConnection.Subscribe(topic)