Mercurial > libervia-backend
comparison sat_bridge/DBus.py @ 16:0a024d5e0cd0
New account creation (in-band registration)
- new method "sendAnswer" in bridge for asyncronous result communication
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 02 Nov 2009 00:45:03 +0100 |
parents | 14d7861ca59e |
children | 74a39f40eb6d |
comparison
equal
deleted
inserted
replaced
15:218ec9984fa5 | 16:0a024d5e0cd0 |
---|---|
26 import dbus.mainloop.glib | 26 import dbus.mainloop.glib |
27 import pdb | 27 import pdb |
28 from logging import debug, info, error | 28 from logging import debug, info, error |
29 | 29 |
30 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix | 30 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix |
31 const_COMM_SUFFIX = ".communication" | |
32 const_REQ_SUFFIX = ".request" | |
31 | 33 |
32 class DbusObject(dbus.service.Object): | 34 class DbusObject(dbus.service.Object): |
33 | 35 |
34 def __init__(self, bus, path): | 36 def __init__(self, bus, path): |
35 dbus.service.Object.__init__(self, bus, path) | 37 dbus.service.Object.__init__(self, bus, path) |
39 def register(self, name, cb): | 41 def register(self, name, cb): |
40 self.cb[name]=cb | 42 self.cb[name]=cb |
41 | 43 |
42 ### signals ### | 44 ### signals ### |
43 | 45 |
44 @dbus.service.signal(const_INT_PREFIX+".communication", | 46 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
45 signature='sa{ss}as') | 47 signature='sa{ss}as') |
46 def newContact(self, contact, attributes, groups): | 48 def newContact(self, contact, attributes, groups): |
47 debug("new contact signal (%s) sended", contact) | 49 debug("new contact signal (%s) sended", contact) |
48 | 50 |
49 @dbus.service.signal(const_INT_PREFIX+".communication", | 51 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
50 signature='ssss') | 52 signature='ssss') |
51 def newMessage(self, from_jid, msg, type='chat', to=''): | 53 def newMessage(self, from_jid, msg, type='chat', to=''): |
52 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) | 54 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) |
53 | 55 |
54 @dbus.service.signal(const_INT_PREFIX+".communication", | 56 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
55 signature='ssssi') | 57 signature='ssssi') |
56 def presenceUpdate(self, jid, type, show, status, priority): | 58 def presenceUpdate(self, jid, type, show, status, priority): |
57 debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) | 59 debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) |
58 | 60 |
59 @dbus.service.signal(const_INT_PREFIX+".communication", | 61 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
60 signature='sss') | 62 signature='sss') |
61 def paramUpdate(self, name, value, namespace): | 63 def paramUpdate(self, name, value, namespace): |
62 debug("param update signal: %s=%s in namespace %s", name, value, namespace) | 64 debug("param update signal: %s=%s in namespace %s", name, value, namespace) |
63 | 65 |
64 @dbus.service.signal(const_INT_PREFIX+".communication", | 66 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
65 signature='s') | 67 signature='s') |
66 def contactDeleted(self, jid): | 68 def contactDeleted(self, jid): |
67 debug("contact deleted signal: %s", jid) | 69 debug("contact deleted signal: %s", jid) |
68 | 70 |
69 @dbus.service.signal(const_INT_PREFIX+".request", | 71 @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, |
70 signature='ssa{ss}') | 72 signature='ssa{ss}') |
71 def askConfirmation(self, type, id, data): | 73 def askConfirmation(self, type, id, data): |
72 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) | 74 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) |
73 | 75 |
74 | 76 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, |
77 signature='ssa{ss}') | |
78 def sendAnswer(self, type, id, data): | |
79 debug("sending answer: id = [%s] type = %s data = %s", id, type, data) | |
75 | 80 |
76 ### methods ### | 81 ### methods ### |
77 | 82 |
78 @dbus.service.method(const_INT_PREFIX+".communication", | 83 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
84 in_signature='sssi', out_signature='s') | |
85 def registerNewAccount(self, login, password, host, port=5222): | |
86 info ("New account registration asked") | |
87 return self.cb["registerNewAccount"](login, password, host, port) | |
88 | |
89 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, | |
79 in_signature='', out_signature='') | 90 in_signature='', out_signature='') |
80 def connect(self): | 91 def connect(self): |
81 info ("Connection asked") | 92 info ("Connection asked") |
82 return self.cb["connect"]() | 93 return self.cb["connect"]() |
83 | 94 |
84 @dbus.service.method(const_INT_PREFIX+".communication", | 95 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
85 in_signature='', out_signature='') | 96 in_signature='', out_signature='') |
86 def disconnect(self): | 97 def disconnect(self): |
87 info ("Disconnection asked") | 98 info ("Disconnection asked") |
88 return self.cb["disconnect"]() | 99 return self.cb["disconnect"]() |
89 | 100 |
90 @dbus.service.method(const_INT_PREFIX+".communication", | 101 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
91 in_signature='', out_signature='a(sa{ss}as)') | 102 in_signature='', out_signature='a(sa{ss}as)') |
92 def getContacts(self): | 103 def getContacts(self): |
93 debug("getContacts...") | 104 debug("getContacts...") |
94 return self.cb["getContacts"]() | 105 return self.cb["getContacts"]() |
95 | 106 |
96 @dbus.service.method(const_INT_PREFIX+".communication", | 107 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
97 in_signature='', out_signature='a(ssssi)') | 108 in_signature='', out_signature='a(ssssi)') |
98 def getPresenceStatus(self): | 109 def getPresenceStatus(self): |
99 debug("getPresenceStatus...") | 110 debug("getPresenceStatus...") |
100 return self.cb["getPresenceStatus"]() | 111 return self.cb["getPresenceStatus"]() |
101 | 112 |
102 @dbus.service.method(const_INT_PREFIX+".communication", | 113 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
103 in_signature='ss', out_signature='') | 114 in_signature='ss', out_signature='') |
104 def sendMessage(self, to, message): | 115 def sendMessage(self, to, message): |
105 debug("sendMessage...") | 116 debug("sendMessage...") |
106 self.cb["sendMessage"](to, message) | 117 self.cb["sendMessage"](to, message) |
107 | 118 |
108 @dbus.service.method(const_INT_PREFIX+".communication", | 119 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
109 in_signature='ssssi', out_signature='') | 120 in_signature='ssssi', out_signature='') |
110 def setPresence(self, to="", type="", show="", status="", priority=0): | 121 def setPresence(self, to="", type="", show="", status="", priority=0): |
111 self.cb["setPresence"](to, type, show, status, priority) | 122 self.cb["setPresence"](to, type, show, status, priority) |
112 | 123 |
113 @dbus.service.method(const_INT_PREFIX+".communication", | 124 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
114 in_signature='sss', out_signature='') | 125 in_signature='sss', out_signature='') |
115 def setParam(self, name, value, namespace="default"): | 126 def setParam(self, name, value, namespace="default"): |
116 self.cb["setParam"](name, str(value), namespace) | 127 self.cb["setParam"](name, str(value), namespace) |
117 | 128 |
118 @dbus.service.method(const_INT_PREFIX+".communication", | 129 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
119 in_signature='ss', out_signature='(ss)') | 130 in_signature='ss', out_signature='(ss)') |
120 def getParam(self, name, namespace="default"): | 131 def getParam(self, name, namespace="default"): |
121 return self.cb["getParam"](name, namespace) | 132 return self.cb["getParam"](name, namespace) |
122 | 133 |
123 @dbus.service.method(const_INT_PREFIX+".communication", | 134 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
124 in_signature='s', out_signature='a(sss)') | 135 in_signature='s', out_signature='a(sss)') |
125 def getParams(self, namespace): | 136 def getParams(self, namespace): |
126 return self.cb["getParams"](namespace) | 137 return self.cb["getParams"](namespace) |
127 | 138 |
128 @dbus.service.method(const_INT_PREFIX+".communication", | 139 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
129 in_signature='', out_signature='as') | 140 in_signature='', out_signature='as') |
130 def getParamsCategories(self): | 141 def getParamsCategories(self): |
131 return self.cb["getParamsCategories"]() | 142 return self.cb["getParamsCategories"]() |
132 | 143 |
133 @dbus.service.method(const_INT_PREFIX+".communication", | 144 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
134 in_signature='ssi', out_signature='a{i(ss)}') | 145 in_signature='ssi', out_signature='a{i(ss)}') |
135 def getHistory(self, from_jid, to_jid, size): | 146 def getHistory(self, from_jid, to_jid, size): |
136 debug("History asked for %s", to_jid) | 147 debug("History asked for %s", to_jid) |
137 return self.cb["getHistory"](from_jid, to_jid, size) | 148 return self.cb["getHistory"](from_jid, to_jid, size) |
138 | 149 |
139 @dbus.service.method(const_INT_PREFIX+".communication", | 150 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
140 in_signature='s', out_signature='') | 151 in_signature='s', out_signature='') |
141 def addContact(self, jid): | 152 def addContact(self, jid): |
142 debug("Subscription asked for %s", jid) | 153 debug("Subscription asked for %s", jid) |
143 return self.cb["addContact"](jid) | 154 return self.cb["addContact"](jid) |
144 | 155 |
145 @dbus.service.method(const_INT_PREFIX+".communication", | 156 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
146 in_signature='s', out_signature='') | 157 in_signature='s', out_signature='') |
147 def delContact(self, jid): | 158 def delContact(self, jid): |
148 debug("Unsubscription asked for %s", jid) | 159 debug("Unsubscription asked for %s", jid) |
149 return self.cb["delContact"](jid) | 160 return self.cb["delContact"](jid) |
150 | 161 |
151 @dbus.service.method(const_INT_PREFIX+".communication", | 162 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, |
152 in_signature='', out_signature='b') | 163 in_signature='', out_signature='b') |
153 def isConnected(self): | 164 def isConnected(self): |
154 debug("Connection status requested") | 165 debug("Connection status requested") |
155 return self.cb["isConnected"]() | 166 return self.cb["isConnected"]() |
156 | 167 |
157 @dbus.service.method(const_INT_PREFIX+".request", | 168 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, |
158 in_signature='sba{ss}', out_signature='') | 169 in_signature='sba{ss}', out_signature='') |
159 def confirmationAnswer(self, id, accepted, data): | 170 def confirmationAnswer(self, id, accepted, data): |
160 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") | 171 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") |
161 return self.cb["confirmationAnswer"](id, accepted, data) | 172 return self.cb["confirmationAnswer"](id, accepted, data) |
162 | 173 |
163 @dbus.service.method(const_INT_PREFIX+".request", | 174 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, |
164 in_signature='s', out_signature='a{ss}') | 175 in_signature='s', out_signature='a{ss}') |
165 def getProgress(self, id): | 176 def getProgress(self, id): |
166 #debug("Progress asked for %s", id) | 177 #debug("Progress asked for %s", id) |
167 return self.cb["getProgress"](id) | 178 return self.cb["getProgress"](id) |
168 | 179 |
169 def _attribute_string(self, in_sign): | 180 def __attribute_string(self, in_sign): |
170 i=0 | 181 i=0 |
171 idx=0 | 182 idx=0 |
172 attr_string="" | 183 attr_string="" |
173 while i<len(in_sign): | 184 while i<len(in_sign): |
174 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: | 185 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: |
190 | 201 |
191 | 202 |
192 def addMethod(self, name, int_suffix, in_sign, out_sign): | 203 def addMethod(self, name, int_suffix, in_sign, out_sign): |
193 """Dynamically add a method to Dbus Bridge""" | 204 """Dynamically add a method to Dbus Bridge""" |
194 #FIXME: Better way ??? | 205 #FIXME: Better way ??? |
195 attributes = self._attribute_string(in_sign) | 206 attributes = self.__attribute_string(in_sign) |
196 | 207 |
197 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') | 208 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') |
198 exec (code) | 209 exec (code) |
199 method = locals()[name] | 210 method = locals()[name] |
200 setattr(DbusObject, name, dbus.service.method( | 211 setattr(DbusObject, name, dbus.service.method( |
229 self.dbus_bridge.contactDeleted(jid) | 240 self.dbus_bridge.contactDeleted(jid) |
230 | 241 |
231 def askConfirmation(self, type, id, data): | 242 def askConfirmation(self, type, id, data): |
232 self.dbus_bridge.askConfirmation(type, id, data) | 243 self.dbus_bridge.askConfirmation(type, id, data) |
233 | 244 |
245 def sendAnswer(self, type, id, data): | |
246 self.dbus_bridge.sendAnswer(type, id, data) | |
247 | |
234 def register(self, name, callback): | 248 def register(self, name, callback): |
235 debug("registering DBus bridge method [%s]",name) | 249 debug("registering DBus bridge method [%s]",name) |
236 self.dbus_bridge.register(name, callback) | 250 self.dbus_bridge.register(name, callback) |
237 | 251 |
238 def addMethod(self, name, int_suffix, in_sign, out_sign, method): | 252 def addMethod(self, name, int_suffix, in_sign, out_sign, method): |