-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_topology.py
37 lines (30 loc) · 1.09 KB
/
bus_topology.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
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
# Define the Star Topology server
class StarServer(Protocol):
def __init__(self, factory):
self.factory = factory
# Called when a new connection is made
def connectionMade(self):
self.factory.clients.append(self)
print("New client connected")
# Called when a connection is lost
def connectionLost(self, reason):
self.factory.clients.remove(self)
print("Client disconnected")
# Called when data is received
def dataReceived(self, data):
print("Received data: ", data.decode('utf-8'))
for client in self.factory.clients:
if client != self:
client.transport.write(data)
# Define the Star Topology factory
class StarFactory(Factory):
def __init__(self):
self.clients = []
def buildProtocol(self, addr):
return StarServer(self)
# Start the Star Topology server
reactor.listenTCP(8000, StarFactory())
print("Star Topology server started")
reactor.run()