-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·146 lines (117 loc) · 4.45 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
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
#!/usr/bin/env python
#-----------------------------------------------------------------------
# server.py
#-----------------------------------------------------------------------
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
from threading import Thread
from time import time
import sys
import signal
import iputils
import random
import jsonutils
import ConfigParser
#-----------------------------------------------------------------------
config = ConfigParser.ConfigParser()
config.read("tetrisrc.conf")
port = int(config.get("configvalues","port"))
dialogbox = bool(config.get("configvalues", "serverdialogbox"))
if dialogbox == True:
from Tkinter import *
import tkSimpleDialog
class RoomNameDialog(tkSimpleDialog.Dialog):
def __init__(self,parent,room_name,title=None):
self.room_name = room_name
tkSimpleDialog.Dialog.__init__(self,parent,title)
def body(self, master):
Label(master, text="The room name will be: "+self.room_name).grid(row=0)
Label(master, text="Press \"OK\" to start the server.").grid(row=1)
BACKLOG = 5
chatLog = [] # This will be an array of tuples in the form (time, address, msg)
connectionThreads = [] # Keep track of all connections so we can blast messages
#-----------------------------------------------------------------------
class ServerThread(Thread):
def __init__(self, sock, address):
Thread.__init__(self)
self._sock = sock
self._address = address
# Opens a input stream that listens for new messages from client
def run(self):
print 'Spawned thread'
self._inFlo = self._sock.makefile(mode='r')
while True:
line = self._inFlo.readline()
if not line:
break
d = jsonutils.jsonToDict(line)
if "lines" in d:
blastToRandom(line, self)
elif "control" in d:
blastToAll(line)
#blastToAllButMe(line, self) # Send out new message to all connected clients
self.quitThread()
def sendMsg(self, message):
outFlo = self._sock.makefile(mode='w')
outFlo.write(message + "\n")
outFlo.close()
def quitThread(self):
self._inFlo.close()
self._sock.close()
print 'Closed socket'
connectionThreads.remove(self) # Remove self from the list of threads
print "Quitting this server thread..."
self._Thread__stop()
#-----------------------------------------------------------------------
def blastToAllButMe(newMsg, exceptThread):
for client in connectionThreads:
if client != exceptThread:
client.sendMsg(newMsg)
def blastToAll(newMsg):
for client in connectionThreads:
client.sendMsg(newMsg)
def blastToRandom(newMsg, exceptThread):
l = len(connectionThreads)
if l <= 1:
return
connectionThreads.remove(exceptThread)
randInt = random.randint(0, l - 2)
t = connectionThreads[randInt]
t.sendMsg(newMsg)
connectionThreads.append(exceptThread)
def quitThreads(signal, frame):
print "Server received ctrl-c"
for client in connectionThreads:
client.quitThread()
print "Quit a server thread..."
print "Quit all threads..."
sys.exit()
def main():
myIP = iputils.getMyIP()
room_name = iputils.ipToWords(myIP)
print "Room name:", room_name
if dialogbox == True:
root = Tk()
d = RoomNameDialog(root, room_name, "Room Name")
root.destroy()
try:
serverSock = socket(AF_INET, SOCK_STREAM)
serverSock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
print 'Opened server socket'
serverSock.bind(('', port))
print 'Bound server socket to port'
serverSock.listen(BACKLOG)
print 'Listening'
while True:
sock, address = serverSock.accept()
print 'Accepted connection, opened socket'
serverThread = ServerThread(sock, address);
serverThread.start();
serverThread.sendMsg("Welcome to the LAN party!")
connectionThreads.append(serverThread) # Add thread to list
except Exception, e:
print e
#-----------------------------------------------------------------------
signal.signal(signal.SIGINT, quitThreads)
signal.signal(signal.SIGQUIT, quitThreads)
if __name__ == '__main__':
main()