comparison sat/bridge/pb.py @ 2579:787b15d16347

bridge: added generated pb files to repository (needed for Cagou on Android)
author Goffi <goffi@goffi.org>
date Fri, 13 Apr 2018 18:45:43 +0200
parents
children 56f94936df1e
comparison
equal deleted inserted replaced
2578:bf1b12a8f597 2579:787b15d16347
1 #!/usr/bin/env python2
2 #-*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from sat.core.log import getLogger
22 log = getLogger(__name__)
23 from twisted.spread import jelly, pb
24 from twisted.internet import reactor
25
26
27 ## jelly hack
28 # we monkey patch jelly to handle namedtuple
29 ori_jelly = jelly._Jellier.jelly
30
31 def fixed_jelly(self, obj):
32 """this method fix handling of namedtuple"""
33 if isinstance(obj, tuple) and not obj is tuple:
34 obj = tuple(obj)
35 return ori_jelly(self, obj)
36
37 jelly._Jellier.jelly = fixed_jelly
38
39
40 class PBRoot(pb.Root):
41
42 def __init__(self):
43 self.signals_handlers = []
44
45 def remote_initBridge(self, signals_handler):
46 self.signals_handlers.append(signals_handler)
47 log.info(u"registered signal handler")
48
49 def sendSignalEb(self, failure, signal_name):
50 log.error(u"Error while sending signal {name}: {msg}".format(
51 name = signal_name,
52 msg = failure,
53 ))
54
55 def sendSignal(self, name, args, kwargs):
56 to_remove = []
57 for handler in self.signals_handlers:
58 try:
59 d = handler.callRemote(name, *args, **kwargs)
60 except pb.DeadReferenceError:
61 to_remove.append(handler)
62 else:
63 d.addErrback(self.sendSignalEb, name)
64 if to_remove:
65 for handler in to_remove:
66 log.debug(u"Removing signal handler for dead frontend")
67 self.signals_handlers.remove(handler)
68
69 ##METHODS_PART##
70
71
72 class Bridge(object):
73
74 def __init__(self):
75 log.info("Init Perspective Broker...")
76 self.root = PBRoot()
77 reactor.listenTCP(8789, pb.PBServerFactory(self.root))
78
79 def sendSignal(self, name, *args, **kwargs):
80 self.root.sendSignal(name, args, kwargs)
81
82 def remote_initBridge(self, signals_handler):
83 self.signals_handlers.append(signals_handler)
84 log.info(u"registered signal handler")
85
86 def register_method(self, name, callback):
87 log.debug("registering PB bridge method [%s]" % name)
88 setattr(self.root, "remote_"+name, callback)
89 # self.root.register_method(name, callback)
90
91 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
92 """Dynamically add a method to PB Bridge"""
93 #FIXME: doc parameter is kept only temporary, the time to remove it from calls
94 log.debug("Adding method {name} to PB bridge".format(name=name))
95 self.register_method(name, method)
96
97 def addSignal(self, name, int_suffix, signature, doc={}):
98 log.debug("Adding signal {name} to PB bridge".format(name=name))
99 setattr(self, name, lambda *args, **kwargs: self.sendSignal(name, *args, **kwargs))
100
101 def actionNew(self, action_data, id, security_limit, profile):
102 self.sendSignal("actionNew", action_data, id, security_limit, profile)
103
104 def connected(self, profile, jid_s):
105 self.sendSignal("connected", profile, jid_s)
106
107 def contactDeleted(self, entity_jid, profile):
108 self.sendSignal("contactDeleted", entity_jid, profile)
109
110 def disconnected(self, profile):
111 self.sendSignal("disconnected", profile)
112
113 def entityDataUpdated(self, jid, name, value, profile):
114 self.sendSignal("entityDataUpdated", jid, name, value, profile)
115
116 def messageNew(self, uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile):
117 self.sendSignal("messageNew", uid, timestamp, from_jid, to_jid, message, subject, mess_type, extra, profile)
118
119 def newContact(self, contact_jid, attributes, groups, profile):
120 self.sendSignal("newContact", contact_jid, attributes, groups, profile)
121
122 def paramUpdate(self, name, value, category, profile):
123 self.sendSignal("paramUpdate", name, value, category, profile)
124
125 def presenceUpdate(self, entity_jid, show, priority, statuses, profile):
126 self.sendSignal("presenceUpdate", entity_jid, show, priority, statuses, profile)
127
128 def progressError(self, id, error, profile):
129 self.sendSignal("progressError", id, error, profile)
130
131 def progressFinished(self, id, metadata, profile):
132 self.sendSignal("progressFinished", id, metadata, profile)
133
134 def progressStarted(self, id, metadata, profile):
135 self.sendSignal("progressStarted", id, metadata, profile)
136
137 def subscribe(self, sub_type, entity_jid, profile):
138 self.sendSignal("subscribe", sub_type, entity_jid, profile)