Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
107 lines (86 loc) · 2.53 KB
/
Copy pathserver.py
File metadata and controls
107 lines (86 loc) · 2.53 KB
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
#!/usr/bin/env python
# Echo server program
# Copyright (C) 2002,2003 Mike Leonhard
# http://tamale.net/
#
# version 1.1 (2003/07/07 02:08)
# * works with updated ListenClass
# version 1.0 (2003/07/06 12:32)
import select, time
from inetcomms import *
class CoordinatorClass:
def __init__( self, port=12360 ):
"""Initialize Coordinator class"""
self.liasons = []
self.pending = []
self.listen = ListenClass( port )
self.sockets = self.liasons + [self.listen]
def liasonconnectedfilter( self, l ):
"""Return the connected status of a liason"""
return l.connected
def liasonsendfilter( self, l ):
"""Return the whether the liason has unsent data"""
return l.connected and len( l.outbuffer ) > 0
def cycle( self ):
"""Check each socket"""
# clean liason list
self.liasons = filter( self.liasonconnectedfilter, self.liasons )
# each new connection from the listen socket
for (connection,address) in self.listen.newconnections:
# create a new liason for the connection
l = LiasonClass( connection )
# send greeting
l.dosend( "You have entered a strange and mysterious place.\n" )
# add the liason to the list
self.liasons.append( l )
# all new connections have been handled
self.listen.newconnections = []
# update list of listenable sockets
self.sockets = self.liasons + [self.listen]
# update list of sockets with pending outgoing data
self.pending = filter( self.liasonsendfilter, self.liasons )
def dosend( self, data ):
"""Send data through each liason"""
# each liason
for i in self.liasons:
# send the data
i.dosend( data )
def cleanup( self ):
"""Cleanup the liasons and listen socket"""
# each liason
for l in self.liasons:
# disconnect
l.disconnect()
# clear list of liasons
self.liasons = []
# listen socket
self.listen.disconnect()
def loop( self ):
"""Loop while reading liasons and listen socket"""
# begin looping
looping = 1
# loop
while looping == 1:
# get lists of sockets that are ready to be read or written
(readable, sendable, e) = select.select( self.sockets, self.pending, [] )
# check each readable socket
for l in readable:
# read
data = l.doread()
# data was read
if data:
# send it out
self.dosend( data )
# check each sendable socket
for l in sendable:
# write
l.dosend()
# update lists
self.cycle()
# pause
#time.sleep( 5 )
print 'Beginning'
coordinator = CoordinatorClass()
coordinator.loop()
coordinator.cleanup()
print 'Ending'