<s> """<STR_LIT>""" <EOL> import sys <EOL> import time <EOL> import threading <EOL> import xmpp <EOL> global devices <EOL> global datasources <EOL> global connection <EOL> global options <EOL> global threads_running <EOL> global outputBox <EOL> kill_threads = False <EOL> class PublishToExosite ( threading . Thread ) : <EOL> def __init__ ( self , connection = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:password>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . ringHead = <NUM_LIT:0> <EOL> self . ringTail = <NUM_LIT:0> <EOL> self . ringItems = <NUM_LIT:0> <EOL> self . ringSize = <NUM_LIT> <EOL> self . ring = { } <EOL> self . exompp = Exompp ( connection ) <EOL> if - <NUM_LIT:1> == self . exompp . connect ( ) : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> self . datasources = { } <EOL> def stop ( self ) : <EOL> global kill_threads <EOL> kill_threads = True <EOL> def addData ( self , device_cik , resource , value ) : <EOL> if self . ringItems + <NUM_LIT:1> > self . ringSize : <EOL> print "<STR_LIT>" <EOL> self . ringItems = <NUM_LIT:0> <EOL> self . ringHead = <NUM_LIT:0> <EOL> self . ringTail = <NUM_LIT:0> <EOL> self . ring [ self . ringHead ] = { '<STR_LIT>' : device_cik , '<STR_LIT>' : resource , '<STR_LIT>' : value } <EOL> self . ringItems += <NUM_LIT:1> <EOL> if self . ringHead + <NUM_LIT:1> > self . ringSize : <EOL> self . ringHead = <NUM_LIT:0> <EOL> else : self . ringHead += <NUM_LIT:1> <EOL> def run ( self ) : <EOL> global kill_threads <EOL> lastcik = '<STR_LIT>' <EOL> lastringsize = <NUM_LIT:0> <EOL> message_output = True <EOL> print "<STR_LIT>" <EOL> while False == kill_threads : <EOL> if self . ringItems > <NUM_LIT:0> : <EOL> device_cik = self . ring [ self . ringTail ] [ '<STR_LIT>' ] <EOL> try : <EOL> self . datasources [ device_cik ] <EOL> except : <EOL> self . datasources [ device_cik ] = self . exompp . listdatasources ( device_cik ) <EOL> res_name = self . ring [ self . ringTail ] [ '<STR_LIT>' ] <EOL> res_value = self . ring [ self . ringTail ] [ '<STR_LIT>' ] <EOL> if self . ringTail + <NUM_LIT:1> > self . ringSize : <EOL> self . ringTail = <NUM_LIT:0> <EOL> else : self . ringTail += <NUM_LIT:1> <EOL> if self . ringItems > <NUM_LIT:0> : <EOL> self . ringItems -= <NUM_LIT:1> <EOL> if lastcik != device_cik : <EOL> lastcik = device_cik <EOL> if - <NUM_LIT:1> == self . exompp . setcik ( device_cik ) : <EOL> message_output = True <EOL> print "<STR_LIT>" % device_cik <EOL> lastcik = '<STR_LIT>' <EOL> continue <EOL> try : <EOL> res_number = self . datasources [ device_cik ] [ res_name ] <EOL> except : <EOL> message_output = True <EOL> print "<STR_LIT>" % res_name <EOL> res_number = <NUM_LIT:0> <EOL> try : <EOL> for a , b in self . datasources [ device_cik ] . iteritems ( ) : <EOL> if int ( b ) > int ( res_number ) : res_number = int ( b ) <EOL> res_number += <NUM_LIT:1> <EOL> except : <EOL> res_number = <NUM_LIT:1> <EOL> if - <NUM_LIT:1> == self . exompp . createdatasource ( res_name , res_number ) : <EOL> print "<STR_LIT>" % res_name <EOL> continue <EOL> else : <EOL> self . datasources [ device_cik ] = self . exompp . listdatasources ( device_cik ) <EOL> if - <NUM_LIT:1> == self . exompp . write ( res_number , res_value ) : <EOL> message_output = True <EOL> lastcik = '<STR_LIT>' <EOL> continue <EOL> else : <EOL> if True == message_output : <EOL> print "<STR_LIT>" <EOL> sys . stdout . write ( '<STR_LIT>' ) <EOL> message_output = False <EOL> currentringitems = self . ringItems <EOL> if lastringsize != currentringitems : <EOL> bufstring = '<STR_LIT>' <EOL> rewstring = '<STR_LIT>' <EOL> if lastringsize < currentringitems : <EOL> sizediff = currentringitems - lastringsize <EOL> while sizediff > <NUM_LIT:0> : <EOL> bufstring += '<STR_LIT:*>' <EOL> sizediff -= <NUM_LIT:1> <EOL> sys . stdout . write ( bufstring ) <EOL> else : <EOL> sizediff = lastringsize - currentringitems <EOL> while sizediff > <NUM_LIT:0> : <EOL> bufstring += '<STR_LIT:U+0020>' <EOL> rewstring += '<STR_LIT>' <EOL> sizediff -= <NUM_LIT:1> <EOL> sys . stdout . write ( rewstring ) <EOL> sys . stdout . write ( bufstring ) <EOL> sys . stdout . write ( rewstring ) <EOL> lastringsize = currentringitems <EOL> sys . stdout . flush ( ) <EOL> else : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> class Exompp ( ) : <EOL> def __init__ ( self , connection ) : <EOL> self . connection = connection <EOL> self . duplicate = False <EOL> self . dsname = '<STR_LIT>' <EOL> self . dsresource = '<STR_LIT>' <EOL> def connect ( self ) : <EOL> retry = <NUM_LIT:1> <EOL> while <NUM_LIT:0> != retry : <EOL> if retry > <NUM_LIT:1> : <EOL> time . sleep ( <NUM_LIT:10> ) <EOL> try : <EOL> jid = xmpp . protocol . JID ( self . connection [ '<STR_LIT>' ] ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> retry += <NUM_LIT:1> <EOL> continue <EOL> cl = xmpp . Client ( jid . getDomain ( ) , debug = <NUM_LIT:0> ) <EOL> self . messenger = Messenger ( cl ) <EOL> try : <EOL> con = cl . connect ( ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> retry += <NUM_LIT:1> <EOL> continue <EOL> auth = <NUM_LIT:0> <EOL> try : <EOL> auth = cl . auth ( jid . getNode ( ) , self . connection [ '<STR_LIT:password>' ] ) <EOL> except : <EOL> print "<STR_LIT>" <EOL> retry += <NUM_LIT:1> <EOL> continue <EOL> if not auth : <EOL> print "<STR_LIT>" <EOL> retry += <NUM_LIT:1> <EOL> continue <EOL> cl . RegisterHandler ( '<STR_LIT:message>' , self . messenger . message_handler ) <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . stdcallback , '<STR_LIT>' ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> retry += <NUM_LIT:1> <EOL> continue <EOL> else : <EOL> if retry > <NUM_LIT:1> : print "<STR_LIT>" <EOL> retry = <NUM_LIT:0> <EOL> def setcik ( self , cikvalue ) : <EOL> retval = self . rawwrite ( '<STR_LIT>' % cikvalue , '<STR_LIT>' ) <EOL> if - <NUM_LIT:1> == retval : <EOL> print "<STR_LIT>" % cikvalue <EOL> return retval <EOL> def createdatasource ( self , name , resource ) : <EOL> self . dsname = name <EOL> self . dsresource = resource <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . checkdsexistscallback ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % ( name , resource ) <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> if self . duplicate : <EOL> print "<STR_LIT>" % name <EOL> self . duplicate = False <EOL> else : <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' % ( name , resource ) , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . cdscallback ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % ( name , resource ) <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> def rawwrite ( self , messagebody , expected = '<STR_LIT>' ) : <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = messagebody , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . stdcallback , expected ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % messagebody . strip ( ) <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> def write ( self , resource , data ) : <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' % ( resource , data ) , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . stdcallback , '<STR_LIT>' ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % resource <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> def dsread ( self , ds_name , points ) : <EOL> if <NUM_LIT:1> != points : <EOL> print "<STR_LIT>" <EOL> points = <NUM_LIT:1> <EOL> self . readmessage = { } <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' % ( ds_name , points ) , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . readcallback ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % ds_name <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> return self . readmessage <EOL> def read ( self , resource ) : <EOL> self . readmessage = { } <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' % resource , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . readcallback ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % resource <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> return self . readmessage <EOL> def listdatasources ( self , cik ) : <EOL> self . dslist = { } <EOL> self . setcik ( cik ) <EOL> msg = xmpp . protocol . Message ( to = self . connection [ '<STR_LIT>' ] , <EOL> body = '<STR_LIT>' , <EOL> typ = '<STR_LIT>' ) <EOL> self . messenger . send ( msg , self . dslistcallback ) <EOL> if self . messenger . wait ( ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % cik <EOL> self . connect ( ) <EOL> return - <NUM_LIT:1> <EOL> return self . dslist <EOL> def stdcallback ( self , response , expected ) : <EOL> if expected != '<STR_LIT>' : <EOL> if response != expected : return - <NUM_LIT:1> <EOL> def cdscallback ( self , response , expected ) : <EOL> if response . find ( "<STR_LIT:error>" ) != - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % response <EOL> return - <NUM_LIT:1> <EOL> def checkdsexistscallback ( self , response , expected ) : <EOL> start = response . find ( self . dsname ) <EOL> if start != - <NUM_LIT:1> : <EOL> end = response . find ( '<STR_LIT:U+002C>' , start ) <EOL> if end - start > len ( self . dsname ) : return <EOL> self . duplicate = True <EOL> start = response . find ( '<STR_LIT:U+002C>' , start ) + <NUM_LIT:1> <EOL> end = response . find ( '<STR_LIT:U+002C>' , start ) <EOL> if self . dsresource != response [ start : end ] : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> def dslistcallback ( self , response , expected ) : <EOL> if - <NUM_LIT:1> == response . find ( '<STR_LIT:error>' ) : <EOL> start = <NUM_LIT:0> <EOL> end = <NUM_LIT:1> <EOL> while - <NUM_LIT:1> != start : <EOL> end = response . find ( '<STR_LIT:U+002C>' , start ) <EOL> dsname = response [ start : end ] <EOL> start = end + <NUM_LIT:1> <EOL> end = response . find ( '<STR_LIT:U+002C>' , start ) <EOL> self . dslist [ dsname ] = response [ start : end ] <EOL> start = response . find ( '<STR_LIT>' , end ) <EOL> if - <NUM_LIT:1> != start : start += <NUM_LIT:1> <EOL> def readcallback ( self , response , expected ) : <EOL> if - <NUM_LIT:1> == response . find ( '<STR_LIT:error>' ) : <EOL> start = <NUM_LIT:0> <EOL> response = response . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> length = len ( response ) <EOL> start = response . find ( '<STR_LIT:U+002C>' , start ) + <NUM_LIT:1> <EOL> if '<STR_LIT>' == response [ start ] : start += <NUM_LIT:1> <EOL> end = length <EOL> if '<STR_LIT>' == response [ end - <NUM_LIT:1> ] : end -= <NUM_LIT:1> <EOL> self . readmessage = response [ start : end ] <EOL> else : <EOL> print "<STR_LIT>" <EOL> return - <NUM_LIT:1> <EOL> class Messenger ( object ) : <EOL> def __init__ ( self , client ) : <EOL> self . wait_for_response = False <EOL> self . callback = None <EOL> self . client = client <EOL> self . start = <NUM_LIT:0> <EOL> def wait ( self ) : <EOL> self . start = time . clock ( ) <EOL> while self . wait_for_response : <EOL> if time . clock ( ) - self . start > <NUM_LIT:10> : <EOL> self . wait_for_response = False <EOL> return - <NUM_LIT:1> <EOL> if not self . client . Process ( <NUM_LIT:1> ) : <EOL> print '<STR_LIT>' <EOL> return - <NUM_LIT:1> <EOL> def message_handler ( self , con , event ) : <EOL> response = event . getBody ( ) <EOL> if self . callback : <EOL> if - <NUM_LIT:1> == self . callback ( response , self . callbackexpected ) : <EOL> print "<STR_LIT>" % response <EOL> self . start = time . clock ( ) - <NUM_LIT:11> <EOL> else : <EOL> self . wait_for_response = False <EOL> else : <EOL> if response . find ( "<STR_LIT>" ) == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" % response <EOL> self . start = time . clock ( ) - <NUM_LIT:11> <EOL> else : <EOL> self . wait_for_response = False <EOL> def send ( self , message , callback = None , callbackexpected = '<STR_LIT>' ) : <EOL> self . wait_for_response = True <EOL> self . callback = callback <EOL> self . callbackexpected = callbackexpected <EOL> self . client . send ( message ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
