author | Tero Marttila <terom@fixme.fi> |
Sat, 20 Feb 2010 23:06:43 +0200 | |
changeset 64 | 8574aeff9b36 |
parent 62 | e4db89a5f6bc |
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.protocols import amp |
64 | 2 |
from twisted.python import log |
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
from fixbot.module import ModuleInfo, Event |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
class CmdModuleRegister (amp.Command) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
Register module |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
""" |
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 |
arguments = [ |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
('name', amp.String()), |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
] |
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 |
class CmdModuleEvent (amp.Command) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
Broadcast event |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
arguments = [ |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
('type', amp.String()), |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
('msg', amp.String()), |
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 |
|
64 | 25 |
class CmdModuleAbort (amp.Command) : |
26 |
""" |
|
27 |
Module has failed and will now disconnect |
|
28 |
""" |
|
29 |
||
30 |
arguments = [ |
|
31 |
('msg', amp.String()) # string describing the error occuring |
|
32 |
] |
|
33 |
||
34 |
requiresAnswer = False |
|
35 |
||
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
class ServerProtocol (amp.AMP) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
Nexus-side command handler |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
# the registered ModuleInfo |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
module = None |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
|
64 | 44 |
def connectionMade (self) : |
45 |
log.msg("Module connecting from: %s" % (self.transport.getPeer())) |
|
46 |
||
47 |
||
48 |
def connectionLost (self, reason) : |
|
49 |
log.err(reason, "Module lost") |
|
50 |
||
51 |
if self.module : |
|
52 |
# drop it |
|
53 |
self.factory.nexus.unregisterModule(self.module, reason.getErrorMessage()) |
|
54 |
||
55 |
||
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
@CmdModuleRegister.responder |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
def on_ModuleRegister (self, name) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
# construct the ModuleInfo |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
mi = ModuleInfo() |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
mi.name = name |
64 | 61 |
|
62 |
log.msg("Module registered: %s" % (mi)) |
|
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
# register |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
self.factory.nexus.registerModule(mi, self) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
self.module = mi |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
# ok |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
return {} |
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 |
@CmdModuleEvent.responder |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
def on_ModuleEvent (self, type, msg) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
# as Event |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
e = Event(self.module, type, msg) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
# publish |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
self.factory.nexus.handleEvent(e) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
# ok |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
return {} |
64 | 81 |
|
82 |
@CmdModuleAbort.responder |
|
83 |
def on_ModuleAbort (self, msg) : |
|
84 |
# unhook |
|
85 |
module = self.module |
|
86 |
self.module = None |
|
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
|
64 | 88 |
# report |
89 |
self.factory.nexus.unregisterModule(self.module, msg) |
|
90 |
||
91 |
# XXX: stop accepting commands etc? |
|
62
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 |
class ClientProtocol (amp.AMP) : |
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 |
Module-side command sender/handler |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
""" |
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 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
def connectionMade (self) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
Connected to nexus, send ModuleRegister |
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 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
# register |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
self.sendModuleRegister(self.factory.name).addCallback(self._ModuleRegisterOK) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
|
64 | 107 |
def connectionLost (self, reason) : |
108 |
""" |
|
109 |
Disconnected from nexus, for whatever reason... |
|
110 |
""" |
|
111 |
||
112 |
log.err(reason, "API connection lost") |
|
113 |
||
114 |
# XXX: was this expected? Reconnect? |
|
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
def sendModuleRegister (self, name) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |
Register with given module name |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
return self.callRemote(CmdModuleRegister, name=name) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
def _ModuleRegisterOK (self, ret) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
125 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
Registered with nexus, commence operation |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
128 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
129 |
self.factory._onRegistered(self) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
132 |
def sendEvent (self, event) : |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
133 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
134 |
Broadcast event to nexus |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
""" |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
|
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
137 |
self.callRemote(CmdModuleEvent, type=event.type, msg=event.msg) |
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
138 |
|
64 | 139 |
def sendModuleAbort (self, msg) : |
140 |
""" |
|
141 |
Send CmdModuleAbort - no response is expected |
|
142 |
""" |
|
62
e4db89a5f6bc
restructure api, using AMP as the client-server protocol
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
|
64 | 144 |
self.callRemote(CmdModuleAbort, msg=msg) |
145 |
||
146 |
||
147 |
def abort (self, msg) : |
|
148 |
""" |
|
149 |
Send abort message and drop connection |
|
150 |
""" |
|
151 |
||
152 |
# disconnect. This should leave the transport to flush buffers, and then call connectionLost |
|
153 |
# should also stop us from sending any more commands |
|
154 |
self.transport.loseConnection() |
|
155 |