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