source: trigger/CommSocket.py@ 9493

Last change on this file since 9493 was 115, checked in by daqct3, 15 years ago
New Functions
  • Property svn:executable set to *
File size: 7.5 KB
Line 
1#!/usr/bin/python
2# First the server
3
4
5
6"""
7"""
8
9import select
10import socket
11import sys
12import signal
13from communication import send, receive
14import threading
15import GlobalVariables
16import time
17BUFSIZ = 1024
18class Sender(threading.Thread):
19 """ Sends the messages """
20 outputs = []
21 oldmessage=''
22
23 def run(self):
24 running = 1
25 while running:
26 time.sleep(0.001)
27 if(self.oldmessage!=GlobalVariables.ServerMessage):
28 self.oldmessage=GlobalVariables.ServerMessage
29
30 for o in self.outputs:
31 # o.send(msg)
32 #print "sending (glob): ",GlobalVariables.ServerMessage,"\n"
33 send(o,GlobalVariables.ServerMessage )
34 if(GlobalVariables.UserInput[1:]=='exit' or GlobalVariables.UserInput[1:]=='EXIT'):
35 running=0
36
37
38
39
40class CommServer(threading.Thread):
41 """ Simple server using select """
42 oldmessage=''
43 sender=Sender()
44 def __init__(self, port=3490, backlog=5):
45 self.clients = 0
46 # Client map
47 self.clientmap = {}
48 # Output socket list
49 self.outputs = []
50 self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
51 self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
52 self.server.bind(('',port))
53 print 'SERVER: Listening to port',port,'...'
54 self.server.listen(backlog)
55 self.sender.start()
56 # Trap keyboard interrupts
57 #signal.signal(signal.SIGINT, self.sighandler)
58 threading.Thread.__init__(self)
59 #def sighandler(self, signum, frame):
60 # if(GlobalVariables.UserInput[1:]=='exit' or GlobalVariables.UserInput[1:]=='EXIT'):
61 # running=0
62 # for o in self.outputs:
63 # o.close()
64 #
65 # self.server.close()
66 # print "TEST"
67 # Close the server
68 # print 'SERVER: Shutting down server...'
69 # Close existing client sockets
70 # for o in self.outputs:
71 # o.close()
72
73 # self.server.close()
74
75 def getname(self, client):
76
77 # Return the printable name of the
78 # client, given its socket...
79 info = self.clientmap[client]
80 host, name = info[0][0], info[1]
81 return '@'.join((name, host))
82
83 def run(self):
84
85 inputs = [self.server,sys.stdin]
86 self.outputs = []
87
88 running = 1
89
90 while running:
91 time.sleep(0.001)
92 #if(self.oldmessage!=GlobalVariables.ServerMessage):
93 # self.oldmessage=GlobalVariables.ServerMessage
94 # for o in self.outputs:
95 # # o.send(msg)
96 # #print "sending (glob): ",GlobalVariables.ServerMessage,"\n"
97 # send(o,GlobalVariables.ServerMessage )
98 if(GlobalVariables.UserInput[1:]=='exit' or GlobalVariables.UserInput[1:]=='EXIT'):
99 running=0
100
101 try:
102 inputready,outputready,exceptready = select.select(inputs, self.outputs, [])
103 except select.error, e:
104 break
105 except socket.error, e:
106 break
107
108 for s in inputready:
109
110 if s == self.server:
111 # handle the server socket
112 client, address = self.server.accept()
113 #print 'SERVER: got connection %d from %s' % (client.fileno(), address)
114 GlobalVariables.ServerMessage = 'SERVER: got connection %d from %s' %( client.fileno(), address)+"\n"
115 # Read the login name
116 cname="TESTUSER"
117 #cname = receive(client).split('NAME: ')[1]
118 #print cname
119 # Compute client name and send back
120 self.clients += 1
121 #send(client, 'FROM SERVER: CLIENT: ' + str(address[0]))
122 #send(client,"test");
123 #send(client,"test2");
124
125 inputs.append(client)
126
127 self.clientmap[client] = (address, cname)
128 # Send joining information to other clients
129 msg = '\nFROM SERVER: (Connected: New client (%d) from %s)' % (self.clients, self.getname(client))
130 GlobalVariables.ServerMessage=msg;
131 time.sleep(0.001)
132 #print "appending ", client
133 self.outputs.append(client)
134 self.sender.outputs=self.outputs
135 #elif s == sys.stdin:
136 # handle standard input
137 # junk = sys.stdin.readline()
138 # if (junk[:-1]=='exit' or junk[:-1]=='EXIT' ):
139 # running = 0
140 # Close the server
141 # print 'SERVER: Shutting down server...'
142 # # Close existing client sockets
143 # for o in self.outputs:
144 # o.close()
145
146 elif s!=sys.stdin:
147 # handle all other sockets
148 try:
149 data = s.recv(BUFSIZ)
150
151 #data = receive(s)
152 #print data
153 if data:
154 GlobalVariables.counter += 1
155 GlobalVariables.counter %= 10
156 data = str(GlobalVariables.counter)+data
157 # Send as new client's message...
158 msg = 'SERVER: You sent: >> ' + data
159 msg+="\n"
160 GlobalVariables.UserInput=data
161 #print "SERVER: recv data: "+data
162 #print "msg is: ",msg
163
164 #for o in self.outputs:
165 #msg="TSTSTS"
166 # print "sending ",msg," to ",o
167
168 # send(o, msg)
169 if(GlobalVariables.UserInput[1:]=='exit' or GlobalVariables.UserInput[1:]=='EXIT'):
170 running=0
171 # Send data to all except ourselves
172
173 else:
174 GlobalVariables.ServerMessage ='SERVER: %d hung up' % s.fileno()
175 self.clients -= 1
176 s.close()
177 inputs.remove(s)
178 self.outputs.remove(s)
179 self.sender.outputs=self.outputs
180 # Send client leaving information to others
181 msg = 'SERVER: \n(Hung up: Client from %s)' % self.getname(s)
182 #for o in self.outputs:
183 # o.send(msg)
184 # send(o, msg)
185
186 except socket.error, e:
187 # Remove
188 inputs.remove(s)
189 self.outputs.remove(s)
190
191
192 GlobalVariables.ServerMessage ='SERVER: Shutting down server...'
193
194 # Close existing client sockets
195 for o in self.outputs:
196 o.close()
197 self.server.close()
198 #def run(self):
199 # self.serve()
200
201if __name__ == "__main__":
202 commServer=CommServer(3491,5)
203 commServer.start()
Note: See TracBrowser for help on using the repository browser.