Skip to content

Commit cccfd1a

Browse files
committed
bridge.py: added sockets send_to_all command
1 parent a35576b commit cccfd1a

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

linux/bridge/sockets.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@
55

66

77
class SocketClient:
8-
def __init__(self, sock):
9-
self.sock = sock
8+
def __init__(self):
109
self.txbuff = ''
1110
self.rxbuff = ''
12-
self.connected = True
1311
self.connecting = False
14-
15-
def __init__(self, address, port):
16-
self.txbuff = ''
17-
self.rxbuff = ''
1812
self.connected = False
19-
self.connecting = True
2013

14+
def set_sock(self, sock):
15+
self.sock = sock
16+
self.connected = True
17+
18+
def connect(self, address, port):
2119
self.sock = socket.socket(AF_INET, SOCK_STREAM)
2220
self.sock.setblocking(0)
2321
try:
2422
self.sock.connect((address, port))
2523
except socket.error, e:
2624
pass
25+
self.connecting = True
2726

2827
def run(self):
2928
if self.connecting:
@@ -103,7 +102,8 @@ def run(self):
103102

104103
def connect(self, address, port):
105104
# Determine the next id to assign to socket
106-
client = SocketClient(address, port)
105+
client = SocketClient()
106+
client.connect(address, port)
107107
while self.next_id in self.clients:
108108
self.next_id = (self.next_id + 1) % 256
109109
self.clients[self.next_id] = client
@@ -135,7 +135,8 @@ def accept(self):
135135
# Accept new connections
136136
(client_sock, address) = self.server.accept()
137137
# IP filtering could be here
138-
client = SocketClient(client_sock)
138+
client = SocketClient()
139+
client.set_sock(client_sock)
139140

140141
# Determine the next id to assign to socket
141142
while self.next_id in self.clients:
@@ -154,6 +155,11 @@ def send(self, id, data):
154155
self.clients[id].send(data)
155156
return True
156157

158+
def send_to_all(self, data):
159+
for id in self.clients:
160+
self.send(id, data)
161+
return True
162+
157163
def is_connected(self, id):
158164
if not id in self.clients:
159165
return None
@@ -204,6 +210,11 @@ def run(self, data):
204210
server.send(id, data[1:])
205211
return ''
206212

213+
class WRITE_TO_ALL_Command:
214+
def run(self, data):
215+
server.send_to_all(data)
216+
return ''
217+
207218
class READ_Command:
208219
def run(self, data):
209220
id = ord(data[0])
@@ -245,6 +256,7 @@ def init(command_processor):
245256
command_processor.register('j', CLOSE_Command())
246257
command_processor.register('c', CONNECTING_Command())
247258
command_processor.register('C', CONNECT_Command())
259+
command_processor.register('b', WRITE_TO_ALL_Command())
248260
command_processor.register_runner(server)
249261

250262
def test():

0 commit comments

Comments
 (0)