-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.py
executable file
·152 lines (116 loc) · 3.45 KB
/
controller.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
import socket
import sys
import time
import signal
from Queue import Queue
from threading import Thread
from thread import *
from utils import StoppableThread
HOST = ''
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "socket created"
try:
s.bind((HOST,PORT))
except socket.error , msg:
print 'Bind failed. Error Code: ' + str(msg[0]) + 'Message ' + msg[1]
sys.exit()
print "socket bind complete"
s.listen(10)
print 'Socket now listening'
class Controller(StoppableThread):
def __init__(self):
super(Controller, self).__init__()
self.clients = []
self.TempClients = []
def addClient(self, client):
self.clients.append(client)
def regTempClient(self, client):
self.TempClients.append(client)
def sendToTempClients(self, data):
print 'sendtotempclients'
for client in self.TempClients:
print 'sending to a temp client'
client.addData(data)
print 'Sent data to client'
def stop(self):
super(Controller, self).stop()
for client in self.clients:
print "Stopping a client"
client.stop()
for client in self.clients:
print "joining a client"
client.join()
print "done stopping"
def run(self):
while self.isRunning():
self.sleep(5)
print "Controller thread running..."
myController = Controller()
myController.start()
#Function for handling connections
class clientThread(StoppableThread):
def __init__(self, conn):
super(clientThread, self).__init__()
self.conn = conn
self.EventData = Queue()
def receivedData(self, data):
reply = 'Message Received at the server!\n'
if data == "Reg: Temp":
# Register to receive temp data
myController.regTempClient(self)
else:
myController.sendToTempClients(data)
print 'sent temp to controller ' + data + ' ' + str(self.conn.getpeername()[1])
print data + ' ' + str(self.conn.getpeername()[1])
self.conn.sendall(reply)
# Put in Queue
def addData(self, data):
print 'adding data to client '+ str(self.conn.getpeername()[1])
self.EventData.put(data)
print 'adddded data '+ str(self.conn.getpeername()[1])
def checkForData(self):
print 'checking for data ' + str(self.conn.getpeername()[1])
if not self.EventData.empty():
print 'data found '+ str(self.conn.getpeername()[1])
data = self.EventData.get()
self.conn.sendall(data)
print 'data sent '+ str(self.conn.getpeername()[1])
def run(self):
self.conn.settimeout(1)
#Sending message to connected client
self.conn.send('Welcome to the server.\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while self.isRunning():
print 'looping ' + str(self.conn.getpeername())
try:
#Receiving from client
data = self.conn.recv(1024)
if data:
self.receivedData(data)
print 'done data RRR ' + str(self.conn.getpeername()[1])
except socket.timeout:
pass
print 'to check for data ' + str(self.conn.getpeername()[1])
self.checkForData()
print 'CLOSING '+ str(self.conn.getpeername()[1])
self.conn.close()
# Run Server
try:
#now keep talking with the client
while 1:
#wait to accept a connection
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
client = clientThread(conn)
myController.addClient(client)
client.start()
#start new thread
#start_new_thread(clientthread ,(conn,))
except KeyboardInterrupt:
print 'Quitting'
s.close()
myController.stop()
myController.join()
print 'done'