Changeset 654

Show
Ignore:
Timestamp:
05/09/08 15:09:39 (7 months ago)
Author:
sylvain
Message:

added a simple chat sample

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • oss/headstock/headstock/api/contact.py

    r652 r654  
    9494    @staticmethod 
    9595    def from_element(e): 
    96         print e.xml() 
    9796        r = Roster(JID.parse(e.xml_parent.get_attribute_value('from')), 
    9897                   JID.parse(e.xml_parent.get_attribute_value('to'))) 
  • oss/headstock/headstock/api/discovery.py

    r652 r654  
    1313from bridge.common import XMPP_DISCO_INFO_NS, XMPP_DISCO_ITEMS_NS, \ 
    1414     XMPP_OOB_NS, XMPP_SI_NS, XMPP_SI_FILE_TRANSFER_NS, XMPP_BYTESTREAMS_NS,\ 
    15      XMPP_DATA_FORM_NS, XMPP_CLIENT_NS, XMPP_STREAM_NS 
     15     XMPP_DATA_FORM_NS, XMPP_CLIENT_NS, XMPP_STREAM_NS, XMPP_PUBSUB_NS 
    1616 
    17 __all__ = ['FeaturesDiscovery', 'ItemsDiscovery'] 
     17__all__ = ['FeaturesDiscovery', 'ItemsDiscovery', 
     18           'SubscriptionsDiscovery'] 
    1819 
    1920class Identity(object): 
     
    4344        return '<Item %s at %s>' % (str(self.jid), hex(id(self))) 
    4445 
     46class Subscription(object): 
     47    def __init__(self, node, jid, state): 
     48        self.node = node 
     49        self.jid = jid 
     50        self.state = state 
     51 
     52    def __repr__(self): 
     53        return '<Subscription %s [%s:%s] at %s>' % (str(self.jid), self.node,  
     54                                                    self.state, hex(id(self))) 
     55 
    4556class FeaturesDiscovery(Entity): 
    4657    def __init__(self, from_jid, to_jid, node_name=None, type=u'get', stanza_id=None): 
     
    5162        self.features = [] 
    5263        self.items = [] 
     64 
     65    def has_feature(self, feature): 
     66        for f in self.features: 
     67            if f.var == feature: 
     68                return True 
     69 
     70        return False 
    5371     
    5472    @staticmethod 
     
    7896                    if i.xml_ns in [XMPP_DISCO_INFO_NS, XMPP_DISCO_ITEMS_NS]: 
    7997                        if i.xml_name == 'identity': 
    80                             ident = Identity(i.get_attribute('name'), 
    81                                              i.get_attribute('category'), 
    82                                              i.get_attribute('type')) 
     98                            ident = Identity(i.get_attribute_value('name'), 
     99                                             i.get_attribute_value('category'), 
     100                                             i.get_attribute_value('type')) 
    83101                            disco.identities.append(ident) 
    84102                        elif i.xml_name == 'feature': 
    85                             feat = Feature(i.get_attribute('var')) 
     103                            feat = Feature(i.get_attribute_value('var')) 
    86104                            disco.features.append(feat) 
    87105                        elif i.xml_name == 'item': 
    88                             jid = JID.parse(unicode(i.get_attribute('jid'))) 
    89                             item = Item(jid, i.get_attribute('action'), 
    90                                         i.get_attribute('name'), 
    91                                         i.get_attribute('node')) 
     106                            jid = JID.parse(unicode(i.get_attribute_value('jid'))) 
     107                            item = Item(jid, i.get_attribute_value('action'), 
     108                                        i.get_attribute_value('name'), 
     109                                        i.get_attribute_value('node')) 
    92110                            disco.items.append(item) 
    93111                    elif i.xml_ns == XMPP_DATA_FORM_NS: 
     
    127145                    for i in c.xml_children: 
    128146                        if i.xml_name == 'item' and i.xml_ns in [XMPP_DISCO_ITEMS_NS]: 
    129                             jid = JID.parse(unicode(i.get_attribute('jid'))) 
    130                             item = Item(jid, i.get_attribute('action'), 
    131                                         i.get_attribute('name'), 
    132                                         i.get_attribute('node')) 
     147                            jid = JID.parse(unicode(i.get_attribute_value('jid'))) 
     148                            item = Item(jid, i.get_attribute_value('action'), 
     149                                        i.get_attribute_value('name'), 
     150                                        i.get_attribute_value('node')) 
    133151                            disco.items.append(item) 
    134152            elif c.xml_ns == XMPP_CLIENT_NS and c.xml_name == 'error': 
     
    136154 
    137155        return disco 
     156 
     157class SubscriptionsDiscovery(Entity): 
     158    def __init__(self, from_jid, to_jid, type=u'get', stanza_id=None): 
     159        Entity.__init__(self, from_jid, to_jid, type, stanza_id) 
     160        self.subscriptions  = [] 
     161     
     162    @staticmethod 
     163    def to_element(e): 
     164        iq = Entity.to_element(e) 
     165        query = E(u'query', namespace=XMPP_PUBSUB_NS, parent=iq) 
     166        E('subscriptions', namespace=XMPP_PUBSUB_NS, parent=query) 
     167 
     168        return iq 
     169 
     170    @staticmethod 
     171    def from_element(e): 
     172        disco = SubscriptionsDiscovery(JID.parse(e.get_attribute_value('from')), 
     173                                       JID.parse(e.get_attribute_value('to')), 
     174                                       type=e.get_attribute_value('type'), 
     175                                       stanza_id=e.get_attribute_value('id')) 
     176 
     177        for c in e.xml_children: 
     178            if not isinstance(c, E): 
     179                continue 
     180 
     181            if c.xml_ns == XMPP_PUBSUB_NS: 
     182                if c.xml_name == 'subscriptions': 
     183                    for i in c.xml_children: 
     184                        if i.xml_name == 'subscription' and i.xml_ns == XMPP_DISCO_ITEMS_NS: 
     185                            jid = JID.parse(unicode(i.get_attribute_value('jid'))) 
     186                            item = Subscription(i.get_attribute_value('node'), 
     187                                                jid, i.get_attribute_value('subscription')) 
     188                            disco.subscriptions.append(item) 
     189            elif c.xml_ns == XMPP_CLIENT_NS and c.xml_name == 'error': 
     190                disco.error = Error.from_element(c) 
     191 
     192        return disco 
  • oss/headstock/headstock/api/im.py

    r641 r654  
    2424        return '<Body at %s>' % (hex(id(self)),) 
    2525 
     26    def __str__(self): 
     27        return str(self.plain_body) 
     28 
    2629class Subject(object): 
    2730    def __init__(self, content, lang=None): 
     
    4043 
    4144class Event(object): 
    42     def __init__(self, offline=False, composing=False, 
    43                  delivered=False, displayed=False): 
    44         self.offline = offline 
    45         self.composing = composing 
    46         self.delivered = delivered 
    47         self.displayed = displayed 
    48  
    49     def __repr__(self): 
    50         return '<Event at %s>' % (hex(id(self)),) 
     45    offline = u"offline" 
     46    composing = u"composing" 
     47    delivered = u"delivered" 
     48    displayed = u"displayed" 
    5149 
    5250class Message(Entity): 
     
    8078             
    8179            if child.xml_ns == XMPP_EVENT_NS: 
    82                 message.event = Event(child.has_child('offline', XMPP_EVENT_NS), 
    83                                       child.has_child('composing', XMPP_EVENT_NS), 
    84                                       child.has_child('delivered', XMPP_EVENT_NS), 
    85                                       child.has_child('displayed', XMPP_EVENT_NS)) 
     80                if child.has_child('offline', XMPP_EVENT_NS): 
     81                    message.event = Event.offline 
     82                if child.has_child('composing', XMPP_EVENT_NS): 
     83                    message.event = Event.composing 
     84                if child.has_child('delivered', XMPP_EVENT_NS): 
     85                    message.event = Event.delivered 
     86                if child.has_child('displayed', XMPP_EVENT_NS): 
     87                    message.event = Event.displayed 
     88 
    8689            elif child.xml_ns == XMPP_CLIENT_NS: 
    8790                if child.xml_name == 'body': 
     
    139142              namespace=XMPP_CLIENT_NS, parent=e) 
    140143 
     144        x = E(u'x', namespace=XMPP_EVENT_NS, parent=e) 
    141145        if m.event: 
    142             x = E(u'x', namespace=XMPP_EVENT_NS, parent=e) 
    143             if m.event.offline: 
    144                 E(u'offline', namespace=XMPP_EVENT_NS, parent=x) 
    145             elif m.event.composing: 
    146                 E(u'composing', namespace=XMPP_EVENT_NS, parent=x) 
    147             elif m.event.delivered: 
    148                 E(u'delivered', namespace=XMPP_EVENT_NS, parent=x) 
    149             elif m.event.displayed: 
    150                 E(u'displayed', namespace=XMPP_EVENT_NS, parent=x) 
     146            E(m.event, namespace=XMPP_EVENT_NS, parent=x) 
    151147 
    152148        for f in m.foreign: 
  • oss/headstock/headstock/api/pubsub.py

    r652 r654  
    1111from bridge import Attribute as A 
    1212from bridge.common import XMPP_CLIENT_NS, XMPP_STREAM_NS, \ 
    13     XMPP_PUBSUB_NS, XMPP_PUBSUB_OWNER_NS, XMPP_PUBSUB_NODE_CONFIG_NS 
     13    XMPP_PUBSUB_NS, XMPP_PUBSUB_OWNER_NS, XMPP_PUBSUB_NODE_CONFIG_NS,\ 
     14    XMPP_PUBSUB_EVENT_NS 
    1415 
    1516class Configure(object): 
     
    3031 
    3132class Item(object): 
    32     def __init__(self, id=None, payload=None): 
     33    def __init__(self, id=None, payload=None, eventType=None): 
    3334        self.id = id 
    3435        self.payload = payload 
     36        self.event = eventType 
    3537 
    3638    def __repr__(self): 
     
    228230         
    229231     
    230 class Items(object): 
    231     def __init__(self, max_items=None, node=None, subid=None): 
    232         self.max_items = max_items 
    233         self.node = node 
    234         self.subid = subid 
     232class Message(Entity): 
     233    def __init__(self, from_jid, to_jid): 
     234        Entity.__init__(self, from_jid, to_jid)  
     235        self.node_name = None 
    235236        self.items = [] 
    236          
    237     def __repr__(self): 
    238         return '<Items "%s" on (%s) at %s>' % (self.subid or '', self.node, hex(id(self)),) 
    239      
     237 
     238    @staticmethod 
     239    def from_element(e): 
     240        msg = Message(JID.parse(e.get_attribute_value('from')), 
     241                      JID.parse(e.get_attribute_value('to'))) 
     242 
     243        for c in e.xml_children: 
     244            if c.xml_ns == XMPP_PUBSUB_EVENT_NS: # x or event 
     245                for i in c.xml_children: 
     246                    if i.xml_ns == XMPP_PUBSUB_EVENT_NS: # items 
     247                        msg.node_name = i.get_attribute_value('node') 
     248                        for t in i.xml_children: # item 
     249                            payload = None 
     250                            if t.xml_children: 
     251                                payload = t.xml_children[0] 
     252                            msg.items.append(Item(id=t.get_attribute_value('id'), 
     253                                                  payload=payload, eventType=t.xml_name)) 
     254 
     255        return msg 
  • oss/headstock/headstock/protocol/core/stream.py

    r652 r654  
    115115    Inboxes = {"inbox"   : "bridge.Element instance", 
    116116               "control" : "Shutdown the client stream", 
    117                "forward": "bridge.Element instance to be sent out to the client",} 
     117               "forward": "bridge.Element instance to be sent out to the client", 
     118               "proceedtls": "", 
     119               "tlssuccess": "", 
     120               "tlsfailure": ""} 
    118121     
    119122    Outboxes = {"outbox" : "Any string data to be passed to the client", 
     
    122125                "log"    : "String to be logged", 
    123126                "error"  : "bridge.Element instance", 
     127                "starttls": "", 
     128                "jid"    : "", 
    124129                "bound"  : "indicates the client has been successfully bound to an XMPP server", 
    125130                "terminated": "Indicates the stream has been terminated by the peer", 
     
    129134                "%s.message" % XMPP_CLIENT_NS: "Handles 'message' element in the %s namespace" %XMPP_CLIENT_NS} 
    130135    
    131     def __init__(self, jid=None, password_lookup=None): 
     136    def __init__(self, jid=None, password_lookup=None, use_tls=False): 
    132137        """ 
    133138        A client stream is the interface between a remote XMPP service 
     
    143148        self.jid = jid 
    144149        self.password_lookup = password_lookup 
     150        self.use_tls = use_tls 
    145151 
    146152        self.status = DISCONNECTED 
     
    177183 
    178184    def _send_stream_header(self, omit_decl=False): 
    179         attributes = {u'to': self.jid.domain, u'version': u'1.0', 
    180                       u'id': generate_unique()} 
     185        attributes = {u'to': self.jid.domain, u'version': u'1.0'} 
    181186        stream = E(u'stream', attributes=attributes, 
    182187                   prefix=XMPP_STREAM_PREFIX, namespace=XMPP_STREAM_NS) 
     
    191196        self.status = CONNECTED 
    192197        mechanisms = e.get_child('mechanisms', ns=XMPP_SASL_NS) 
    193         if mechanisms: 
     198        support_tls = e.get_child('starttls', ns=XMPP_TLS_NS) 
     199        if support_tls and self.use_tls: 
     200            tls = E(u'starttls', namespace=XMPP_TLS_NS) 
     201            self.propagate(element=tls) 
     202        elif mechanisms: 
    194203            self._handle_mechanisms(mechanisms) 
    195204             
     205    def _handle_tls(self): 
     206        self._reset_stream_header(omit_decl=True) 
     207 
     208    def _proceed_tls(self, e): 
     209        self.log(e) 
     210        self.send('', 'starttls') 
     211 
    196212    def _handle_challenge(self, e): 
    197213        self.log(e) 
     
    270286    def _handle_jid(self, e): 
    271287        self.log(e) 
    272         #self.jid = JID.parse(e.xml_text) 
     288        self.jid = JID.parse(e.xml_text) 
    273289         
    274290        self.status = ACTIVE 
     291 
     292        self.send(self.jid, 'jid') 
    275293 
    276294        # Sends the initial presence information to the server 
    277295        self.propagate(element=Stanza(u'presence').to_element()) 
    278  
     296         
    279297        # Asks immediatly for the client's roster list 
    280298        iq = Iq.create_get_iq(from_jid=unicode(self.jid), stanza_id=generate_unique()) 
     
    296314                    self.send(producerFinished(), "signal") 
    297315                    break 
     316 
     317            if self.dataReady('tlssuccess'): 
     318                self.recv('tlssuccess') 
     319                yield 1 
     320                self._handle_tls() 
     321                yield 1 
    298322 
    299323            if self.dataReady("forward"): 
     
    316340                    if (e.xml_ns == XMPP_STREAM_NS) and (e.xml_name == 'features'): 
    317341                        self._handle_features(e) 
     342                    elif (e.xml_ns == XMPP_TLS_NS) and (e.xml_name == 'proceed'): 
     343                        self._proceed_tls(e) 
     344                        yield 1 
    318345                    elif (e.xml_ns == XMPP_SASL_NS) and (e.xml_name == 'challenge'): 
    319346                        self._handle_challenge(e) 
     
    346373                        # unhandled element. 
    347374                        self.send(e, "unhandled") 
     375                e = None 
    348376                         
    349377            if not self.anyReady(): 
  • oss/headstock/headstock/protocol/extension/discovery.py

    r652 r654  
    22# -*- coding: utf-8 -*- 
    33 
    4 from headstock.api.discovery import FeaturesDiscovery, ItemsDiscovery 
     4from headstock.api.discovery import FeaturesDiscovery, ItemsDiscovery,\ 
     5    SubscriptionsDiscovery 
    56from bridge import Element as E 
    67from bridge import Attribute as A 
     
    1112 
    1213 
    13 __all__ = ['FeaturesDiscoveryDispatcher', 'ItemsDiscoveryDispatcher'] 
     14__all__ = ['FeaturesDiscoveryDispatcher', 'ItemsDiscoveryDispatcher', 
     15           'SubscriptionsDiscoveryDispatcher', 'DiscoveryDispatcher'] 
    1416 
    1517class FeaturesDiscoveryDispatcher(component): 
     
    2527                "unknown"      : "Unknown element that could not be dispatched properly", 
    2628                "xmpp.get"     : "Activity requests", 
     29                "xmpp.set"     : "Activity requests", 
    2730                "xmpp.result"  : "Activity responses", 
    2831                "xmpp.error"   : "Activity response error", 
     
    7881                "unknown"      : "Unknown element that could not be dispatched properly", 
    7982                "xmpp.get"     : "Activity requests", 
     83                "xmpp.set"     : "Activity requests", 
    8084                "xmpp.result"  : "Activity responses", 
    8185                "xmpp.error"   : "Activity response error", 
     
    118122   
    119123            yield 1 
     124 
     125class SubscriptionsDiscoveryDispatcher(component): 
     126     
     127    Inboxes = {"inbox"              : "bridge.Element instance", 
     128               "control"            : "Shutdown the client stream", 
     129               "forward"            : "headstock.api.contact.Message instance to be sent back to the client. Transforms the instance to a bridge.Element instance and puts it into the 'outbox'", 
     130               } 
     131     
     132    Outboxes = {"outbox"       : "bridge.Element instance", 
     133                "signal"       : "Shutdown signal", 
     134                "log"          : "log", 
     135                "unknown"      : "Unknown element that could not be dispatched properly", 
     136                "xmpp.get"     : "Activity requests", 
     137                "xmpp.set"     : "Activity requests", 
     138                "xmpp.result"  : "Activity responses", 
     139                "xmpp.error"   : "Activity response error", 
     140                } 
     141     
     142    def __init__(self): 
     143       super(SubscriptionsDiscoveryDispatcher, self).__init__()  
     144 
     145    def main(self): 
     146        while 1: 
     147            if self.dataReady("control"): 
     148                mes = self.recv("control") 
     149                 
     150                if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): 
     151                    self.send(producerFinished(), "signal") 
     152                    break 
     153 
     154            if self.dataReady("forward"): 
     155                m = self.recv("forward") 
     156                self.send(SubscriptionsDiscovery.to_element(m), "outbox") 
     157 
     158            if self.dataReady("inbox"): 
     159                handled = False 
     160                e = self.recv("inbox") 
     161                e = e.xml_parent.xml_parent 
     162                print e.xml() 
     163                self.send(('INCOMING', e), "log") 
     164                 
     165                msg_type = e.get_attribute_value(u'type') or u'get' 
     166                key = 'xmpp.%s' % unicode(msg_type) 
     167 
     168                if key in self.outboxes: 
     169                    self.send(SubscriptionsDiscovery.from_element(e), key) 
     170                    handled = True 
     171 
     172                if not handled: 
     173                    self.send(e, "unknown") 
     174 
     175            if not self.anyReady(): 
     176                self.pause() 
     177   
     178            yield 1 
     179 
     180class DiscoveryDispatcher(component): 
     181    Inboxes = {"inbox"              : "bridge.Element instance", 
     182               "control"            : "Shutdown the client stream", 
     183               "features.inbox": "", 
     184               "subscription.inbox": "", 
     185               "items.inbox": "", 
     186               "features.forward": "", 
     187               "subscription.forward": "", 
     188               "items.forward": "", 
     189               "in.features.get"     : "Activity requests", 
     190               "in.features.set"     : "Activity requests", 
     191               "in.features.result"  : "Activity responses", 
     192               "in.features.error"   : "Activity response error", 
     193               "in.items.get"     : "Activity requests", 
     194               "in.items.set"     : "Activity requests", 
     195               "in.items.result"  : "Activity responses", 
     196               "in.items.error"   : "Activity response error", 
     197               "in.subscription.get"     : "Activity requests", 
     198               "in.subscription.set"     : "Activity requests", 
     199               "in.subscription.result"  : "Activity responses", 
     200               "in.subscription.error"   : "Activity response error",} 
     201     
     202    Outboxes = {"outbox"       : "bridge.Element instance", 
     203                "signal"       : "Shutdown signal", 
     204                "log"          : "log", 
     205                "unknown"      : "Unknown element that could not be dispatched properly", 
     206                "features.outbox": "", 
     207                "subscription.outbox": "", 
     208                "items.outbox": "", 
     209                "out.features.get"     : "Activity requests", 
     210                "out.features.set"     : "Activity requests", 
     211                "out.features.result"  : "Activity responses", 
     212                "out.features.error"   : "Activity response error", 
     213                "out.items.get"     : "Activity requests", 
     214                "out.items.set"     : "Activity requests", 
     215                "out.items.result"  : "Activity responses", 
     216                "out.items.error"   : "Activity response error", 
     217                "out.subscription.get"     : "Activity requests", 
     218                "out.subscription.set"     : "Activity requests", 
     219                "out.subscription.result"  : "Activity responses", 
     220                "out.subscription.error"   : "Activity response error", 
     221                } 
     222     
     223    def __init__(self): 
     224        super(DiscoveryDispatcher, self).__init__()  
     225 
     226    def initComponents(self):         
     227        subdisp = SubscriptionsDiscoveryDispatcher() 
     228        self.link((self, 'subscription.inbox'), (subdisp, 'inbox'), passthrough=1) 
     229        self.link((self, 'subscription.forward'), (subdisp, 'forward'), passthrough=1) 
     230        self.link((self, 'in.subscription.get'), (subdisp, 'forward'), passthrough=1) 
     231        self.link((self, 'in.subscription.set'), (subdisp, 'forward'), passthrough=1) 
     232        self.link((self, 'in.subscription.result'), (subdisp, 'forward'), passthrough=1) 
     233        self.link((self, 'in.subscription.error'), (subdisp, 'forward'), passthrough=1) 
     234        self.link((subdisp, 'outbox'), (self, 'subscription.outbox'), passthrough=2) 
     235        self.link((subdisp, 'xmpp.get'), (self, 'out.subscription.get'), passthrough=2) 
     236        self.link((subdisp, 'xmpp.set'), (self, 'out.subscription.set'), passthrough=2) 
     237        self.link((subdisp, 'xmpp.result'), (self, 'out.subscription.result'), passthrough=2) 
     238        self.link((subdisp, 'xmpp.error'), (self, 'out.subscription.error'), passthrough=2) 
     239        self.link((subdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     240        self.link((subdisp, 'log'), (self, 'log'), passthrough=2) 
     241        self.addChildren(subdisp) 
     242        subdisp.activate() 
     243     
     244        featdisp = FeaturesDiscoveryDispatcher() 
     245        self.link((self, 'features.inbox'), (featdisp, 'inbox'), passthrough=1) 
     246        self.link((self, 'features.forward'), (featdisp, 'forward'), passthrough=1) 
     247        self.link((self, 'in.features.get'), (featdisp, 'forward'), passthrough=1) 
     248        self.link((self, 'in.features.set'), (featdisp, 'forward'), passthrough=1) 
     249        self.link((self, 'in.features.result'), (featdisp, 'forward'), passthrough=1) 
     250        self.link((self, 'in.features.error'), (featdisp, 'forward'), passthrough=1) 
     251        self.link((featdisp, 'outbox'), (self, 'outbox'), passthrough=2) 
     252        self.link((featdisp, 'xmpp.get'), (self, 'out.features.get'), passthrough=2) 
     253        self.link((featdisp, 'xmpp.set'), (self, 'out.features.set'), passthrough=2) 
     254        self.link((featdisp, 'xmpp.result'), (self, 'out.features.result'), passthrough=2) 
     255        self.link((featdisp, 'xmpp.error'), (self, 'out.features.error'), passthrough=2) 
     256        self.link((featdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     257        self.link((featdisp, 'log'), (self, 'log'), passthrough=2) 
     258        self.addChildren(featdisp) 
     259        featdisp.activate() 
     260 
     261        itemsdisp = ItemsDiscoveryDispatcher() 
     262        self.link((self, 'items.inbox'), (itemsdisp, 'inbox'), passthrough=1) 
     263        self.link((self, 'items.forward'), (itemsdisp, 'forward'), passthrough=1) 
     264        self.link((self, 'in.items.get'), (itemsdisp, 'forward'), passthrough=1) 
     265        self.link((self, 'in.items.set'), (itemsdisp, 'forward'), passthrough=1) 
     266        self.link((self, 'in.items.result'), (itemsdisp, 'forward'), passthrough=1) 
     267        self.link((self, 'in.items.error'), (itemsdisp, 'forward'), passthrough=1) 
     268        self.link((itemsdisp, 'outbox'), (self, 'outbox'), passthrough=2) 
     269        self.link((itemsdisp, 'xmpp.get'), (self, 'out.items.get'), passthrough=2) 
     270        self.link((itemsdisp, 'xmpp.set'), (self, 'out.items.set'), passthrough=2) 
     271        self.link((itemsdisp, 'xmpp.result'), (self, 'out.items.result'), passthrough=2) 
     272        self.link((itemsdisp, 'xmpp.error'), (self, 'out.items.error'), passthrough=2) 
     273        self.link((itemsdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     274        self.link((itemsdisp, 'log'), (self, 'log'), passthrough=2) 
     275        self.addChildren(itemsdisp) 
     276        itemsdisp.activate() 
     277 
     278        return 1 
     279 
     280    def main(self): 
     281        yield self.initComponents() 
     282 
     283        while 1: 
     284            if self.dataReady("control"): 
     285                mes = self.recv("control") 
     286                 
     287                if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): 
     288                    self.send(producerFinished(), "signal") 
     289                    break 
     290 
     291            if not self.anyReady(): 
     292                self.pause() 
     293   
     294            yield 1 
     295 
     296        yield 1 
  • oss/headstock/headstock/protocol/extension/pubsub.py

    r652 r654  
    33from Axon.Component import component 
    44from Axon.Ipc import shutdownMicroprocess, producerFinished 
     5from Kamaelia.Chassis.Graphline import Graphline 
    56 
    67from bridge.common import XMPP_PUBSUB_NS, XMPP_DISCO_INFO_NS, \ 
    78     XMPP_DISCO_ITEMS_NS, XMPP_PUBSUB_OWNER_NS, XMPP_PUBSUB_EVENT_NS 
    8 from headstock.api.pubsub import Node 
     9from headstock.api.pubsub import Node, Message 
    910 
    1011__all__ = ['SubscriptionDispatcher', 'NodeCreationDispatcher', 
    1112           'NodeDeletionDispatcher', 'UnsubscriptionDispatcher',  
    12            'ItemPublicationDispatcher', 'ItemDeletionDispatcher'] 
     13           'ItemPublicationDispatcher', 'ItemDeletionDispatcher', 
     14           'MessageEventDispatcher', 'PubSubDispatcher'] 
    1315 
    1416class SubscriptionDispatcher(component): 
     
    359361 
    360362        yield 1 
     363 
     364class MessageEventDispatcher(component): 
     365     
     366    Inboxes = {"inbox"              : "bridge.Element instance", 
     367               "control"            : "",} 
     368     
     369    Outboxes = {"outbox"       : "bridge.Element instance", 
     370                "signal"       : "Shutdown signal", 
     371                "log"          : "log", 
     372                "unknown"      : "Unknown element that could not be dispatched properly", 
     373                "xmpp.message" : "Activity requests", 
     374                } 
     375     
     376    def __init__(self): 
     377       super(MessageEventDispatcher, self).__init__()  
     378 
     379    def main(self): 
     380        yield 1 
     381 
     382        while 1: 
     383            if self.dataReady("control"): 
     384                mes = self.recv("control") 
     385                 
     386                if isinstance(mes, shutdownMicroprocess) or isinstance(mes, producerFinished): 
     387                    self.send(producerFinished(), "signal") 
     388                    break 
     389 
     390            if self.dataReady("inbox"): 
     391                handled = False 
     392                a = self.recv("inbox") 
     393                e = a.xml_parent 
     394                self.send(('INCOMING', e), "log") 
     395                self.send(Message.from_element(e), "xmpp.message") 
     396                     
     397            if not self.anyReady(): 
     398                self.pause() 
     399   
     400            yield 1 
     401 
     402        yield 1 
     403 
     404class PubSubDispatcher(component): 
     405    Inboxes = {"inbox"               : "bridge.Element instance", 
     406               "control"             : "Shutdown the client stream", 
     407               "create.inbox"        : "", 
     408               "create.forward"      : "", 
     409               "delete.inbox"        : "", 
     410               "delete.forward"      : "", 
     411               "subscribe.inbox"     : "", 
     412               "subscribe.forward"   : "", 
     413               "unsubscribe.inbox"   : "", 
     414               "unsubscribe.forward"  : "", 
     415               "publish.inbox"       : "", 
     416               "publish.forward"     : "", 
     417               "retract.inbox"       : "", 
     418               "retract.forward"     : "", 
     419               "message.inbox"       : "", 
     420               "in.create.get"          : "Publish items requests", 
     421               "in.create.set"          : "Publish items responses", 
     422               "in.create.result"       : "Publish items responses", 
     423               "in.delete.error"        : "Publish items response error", 
     424               "in.delete.get"          : "Publish items requests", 
     425               "in.delete.set"          : "Publish items responses", 
     426               "in.delete.result"       : "Publish items responses", 
     427               "in.create.error"        : "Publish items response error", 
     428               "in.subscribe.get"       : "Publish items requests", 
     429               "in.subscribe.set"       : "Publish items responses", 
     430               "in.subscribe.result"    : "Publish items responses", 
     431               "in.subscribe.error"     : "Publish items response error", 
     432               "in.unsubscribe.get"     : "Publish items requests", 
     433               "in.unsubscribe.set"     : "Publish items responses", 
     434               "in.unsubscribe.result"  : "Publish items responses", 
     435               "in.unsubscribe.error"   : "Publish items response error", 
     436               "in.publish.get"         : "Publish items requests", 
     437               "in.publish.set"         : "Publish items responses", 
     438               "in.publish.result"      : "Publish items responses", 
     439               "in.publish.error"       : "Publish items response error", 
     440               "in.retract.get"         : "Retract item requests", 
     441               "in.retract.set"         : "Retract item responses", 
     442               "in.retract.result"      : "Retract item responses", 
     443               "in.retract.error"       : "Retract item response error"} 
     444     
     445    Outboxes = {"outbox"                  : "bridge.Element instance", 
     446                "signal"                  : "Shutdown signal", 
     447                "unknown"                 : "Unknown element that could not be dispatched properly", 
     448                "log"                     : "log", 
     449                "create.outbox"           : "", 
     450                "delete.outbox"           : "", 
     451                "subscribe.outbox"        : "", 
     452                "unsubscribe.outbox"      : "", 
     453                "publish.outbox"          : "", 
     454                "retract.outbox"          : "", 
     455                "message.outbox"          : "", 
     456                "out.create.get"          : "Publish items requests", 
     457                "out.create.set"          : "Publish items responses", 
     458                "out.create.result"       : "Publish items responses", 
     459                "out.delete.error"        : "Publish items response error", 
     460                "out.delete.get"          : "Publish items requests", 
     461                "out.delete.set"          : "Publish items responses", 
     462                "out.delete.result"       : "Publish items responses", 
     463                "out.create.error"        : "Publish items response error", 
     464                "out.subscribe.get"       : "Publish items requests", 
     465                "out.subscribe.set"       : "Publish items responses", 
     466                "out.subscribe.result"    : "Publish items responses", 
     467                "out.subscribe.error"     : "Publish items response error", 
     468                "out.unsubscribe.get"     : "Publish items requests", 
     469                "out.unsubscribe.set"     : "Publish items responses", 
     470                "out.unsubscribe.result"  : "Publish items responses", 
     471                "out.unsubscribe.error"   : "Publish items response error", 
     472                "out.publish.get"         : "Publish items requests", 
     473                "out.publish.set"         : "Publish items responses", 
     474                "out.publish.result"      : "Publish items responses", 
     475                "out.publish.error"       : "Publish items response error", 
     476                "out.retract.get"         : "Retract item requests", 
     477                "out.retract.set"         : "Retract item responses", 
     478                "out.retract.result"      : "Retract item responses", 
     479                "out.retract.error"       : "Retract item response error", 
     480                "out.message"             : "Retract item requests",} 
     481     
     482    def __init__(self): 
     483       super(PubSubDispatcher, self).__init__()  
     484 
     485    def initComponents(self): 
     486        subdisp = SubscriptionDispatcher() 
     487        self.link((self, 'subscribe.inbox'), (subdisp, 'inbox'), passthrough=1) 
     488        self.link((self, 'subscribe.forward'), (subdisp, 'forward'), passthrough=1) 
     489        self.link((self, 'in.subscribe.get'), (subdisp, 'forward'), passthrough=1) 
     490        self.link((self, 'in.subscribe.set'), (subdisp, 'forward'), passthrough=1) 
     491        self.link((self, 'in.subscribe.result'), (subdisp, 'forward'), passthrough=1) 
     492        self.link((self, 'in.subscribe.error'), (subdisp, 'forward'), passthrough=1) 
     493        self.link((subdisp, 'outbox'), (self, 'subscribe.outbox'), passthrough=2) 
     494        self.link((subdisp, 'xmpp.get'), (self, 'out.subscribe.get'), passthrough=2) 
     495        self.link((subdisp, 'xmpp.set'), (self, 'out.subscribe.set'), passthrough=2) 
     496        self.link((subdisp, 'xmpp.result'), (self, 'out.subscribe.result'), passthrough=2) 
     497        self.link((subdisp, 'xmpp.error'), (self, 'out.subscribe.error'), passthrough=2) 
     498        self.link((subdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     499        self.link((subdisp, 'log'), (self, 'log'), passthrough=2) 
     500        self.addChildren(subdisp) 
     501        subdisp.activate() 
     502 
     503        unsubdisp = UnsubscriptionDispatcher() 
     504        self.link((self, 'unsubscribe.inbox'), (unsubdisp, 'inbox'), passthrough=1) 
     505        self.link((self, 'unsubscribe.forward'), (unsubdisp, 'forward'), passthrough=1) 
     506        self.link((self, 'in.unsubscribe.get'), (unsubdisp, 'forward'), passthrough=1) 
     507        self.link((self, 'in.unsubscribe.set'), (unsubdisp, 'forward'), passthrough=1) 
     508        self.link((self, 'in.unsubscribe.result'), (unsubdisp, 'forward'), passthrough=1) 
     509        self.link((self, 'in.unsubscribe.error'), (unsubdisp, 'forward'), passthrough=1) 
     510        self.link((unsubdisp, 'outbox'), (self, 'unsubscribe.outbox'), passthrough=2) 
     511        self.link((unsubdisp, 'xmpp.get'), (self, 'out.unsubscribe.get'), passthrough=2) 
     512        self.link((unsubdisp, 'xmpp.set'), (self, 'out.unsubscribe.set'), passthrough=2) 
     513        self.link((unsubdisp, 'xmpp.result'), (self, 'out.unsubscribe.result'), passthrough=2) 
     514        self.link((unsubdisp, 'xmpp.error'), (self, 'out.unsubscribe.error'), passthrough=2) 
     515        self.link((unsubdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     516        self.link((unsubdisp, 'log'), (self, 'log'), passthrough=2) 
     517        self.addChildren(unsubdisp) 
     518        unsubdisp.activate() 
     519 
     520        nodecreatedisp = NodeCreationDispatcher() 
     521        self.link((self, 'create.inbox'), (nodecreatedisp, 'inbox'), passthrough=1) 
     522        self.link((self, 'create.forward'), (nodecreatedisp, 'forward'), passthrough=1) 
     523        self.link((self, 'in.create.get'), (nodecreatedisp, 'forward'), passthrough=1) 
     524        self.link((self, 'in.create.set'), (nodecreatedisp, 'forward'), passthrough=1) 
     525        self.link((self, 'in.create.result'), (nodecreatedisp, 'forward'), passthrough=1) 
     526        self.link((self, 'in.create.error'), (nodecreatedisp, 'forward'), passthrough=1) 
     527        self.link((nodecreatedisp, 'outbox'), (self, 'create.outbox'), passthrough=2) 
     528        self.link((nodecreatedisp, 'xmpp.get'), (self, 'out.create.get'), passthrough=2) 
     529        self.link((nodecreatedisp, 'xmpp.set'), (self, 'out.create.set'), passthrough=2) 
     530        self.link((nodecreatedisp, 'xmpp.result'), (self, 'out.create.result'), passthrough=2) 
     531        self.link((nodecreatedisp, 'xmpp.error'), (self, 'out.create.error'), passthrough=2) 
     532        self.link((nodecreatedisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     533        self.link((nodecreatedisp, 'log'), (self, 'log'), passthrough=2) 
     534        self.addChildren(nodecreatedisp) 
     535        nodecreatedisp.activate() 
     536 
     537        nodedeletedisp = NodeDeletionDispatcher() 
     538        self.link((self, 'delete.inbox'), (nodedeletedisp, 'inbox'), passthrough=1) 
     539        self.link((self, 'delete.forward'), (nodedeletedisp, 'forward'), passthrough=1) 
     540        self.link((self, 'in.delete.get'), (nodedeletedisp, 'forward'), passthrough=1) 
     541        self.link((self, 'in.delete.set'), (nodedeletedisp, 'forward'), passthrough=1) 
     542        self.link((self, 'in.delete.result'), (nodedeletedisp, 'forward'), passthrough=1) 
     543        self.link((self, 'in.delete.error'), (nodedeletedisp, 'forward'), passthrough=1) 
     544        self.link((nodedeletedisp, 'outbox'), (self, 'delete.outbox'), passthrough=2) 
     545        self.link((nodedeletedisp, 'xmpp.get'), (self, 'out.delete.get'), passthrough=2) 
     546        self.link((nodedeletedisp, 'xmpp.set'), (self, 'out.delete.set'), passthrough=2) 
     547        self.link((nodedeletedisp, 'xmpp.result'), (self, 'out.delete.result'), passthrough=2) 
     548        self.link((nodedeletedisp, 'xmpp.error'), (self, 'out.delete.error'), passthrough=2) 
     549        self.link((nodedeletedisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     550        self.link((nodedeletedisp, 'log'), (self, 'log'), passthrough=2) 
     551        self.addChildren(nodedeletedisp) 
     552        nodedeletedisp.activate() 
     553 
     554        itempublishdisp = ItemPublicationDispatcher() 
     555        self.link((self, 'publish.inbox'), (itempublishdisp, 'inbox'), passthrough=1) 
     556        self.link((self, 'publish.forward'), (itempublishdisp, 'forward'), passthrough=1) 
     557        self.link((self, 'in.publish.get'), (itempublishdisp, 'forward'), passthrough=1) 
     558        self.link((self, 'in.publish.set'), (itempublishdisp, 'forward'), passthrough=1) 
     559        self.link((self, 'in.publish.result'), (itempublishdisp, 'forward'), passthrough=1) 
     560        self.link((self, 'in.publish.error'), (itempublishdisp, 'forward'), passthrough=1) 
     561        self.link((itempublishdisp, 'outbox'), (self, 'publish.outbox'), passthrough=2) 
     562        self.link((itempublishdisp, 'xmpp.get'), (self, 'out.publish.get'), passthrough=2) 
     563        self.link((itempublishdisp, 'xmpp.set'), (self, 'out.publish.set'), passthrough=2) 
     564        self.link((itempublishdisp, 'xmpp.result'), (self, 'out.publish.result'), passthrough=2) 
     565        self.link((itempublishdisp, 'xmpp.error'), (self, 'out.publish.error'), passthrough=2) 
     566        self.link((itempublishdisp, 'unknown'), (self, 'unknown'), passthrough=2) 
     567        self.link((itempublishdisp, 'log'), (self, 'log'), passthrough=2) 
     568        self.addChildren(itempublishdisp) 
     569        itempublishdisp.activate() 
     570 
     571        itemretractdisp = ItemDeletionDispatcher() 
     572        self.link((self, 'retract.inbox'), (itemretractdisp, 'inbox'), passthrough=1) 
     573        self.link((self, 'retract.forward'), (itemretractdisp, 'forward'), passthrough=1) 
     574        self.link((self, 'in.retract.get'), (itemretractdisp, 'forward'), passthrough=1) 
     575        self.link((self, 'in.retract.set'), (itemretractdisp, 'forward'), passthrough=1) 
     576        self.link((self, 'in.retract.result'), (itemretractdisp, 'forward'), passthrough=1) 
     577        self.link((self, 'in.retract.error'), (itemretractdisp, 'forward'), passthrough=1) 
     578        self.link((itemretractdisp, 'outbox'), (self, 'retract.outbox'), passthrough=2) 
     579        self.link((itemretractdisp, 'xmpp.get'), (self, 'out.retract.get'), passthrough=2) 
     580        self.link((itemretractdisp, 'xmpp.set'), (self, 'out.retract.set'), passthrough=2) 
     581        self.link((itemretractdisp, 'xmpp.result'), (self, 'out.retract.result'), passthrough=2) 
     582