-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
109 lines (86 loc) · 3.58 KB
/
server.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
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
import socket
import threading
import time,sys
from FileCommands import FileManipulator
from SerialPort import SerialPort
from pinging.server import ThreadedPingServer,PING_PORT
class ThreadedServer(threading.Thread):
"""The thread of the main server.It listens for new connections from clients
and then it establishes them.
Attributes:
host (str): The IP of the host.
port (int): The TCP port number.
"""
def __init__(self, host, port):
threading.Thread.__init__(self)
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#creates socket
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))#binds it to the appropritae address and port
self.shouldRun=True
self.IP=socket.gethostbyname(socket.gethostname())
def run(self):
self.listen()
def listen(self):
"""In every loop it:
-waits for a new collection from a client
-creates a new thread for every client
-adds it to the CLIENTS list
"""
print('Server is listening in',self.IP,':',self.port )
self.sock.listen(5)
while self.shouldRun:
client, address = self.sock.accept()#waits for a new collection from a client
client.settimeout(60)
if (len(CLIENTS)==0):#if this is the first client reset all buffers which my cause lag
print(serialPort.ser.inWaiting())
#input('Press to reset buffers')
serialPort.resetBuffers()
print(serialPort.ser.inWaiting())
CLIENTS.append( ClientThread(client,address) )#creates a new thread for every client
# and adds it to the CLIENTS list
CLIENTS[-1].daemon=True #stops when main programm ends
CLIENTS[-1].start()
def kill(self):
self.shouldRun=False
class ClientThread(threading.Thread):
"""Every client is represented by this thread"""
def __init__(self,clientsocket,clientAddress):
threading.Thread.__init__(self)
self.clientAddress=clientAddress
self.csocket = clientsocket
print ("New Client added:",clientAddress[0]+":"+str(clientAddress[1]))
def run(self):
self.csocket.send(data)
def sendData(self,data):
"""Sends data to this Client
Parameters:
data (Bytes): The data to be sent.
"""
self.csocket.send(data)
data=bytes("Hi, This is from Server...",'utf-8')
if __name__ == "__main__":
CLIENTS=[]
PORT_NUMBER = 1234
BAUD_RATE=115200
print('-Starting Ping Server')
Pingserver=ThreadedPingServer("0.0.0.0",PING_PORT)
Pingserver.daemon=True #stops when main programm ends
Pingserver.start()
print('-Starting Main Server')
server=ThreadedServer('0.0.0.0',PORT_NUMBER)
server.daemon=True #stops when main programm ends
server.start()
File=FileManipulator('TelemetryData.tlmdt')
File.write(data)
desiredModule='Arduino'
try:
COMport=SerialPort.findPortOf(desiredModule)#input('Enter the COM port which is going to be shared(COMx):')
print(desiredModule,'found at:'+COMport)
serialPort=SerialPort(COMport,BAUD_RATE,File,CLIENTS)
serialPort.listen()
except Exception as e:
print(e)
print('Finished!!!')
input('Press enter to terminate...')