-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonserver.py
More file actions
111 lines (94 loc) · 3.38 KB
/
pythonserver.py
File metadata and controls
111 lines (94 loc) · 3.38 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from time import sleep
from threading import Thread, Lock, Condition
import pickle
import socket
SOCK_IP = '127.0.0.1'#Gcloud instance internal ip
SOCK_PORT = 9001
class Client:
allClients = []
availableClients = {} # {'client name' : client object}
def __init__(self, client_ptr):
Client.allClients.append(self)
self.cl_ptr = client_ptr
self.name = None
self.name = self.get_name()
self.recipient_name = None
self.recipient_name = self.get_recipient_name()
print(f"received name {self.name} and recipient {self.recipient_name}")
Client.availableClients[self.name] = self
try:
self.lobby()
except ConnectionResetError:
print("CONNECTION RESET ERROR")
self.close()
except BrokenPipeError:
print("BROKEN PIPE. Closing connection")
self.close()
def lobby(self):
cl = None
while True:
try:
cl = Client.availableClients[self.recipient_name]
if cl.get_recipient_name() == self.name:
break
else:
print("recipient busy.")
sleep(1)
except KeyError:
print("waiting...")
sleep(1)
continue
if cl is not None:
# found a client who wants to connect to self
self.cl_ptr[0].send('go'.encode())
self.converse(cl)
self.close()
# Enter a loop to keep searching for recipient in available clients
def get_name(self):
if self.name is None:
# receive name
self.name = self.cl_ptr[0].recv(512).decode().rstrip()
print(f"Client connected: {self.name}")
return self.name
def get_recipient_name(self):
if self.recipient_name is None:
# receive recipient name
self.recipient_name = self.cl_ptr[0].recv(512).decode().rstrip()
print(f"Client {self.name} wants to connect to {self.recipient_name}")
return self.recipient_name
# def getRecipientSocket(self):
# search list of available clients
def converse(self, recipient_obj):
print("establishing connection...")
try:
while True:
self.send(recipient_obj, self.read())
except KeyboardInterrupt:
self.close()
except OSError:
self.close()
def send(self, cl_object, data):
cl_object.cl_ptr[0].send(data)
def read(self):
return self.cl_ptr[0].recv(1024)
def close(self):
Client.allClients.remove(self)
Client.availableClients.pop(self.get_name(), None)
self.cl_ptr[0].close()
print(f"Client {self.name} removed.")
def main():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(f"binding socket on {SOCK_IP}:{SOCK_PORT}")
serversocket.bind((SOCK_IP, SOCK_PORT))
serversocket.listen(3)
while True:
try:
client_id = (serversocket.accept(),)
thrd1 = Thread(target=client_handler, args=client_id)
thrd1.start()
except KeyboardInterrupt:
serversocket.close()
break
def client_handler(clientid):
Client(clientid)
main()