Changeset 655
- Timestamp:
- 05/09/08 15:52:34 (7 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
oss/headstock/headstock/example/simplechat/simplechat.py
r654 r655 31 31 32 32 class RosterHandler(component): 33 Inboxes = {"inbox" : "headstock.api.contact.Roster instance to be echoed back", 34 "control" : "Shutdown the client stream", 35 "jid" :"", 36 "ask-activity": "", 37 } 38 39 Outboxes = {"outbox" : "UNUSED", 40 "signal" : "Shutdown signal", 41 "message" : "Message to send", 42 "activity" : "", } 33 Inboxes = {"inbox" : "headstock.api.contact.Roster instance", 34 "control" : "stops the component", 35 "jid" : "headstock.api.jid.JID instance received from the server", 36 "ask-activity" : "request activity status to the server for each roster contact"} 37 38 Outboxes = {"outbox" : "UNUSED", 39 "signal" : "Shutdown signal", 40 "message" : "Message to send", 41 "activity" : "headstock.api.activity.Activity instance to send to the server"} 43 42 44 43 def __init__(self, from_jid): … … 47 46 self.roster = None 48 47 48 def initComponents(self): 49 # We subscribe to the JID backplane component 50 # that will inform us when the server has 51 # returned the per-session jid 52 sub = SubscribeTo("JID") 53 self.link((sub, 'outbox'), (self, 'jid')) 54 self.addChildren(sub) 55 sub.activate() 56 57 return 1 58 49 59 def main(self): 50 sub = SubscribeTo("JID") 51 sub.activate() 52 self.link((sub, 'outbox'), (self, 'jid')) 60 yield self.initComponents() 53 61 54 62 while 1: … … 71 79 print " ", contact.jid 72 80 73 #74 75 81 if self.dataReady('ask-activity'): 76 82 self.recv('ask-activity') … … 87 93 88 94 class DummyMessageHandler(component): 89 Inboxes = {"inbox" : "headstock.api.contact.Message instance to be echoed back", 90 "control" : "Shutdown the client stream", 91 "jid": "", 92 } 93 94 Outboxes = {"outbox" : "bridge.Element instance generated from the Message instance", 95 "signal" : "Shutdown signal", 96 } 95 Inboxes = {"inbox" : "headstock.api.contact.Message instance received from a peer"\ 96 "or the string input in the console", 97 "control" : "stops the component"} 98 99 Outboxes = {"outbox" : "headstock.api.im.Message to send to the client", 100 "signal" : "Shutdown signal"} 97 101 98 102 def __init__(self): … … 100 104 self.from_jid = None 101 105 102 def main(self):106 def initComponents(self): 103 107 sub = SubscribeTo("JID") 104 108 self.link((sub, 'outbox'), (self, 'jid')) 109 self.addChildren(sub) 105 110 sub.activate() 106 111 107 112 sub = SubscribeTo("CONSOLE") 108 113 self.link((sub, 'outbox'), (self, 'inbox')) 109 sub.activate() 114 self.addChildren(sub) 115 sub.activate() 116 117 return 1 118 119 def main(self): 120 yield self.initComponents() 110 121 111 122 while 1: … … 121 132 if self.dataReady("inbox"): 122 133 m = self.recv("inbox") 134 # in this first case, we want to send the message 135 # typed in the console. 136 # The message is of the form: 137 # contant_jid message 123 138 if isinstance(m, str) and m != '': 124 contact_jid, message = m.split(' ', 1) 139 try: 140 contact_jid, message = m.split(' ', 1) 141 except ValueError: 142 print "Messages format: contact_jid message" 143 continue 125 144 m = Message(unicode(self.from_jid), unicode(contact_jid), 126 145 type=u'chat', stanza_id=generate_unique()) 127 m.event = Event.composing 146 m.event = Event.composing # note the composing event status 128 147 m.bodies.append(Body(unicode(message))) 129 148 self.send(m, "outbox") 149 150 # Right after we sent the first message 151 # we send another one reseting the event status 130 152 m = Message(unicode(self.from_jid), unicode(contact_jid), 131 153 type=u'chat', stanza_id=generate_unique()) 132 154 self.send(m, "outbox") 155 # In this case we actually received a message 156 # from a contact, we print it. 133 157 elif isinstance(m, Message): 134 158 for body in m.bodies: … … 141 165 142 166 class DiscoHandler(component): 143 Inboxes = {"inbox" : "", 144 "control" : "", 145 "initiate" : "", 146 "jid" :"", 147 "features.result": "",} 148 149 Outboxes = {"outbox" : "", 150 "signal" : "Shutdown signal", 151 "features-disco": "", 152 "features-announce": ""} 167 Inboxes = {"inbox" : "UNUSED", 168 "control" : "stops the component", 169 "initiate" : "event informing the component the client session is active", 170 "jid" : "headstock.api.jid.JID instance received from the server", 171 "features.result": "headstock.api.discovery.FeaturesDiscovery instance from the server",} 172 173 Outboxes = {"outbox" : "UNUSED", 174 "signal" : "Shutdown signal", 175 "features-disco" : "headstock.api.discovery.FeaturesDiscovery query to the server", 176 "features-announce": "headstock.api.discovery.FeaturesDiscovery informs"\ 177 "the other components about the features instance received from the server"} 153 178 154 179 def __init__(self, from_jid, to_jid): … … 157 182 self.to_jid = to_jid 158 183 184 def initComponents(self): 185 sub = SubscribeTo("JID") 186 self.link((sub, 'outbox'), (self, 'jid')) 187 self.addChildren(sub) 188 sub.activate() 189 190 pub = PublishTo("DISCO_FEAT") 191 self.link((self, 'features-announce'), (pub, 'inbox')) 192 self.addChildren(sub) 193 sub.activate() 194 195 sub = SubscribeTo("BOUND") 196 self.link((sub, 'outbox'), (self, 'initiate')) 197 self.addChildren(sub) 198 sub.activate() 199 200 return 1 201 159 202 def main(self): 160 sub = SubscribeTo("JID") 161 sub.activate() 162 self.link((sub, 'outbox'), (self, 'jid')) 163 164 pub = PublishTo("DISCO_FEAT") 165 pub.activate() 166 self.link((self, 'features-announce'), (pub, 'inbox')) 167 168 sub = SubscribeTo("BOUND") 169 sub.activate() 170 self.link((sub, 'outbox'), (self, 'initiate')) 203 yield self.initComponents() 171 204 172 205 while 1: … … 206 239 207 240 class ActivityHandler(component): 208 Inboxes = {"inbox" : "",209 "control" : "Shutdown the client stream",241 Inboxes = {"inbox" : "headstock.api.discovery.FeaturesDiscovery instance", 242 "control" : "stops the component", 210 243 } 211 244 212 Outboxes = {"outbox" : "bridge.Element instance generated from the Message instance", 213 "signal" : "Shutdown signal", 214 "activity-supported": "", 245 Outboxes = {"outbox" : "UNUSED", 246 "signal" : "Shutdown signal", 247 "activity-supported": "when used this tells the RosterHandler it needs"\ 248 "to request the server for each contact's activity."\ 249 "This is only used when the server supports the feature", 215 250 } 216 251 … … 218 253 super(ActivityHandler, self).__init__() 219 254 255 def initComponents(self): 256 sub = SubscribeTo("DISCO_FEAT") 257 self.link((sub, 'outbox'), (self, 'inbox')) 258 self.addChildren(sub) 259 sub.activate() 260 261 return 1 262 220 263 def main(self): 221 sub = SubscribeTo("DISCO_FEAT")222 sub.activate()223 self.link((sub, 'outbox'), (self, 'inbox'))224 225 264 while 1: 226 265 if self.dataReady("control"):
