-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerThread.py
68 lines (55 loc) · 1.63 KB
/
ServerThread.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
"""
CSCI 334: Distributed Systems
Williams College
Spring 2014
Final Project: BLiP: Peer-to-Peer Chat
Authors:
Jeremy Boissevain
Nile Livingston
Nehemiah Paramore
This is a thread that manages incoming chat requests.
On accepting a request, a new ChatThread is created and tasked with
managing the chat.
"""
import socket
from socket import SHUT_WR
import threading
import ChatThread as ch
class ServerThread(threading.Thread):
# Dedicate a socket to listening.
def __init__(self, peer, port=50007):
super(ServerThread, self).__init__()
HOST = '' # Symbolic name meaning all available interfaces
PORT = port
# Establish socket.
try:
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except:
print "SERVER: Socket error: socket()"
return
# Bind socket.
#try:
self.s.bind((HOST, PORT))
print "Server initialized with port: " + str(PORT)
#except:
#print "SERVER: Socket error: bind()"
#return
self.peer = peer # The peer that contains this ServerThread.
# Should the server be running?
self.running = True
# Close the socket.
def exit(self):
print "SERVER QUITTING"
self.s.close()
self.running = False
# Accept connections as they come, asking the peer to create new ChatThreads.
def run(self):
print "Server is listening..."
while self.running:
self.s.listen(1)
conn, addr = self.s.accept()
# If user is logged in, set up a new ChatThread and add it.
if self.peer.isAuthenticated():
print "Incoming connection from: " + str(addr)
self.peer.addNewChatThread(conn, "PASSIVE")