author | Tero Marttila <terom@fixme.fi> |
Sat, 20 Feb 2010 22:32:18 +0200 | |
changeset 63 | a849c00b63f8 |
parent 62 | e4db89a5f6bc |
child 64 | 8574aeff9b36 |
permissions | -rw-r--r-- |
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
from twisted.internet import protocol |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
from twisted.application import service, internet |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
from twisted.python import log |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
import datetime |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
class ModuleInfo (object) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
Nexus-side handle on a Module |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
# module's name |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
name = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
def __str__ (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
return "Module %s:" % (self.name) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
def __repr__ (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
return "<module %s>" % (self.name, ) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
class Event (object) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
An Event, sent by a Module to the Nexus, to be distributed further |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
# the ModuleInfo object |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
module = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
# the event type as a string |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
type = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
# event message as a string (up to 64k, although that won't fit onto IRC..) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
msg = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
# timestamp as a datetime.datetime |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
when = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
def __init__ (self, module, type, msg) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
self.module = module |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
self.type = type |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
self.msg = msg |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
self.when = datetime.datetime.now() |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
def __str__ (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
return "[%s] %s" % (self.type, self.msg) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
def __repr__ (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
return "%s @ %s" % (self.type, self.when) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
class Module (ModuleInfo, protocol.ClientFactory) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
Module core, handles the API connection state and processes all messages |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
# our API connection to the Nexus |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
connection = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
def __init__ (self, config, protocol) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
config - configuration for connecting to nexus |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
protocol - API client protocol to use for this factory |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
self.protocol = protocol |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
self.connection = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
# XXX: legacy: self.secret = config['api-secret'] |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
def _onRegistered (self, connection) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
Connected to nexus and registered |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
log.msg("Connected and registered") |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
self.connection = connection |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
self.handleConnect() |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
def disconnect (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
Disconnect from Nexus |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
self.connection.transport.loseConnection() |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
def sendEvent (self, type, msg) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
Send event to nexus |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
self.connection.sendEvent(Event(self, type, msg)) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
def handleConnect (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
Do something once we are connected to nexus and registered |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
pass |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
def makeService (module_class, config, protocol) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
s = service.MultiService() |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
# build factory |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
factory = module_class(config, protocol) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
# the API client |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
log.msg("Connecting to API server on [%s:%d]" % (config['api-server'], config['api-port'])) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
api_client = internet.TCPClient(config['api-server'], config['api-port'], factory) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
api_client.setServiceParent(s) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
return s |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |