Пример #1
0
    def run(self):
        # Establish a connection with the redis server                                          
        r = redis.Redis(host=self.host,port=self.port,db=self.db)
        tasklib.register("resolver",self)
        try:
            while True:
                # Get an outgoing message                                                       
                target, msg = self.recv()

                print("RESOLVER:", target, msg)

                # Check if a proxy was already registered                                       
                if tasklib.lookup(target):
                    tasklib.send(target,msg)
                    continue

                # See if redis knows anything about the target                                  
                try:
                    target_info = r.get(target)
                    if target_info:
                        host, port = target_info.decode('utf-8').split(":")
                        port = int(port)

                        # Create a proxy task                                                   
                        proxy(target,target,(host,port),self.authkey)
                        self.log.info("Connected to %s", (host,port))
                        # Send the message to the proxy                                         
                        tasklib.send(target,msg)
                    else:
                        self.log.info("Nothing known about target '%s'", target)
                except Exception as e:
                    self.log.info("Couldn't resolve '%s' : %s:%s", target, type(e),e)
        finally:
            tasklib.unregister("resolver")
Пример #2
0
 def run(self):
     try:
         while True:
             target,msg = self.conn.recv()
             tasklib.send(target,msg)
     finally:
         self.conn.close()
Пример #3
0
 def run(self):
     try:
         self.log.info("Client handling connection %s, %s", self.client_sock, self.client_addr)
         self.client_sock.settimeout(5)
         while self.runnable:
             try:
                 busid_bytes = self.client_sock.recv(16)
             except socket.error:
                 return
             except socket.timeout:
                 continue
             if not busid_bytes:
                 break
             busid = busid_bytes.decode('ascii').strip()
             # Send a message to the database task and get the response
             print("sending")
             tasklib.send(self.busdb_name, ('getid',(busid,self.name)))
             print("sent")
             try:
                 print("rxing")
                 tag, bus = self.recv(timeout=10)
                 print("rx'd")
                 if bus:
                     resp = response_template.format(**bus)
                     self.client_sock.send(resp.encode('ascii'))
                     self.client_sock.close()
                 else:
                     self.client_sock.send(b"unknown\n")
                     self.client_sock.close()
             except tasklib.RecvTimeoutError:
                 self.client_sock.send(b"timeout\n")
     finally:
         self.client_sock.close()
Пример #4
0
 def run(self):
     while True:
         tag, msg = self.recv()
         # If 'bus' message, update the internal dictionary
         if tag == 'bus':
             bus = msg
             self.data[bus['id']] = bus
         # If 'getid' message, get the busid and target for response data
         elif tag == 'getid':
             busid, target = msg
             bus = self.data.get(busid)
             time.sleep(1)
             tasklib.send(target, ('bus',copy.deepcopy(bus)))
Пример #5
0
 def run(self):
     sock = socket(AF_INET, SOCK_DGRAM)
     sock.settimeout(5)
     try:
         sock.sendto(b"",("localhost",31337))
         while self.runnable:
             try:
                 msg, addr = sock.recvfrom(8192)
             except timeout:
                 continue
             msg = msg.decode('ascii')
             fields = msg.split(",")
             # Make a dictionary from the update data
             bus = {
                 'timestamp' : float(fields[1]),
                 'id': fields[2],
                 'run' : fields[3],
                 'route' : fields[4],
                 'lat' : float(fields[5]),
                 'lon' : float(fields[6])
             }
             tasklib.send(self.target, ('bus',bus))
     finally:
         sock.close()
Пример #6
0
 def run(self):
     while True:
         msg = self.recv()
         for target in self.targets:
             tasklib.send(target, msg)
Пример #7
0
 def run(self):
     while True:
         msg = self.recv()
         for target in self.targets:
             tasklib.send(target, msg)