-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.py
85 lines (73 loc) · 2.75 KB
/
home.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
from twisted.internet.protocol import ClientFactory
from twisted.internet.protocol import Protocol
from twisted.internet import reactor
import sys
class InputConnection(Protocol):
def connectionMade(self):
#print "Sending data:"
self.transport.write("This is the data")
def dataReceived(self, data):
#print data
n = raw_input(">")
if n == "exit":
reactor.stop()
else:
self.transport.write(n)
class CommandConnection(Protocol):
def connectionMade(self):
print "Command Connection Received"
# wait for client to connect
reactor.listenTCP(42052, HomeConnectionFactory("client", self))
#def dataReceived(self, data):
#print "command: ",
#print data
class ClientConnection(Protocol):
def __init__(self, command_conn):
self.command_conn = command_conn
self.data_conn = None
self.data = None
def connectionMade(self):
#print "Client Connection Established"
# now set up data connection
reactor.listenTCP(41052, HomeConnectionFactory("data", self.command_conn, self))
self.command_conn.transport.write("connect")
def dataReceived(self, data):
#print "client: ",
#print data
if self.data_conn:
self.data_conn.transport.write(data)
#else:
#print "client: Storing data until data connection finishes"
def passData(self, data_connection):
self.data_conn = data_connection
class DataConnection(Protocol):
def __init__(self, command_conn, client_conn):
self.command_conn = command_conn
self.client_conn = client_conn
def connectionMade(self):
#print "Data Connection Established"
self.client_conn.passData(self)
def dataReceived(self,data):
self.client_conn.transport.write(data)
class HomeConnectionFactory(ClientFactory):
def __init__(self, connection_type, command = None, client = None):
self.connection_type = connection_type
if command:
self.command_conn = command
else:
self.command_conn = CommandConnection()
if client:
self.client_conn = client
else:
self.client_conn = ClientConnection(self.command_conn)
self.data_conn = DataConnection(self.command_conn, self.client_conn)
def buildProtocol(self, addr):
if self.connection_type == "command":
return self.command_conn
elif self.connection_type == "client":
return self.client_conn
elif self.connection_type == "data":
return self.data_conn
if __name__ == "__main__":
reactor.listenTCP( 40052, HomeConnectionFactory("command"))
reactor.run()