Пример #1
0
    def __init__ (self, apikey, myip, cifrouter, controlport, myid, thread_tracker, debug):
        self._lock = threading.RLock()
        self.thread_tracker = thread_tracker
        
        self.cf = Foundation({'apikey': apikey, 
		                 'myip': myip, 
		                 'cifrouter': cifrouter,
		                 'controlport': controlport,
		                 'routerid': "cif-router",
		                 'myid': myid,
                         'thread_tracker': self.thread_tracker})
        
        self.cf.setdebug(debug)
        self.debug = debug
        self.work_queue = deque()
        self.condition = threading.Condition()
        self.myid = myid
        self.myapikey = apikey

        self.pending_apikey_replies = {}
        self.pending_apikey_replies_lock = threading.RLock()
        
        self.apikey_cache = {}
        self.apikey_cache_lock = threading.RLock()
        
        if self.debug == 1:
            print "MiniClient: starting thread"
        self.t = threading.Thread(target=self.run, args=())
        self.t.daemon = True
        self.t.start()
        while not self.t.isAlive():
            print "waiting for pubsub relay thread to become alive"
            time.sleep(1)
        self.thread_tracker.add(id=self.t.ident, user='******', host='localhost', state='Running', info="Miniclient")
Пример #2
0
myip = '127.0.0.1'
try:
    myip = socket.gethostbyname(socket.gethostname())  # has caveats
except Exception as e:
    print "Trouble finding out our local ip based on hostname: ", socket.gethostname(
    )

global cf
global thread_tracker
thread_tracker = ThreadTracker(False)

cf = Foundation({
    'apikey': apikey,
    'myip': myip,
    'cifrouter': cifrouter,
    'controlport': controlport,
    'routerid': "cif-router",
    'myid': myid,
    'thread_tracker': thread_tracker
})

cf.setdebug(debug)

try:
    if debug > 1:
        print "Register with " + cifrouter + " (req->rep)"

    cf.ctrlsocket()
    (routerport, routerpubport) = cf.register()
    routerhname = cifrouter.split(':')
Пример #3
0
        apikey = a

myip = '127.0.0.1'
try:
    myip = socket.gethostbyname(socket.gethostname()) # has caveats
except Exception as e:
    print "Trouble finding out our local ip based on hostname: ", socket.gethostname()

global cf
global thread_tracker
thread_tracker = ThreadTracker(False)

cf = Foundation({'apikey': apikey, 
                 'myip': myip, 
                 'cifrouter': cifrouter,
                 'controlport': controlport,
                 'routerid': "cif-router",
                 'myid': myid,
                 'thread_tracker': thread_tracker})

cf.setdebug(debug)

try:
    if debug > 1:
        print "Register with " + cifrouter + " (req->rep)"
        
    cf.ctrlsocket()
    (routerport, routerpubport) = cf.register()
    routerhname = cifrouter.split(':')
    
    time.sleep(1) # wait for router to connect, sort of lame but see this a lot in zmq code
Пример #4
0
    elif o == "-r":
        cifrouter = a
    elif o == "-h":
        usage()
        sys.exit(2)



myip = socket.gethostbyname(socket.gethostname()) # has caveats
apikey = "c31ebb50-18df-4f47-8ec9-3f7ff778114a"

global cf
cf = Foundation({'apikey' : apikey,
                 'myip'   : myip,
                 'cifrouter' : cifrouter,
                 'controlport' : controlport,
                 'myid' : myid,
                 'routerid' : "cif-router"
                 })


try:
    print "Register with " + cifrouter + " (req->rep)"
    cf.ctrlsocket()
    (routerport, routerpubport) = cf.register()

    subscriber = cf.subscribersocket()
    
    time.sleep(1) # wait for router to connect, sort of lame but see this a lot in zmq code
    
    while True:
Пример #5
0
class MiniClient(object):
    def __init__ (self, apikey, myip, cifrouter, controlport, myid, thread_tracker, debug):
        self._lock = threading.RLock()
        self.thread_tracker = thread_tracker
        
        self.cf = Foundation({'apikey': apikey, 
		                 'myip': myip, 
		                 'cifrouter': cifrouter,
		                 'controlport': controlport,
		                 'routerid': "cif-router",
		                 'myid': myid,
                         'thread_tracker': self.thread_tracker})
        
        self.cf.setdebug(debug)
        self.debug = debug
        self.work_queue = deque()
        self.condition = threading.Condition()
        self.myid = myid
        self.myapikey = apikey

        self.pending_apikey_replies = {}
        self.pending_apikey_replies_lock = threading.RLock()
        
        self.apikey_cache = {}
        self.apikey_cache_lock = threading.RLock()
        
        if self.debug == 1:
            print "MiniClient: starting thread"
        self.t = threading.Thread(target=self.run, args=())
        self.t.daemon = True
        self.t.start()
        while not self.t.isAlive():
            print "waiting for pubsub relay thread to become alive"
            time.sleep(1)
        self.thread_tracker.add(id=self.t.ident, user='******', host='localhost', state='Running', info="Miniclient")
        
    def run(self):
        if self.debug == 1:
            print "MiniClient running"
        if self.debug == 1:
            print "MiniClient: creating control socket."
        self.cf.ctrlsocket()
        if self.debug == 1:
            print "MiniClient: registering with router."
        self.cf.register()

        self.condition.acquire()

        while True:
            if self.debug == 1:
                print "MiniClient: waiting for work"
            self.condition.wait()
        
            if self.debug == 1:
                print "MiniClient: got work " + str(len(self.work_queue))
    
            try:
                self._lock.acquire()
                work = self.work_queue.pop()
                self._lock.release()
                print "MiniClient: command=" + work['command']
                if work['command'] == "lookup_apikey":
                    self.do_lookup_apikey(work)

            except IndexError:
                break
                
        self.condition.release()
        self.thread_tracker.remove(self.t.ident)

    def pending(self):
        self.pending_apikey_replies_lock.acquire()
        n = len(self.pending_apikey_replies)
        self.pending_apikey_replies_lock.release()
        if n > 0:
            return True
        return False
    
    def pending_apikey_lookups(self):
        """
        Returns a list of all apikeys we've looked up for the router,
        but not the cached keys. 
        """
        self.pending_apikey_replies_lock.acquire()
        ks = self.pending_apikey_replies.keys()
        self.pending_apikey_replies_lock.release()
        return ks
    
    """
    Given an apikey, returns the apikey record that the db replied with
    and removes the reply from our internal list. Returns None if we 
    know nothing about the apikey
    """
    
    def get_pending_apikey(self, apikey):
        self.pending_apikey_replies_lock.acquire()
        r = None
        if apikey in self.pending_apikey_replies:
            r = self.pending_apikey_replies[apikey]
            del self.pending_apikey_replies[apikey]
        self.pending_apikey_replies_lock.release()
        return r
    
    def remove_pending_apikey(self, apikey):
        self.pending_apikey_replies_lock.acquire()
        if apikey in self.pending_apikey_replies:
            del self.pending_apikey_replies[apikey]
        self.pending_apikey_replies_lock.release()
        
    def do_lookup_apikey(self, work):
        print "do_lookup_apikey: " + work['apikey']
        
        apikey = work['apikey']
        
        self.apikey_cache_lock.acquire()
        
        if apikey in self.apikey_cache:
            self.pending_apikey_replies_lock.acquire()
            self.pending_apikey_replies[apikey] = self.apikey_cache[apikey]
            self.pending_apikey_replies_lock.release()
        else:
            self.fetch_apikey(apikey)
            
        self.apikey_cache_lock.release()
        
    def fetch_apikey(self, apikey):
        req = APIKeys.makerequest(self.myid, "cif-db", apikey, control_pb2.ControlType.APIKEY_GET)
        req.apikey = self.myapikey
        req.seq = APIKeys.makeseq(req)
        #print "fetch_apikey: sending to cif-db: ", req
    
        self.cf.sendmsg(req, self.fetch_apikey_finished)
        
    def fetch_apikey_finished(self, msg):
        #print "fetch_apikey_finished: ", msg
        #print " "
        
        if msg.status == control_pb2.ControlType.SUCCESS:
            self.pending_apikey_replies_lock.acquire()
            self.apikey_cache_lock.acquire()
            for reply in msg.apiKeyResponseList:
                self.pending_apikey_replies[reply.apikey] = reply
                self.apikey_cache[reply.apikey] = reply
            self.pending_apikey_replies_lock.release()
            self.apikey_cache_lock.release()

    def lookup_apikey(self, apikey):
        if apikey != None:
            print "MiniClient.lookup_apikey(" + apikey + "): acquiring lock"
            self._lock.acquire()
            self.work_queue.append({'command': 'lookup_apikey', 'apikey': apikey })
            print "MiniClient.lookup_apikey(" + apikey + "): acquiring condition"

            self.condition.acquire()
            self.condition.notify()
            self.condition.release()
            self._lock.release()
Пример #6
0
        sleeptime = float(a)
    elif o == "-n":
        count = int(a)
        if count > 0:
            count -= 1
    elif o == "-h":
        usage()
        sys.exit(2)

myip = socket.gethostbyname(socket.gethostname()) # has caveats

global cf
cf = Foundation({'apikey' : apikey,
                 'myip'   : myip,
                 'cifrouter' : cifrouter,
                 'controlport' : controlport,
                 'publisherport' : publisherport,
                 'myid' : myid,
                 'routerid' : "cif-router"
                 })


try:
    print "Register with " + cifrouter + " (req->rep)"

    cf.ctrlsocket()
    (routerport, routerpubport) = cf.register()
    publisher = cf.publishsocket()
    cf.ipublish()
    
    time.sleep(1) # wait for router to connect, sort of lame but see this a lot in zmq code
    
Пример #7
0
     log = Log(connectionPool)
     log.L("cif-db initializing")
     
     print "Initializing APIKeys object"
     apikeys = APIKeys(connection, True)
     
     print "Resolving our APIKey: " + myid
     
     apikey = apikeys.get_by_alias(myid)
     
     print "Initializing foundation"
     
     cf = Foundation({'apikey' : apikey,
                      'myip'   : myip,
                      'cifrouter' : cifrouter,
                      'controlport' : controlport,
                      'myid' : myid,
                      'routerid' : "cif-router",
                      'thread_tracker' : thread_tracker
                      })
 
     primary_index = PrimaryIndex(connectionPool, debug)
     secondary_index = SecondaryIndex(connectionPool, debug)
     
     print "Configuring foundation"
     
     cf.setdebug(debug)
     cf.setdefaultcallback(controlMessageHandler, {'connectionPool': connectionPool})
     
     print "Register with " + cifrouter + " (req->rep)"
     req = cf.ctrlsocket()