Beispiel #1
0
 def run(self):
     """The actual thread code"""
     self._log.debug('Starting %s ' % self.name)
     self._running = True
     self._started = True
     job_info = None
     
     while not self._stop_event.isSet():
         job_info = utils.q_get(self._job_queue, get_timeout=0.5)
         if job_info:
             self._execute_job(job_info)
             
     # Execute remaining jobs
     while True:
         job_info = utils.q_get(self._job_queue, get_timeout=0.5)
         if job_info:
             self._execute_job(job_info)
         else:
             break
     utils.wait(5)
     self._running = False
     #self._log.debug('Stopping %s ' % self.name)
     if self._exit_callback:
         self._exit_callback(self)
     self._log.debug('Exiting %s ' % self.name)
Beispiel #2
0
 def read(self, read_timeout=0):
     """ Read from the console
         Return the console input or None
     """
     if self._console_read_q is not None:
         return utils.q_get(self._console_read_q, get_timeout=read_timeout)
     else:
         utils.wait(read_timeout)
         return None
Beispiel #3
0
 def _get_rpc_request(self, timeout=None):
     # Get size and marshalled request from server
     req = utils.q_get(self._read_q, get_timeout=timeout)
     if req is None:
         return None
     if len(req) < 2:
         self._log.error(
             'RPCConnectionThread._get_rpc_request: Did not get both size bytes'
         )
         return None
     size = ord(req[0]) * 256 + ord(req[1])
     req = req[2:]
     while len(req) < size:
         chunk = utils.q_get(self._read_q, get_timeout=self._timeout)
         if chunk is None:
             self._log.error(
                 'RPCConnectionThread._get_rpc_request: timed out waiting for end of request'
             )
             return None
         req += chunk
     args = marshal.loads(req)
     self._log.info('RPCConnectionThread._get_rpc_request: args = %s' %
                    repr(args))
     cmd_parts = []
     for i, arg in enumerate(args):
         if i == 0:
             cmd_parts.append(''.join(['self._base_rpc_server.', arg, '(']))
         else:
             if i != 1:
                 cmd_parts.append(', ')
             cmd_parts.append(arg)
     cmd_parts.append(')')
     cmd = ''.join(cmd_parts)
     self._log.info(
         'RPCConnectionThread._get_rpc_request: returning command: %s' %
         cmd)
     return cmd
Beispiel #4
0
    def run(self):
        """The actual thread code"""
        self._log.debug('Starting %s ' % self.name)
        self._running = True
        self._started = True

        while not self._stop_event.isSet():
            data = utils.q_get(self._data_q, get_timeout=0.5)
            if data:
                #self._log.debug('ModemWriteThread sent a pkt')
                self._modem.write(data)

        self._running = False
        #self._log.debug('Stopping %s ' % self.name)
        self._log.debug('Exiting %s ' % self.name)
Beispiel #5
0
 def _get_rpc_response(self):
     # Get size and marshalled response from server
     resp = utils.q_get(self._read_q, get_timeout=self._timeout)
     if resp is None:
         self._log.error(
             'RPCServerProxy._get_rpc_response: timed out waiting for start of response'
         )
         return None
     if len(resp) < 2:
         self._log.error(
             'RPCServerProxy._get_rpc_response: Did not get both size bytes'
         )
         return None
     size = ord(resp[0]) * 256 + ord(resp[1])
     resp = resp[2:]
     while len(resp) < size:
         chunk = utils.q_get(self._read_q, get_timeout=self._timeout)
         if chunk is None:
             self._log.error(
                 'RPCServerProxy._get_rpc_response: timed out waiting for end of response'
             )
             return None
         resp += chunk
     return marshal.loads(resp)
Beispiel #6
0
    def run(self):
        """The actual thread code"""
        self._log.debug('Starting %s ' % self.name)
        self._running = True
        self._started = True

        while not self._stop_event.isSet():
            pkt = utils.q_get(self._pkt_q, get_timeout=0.5)
            if pkt is not None:
                self._transmit_pkt(pkt)

        self._running = False
        #self._log.debug('Stopping %s ' % self.name)
        if self._exit_callback:
            self._exit_callback(self)
        self._log.debug('Exiting %s ' % self.name)
Beispiel #7
0
    def run(self):
        """The actual thread code"""
        self._log.debug('Starting %s ' % self.name)
        self._running = True
        self._started = True

        while not self._stop_event.isSet():
            if not self._proc_info.sock_h.is_running():
                break
            s = utils.q_get(self._proc_info.sock_h.get_read_q(),
                            get_timeout=0.5)
            if s:
                print s,

        self._running = False
        #self._log.debug('Stopping %s ' % self.name)
        self._log.debug('Exiting %s ' % self.name)
Beispiel #8
0
 def run(self):
     """The actual thread code"""
     #self._log.debug('Starting %s' % self.name)
     try:
         self._sock.settimeout(0.5)
     except socket.error:
         # client disconnected
         self._stop_event.set()
     self._started = True
     self._running = True
     while not self._stop_event.isSet():
         self._check_for_new_data_q()
         if self._data_q is None:
             # no data queue exists yet
             utils.wait(0.5)
             continue
         data = utils.q_get(self._data_q, get_timeout=1)
         if data is None:
             continue
         #self._log.debug('SockWriteThread: dequeued a %s' % repr(data))
         startTime = datetime.now()
         # keep sending until all data is sent or timeout
         while data and (not self._stop_event.isSet()):
             while True:
                 try:
                     sent = self._sock.send(data[:ProxyPkt.MAX_PKT_LEN])
                 except OSError, e:
                     if e.errno == EINTR:
                         continue
                 except socket.error:
                     # client disconnected
                     self._stop_event.set()
                     break  # out of while True
                 break # out of while True
             if self._stop_event.isSet():
                 break
             #self._log.debug('SockWriteThread: Sent %s bytes to %s' % (str(sent), self._client))
             data = data[sent:]
             elapsedTime = datetime.now() - startTime
             if elapsedTime > self._timeout:
                 self._log.error('Timed out writing to %s' % self._peer_addr_port)
                 self._stop_event.set()
                 break
Beispiel #9
0
    def run(self):
        """The actual thread code"""
        self._log.debug('Starting %s ' % self.name)
        self._running = True
        self._started = True

        while not self._stop_event.isSet():
            if not self._sock_h.is_running():
                self._stop_event.set()
                continue
            data = utils.q_get(self._stream_read_q, get_timeout=0.5)
            if data is not None:
                #self._log.debug('Packetize: received %s bytes' % str(len(data)))
                self._send_passthrough_pkts(data)

        self._running = False
        #self._log.debug('Stopping %s ' % self.name)
        if self._exit_callback:
            self._exit_callback(self)
        self._log.debug('Exiting %s ' % self.name)
Beispiel #10
0
 def _readline(self, sock):
     """ Read a line ending with \n from sock.
         Return received line.
         Return None if shutdown is pending.
     """
     timeout_secs = 2
     max_rx_buf_len = 200
     rx_buf = ''
     while not self._shut_down_is_pending():
         rx_bytes = utils.q_get(self._sock_h.get_read_q(), timeout_secs)
         if rx_bytes is None:
             continue  # timed out
         rx_buf += rx_bytes
         if rx_buf.endswith('\n'):
             return rx_buf
         if len(rx_buf) > max_rx_buf_len:
             self._log.error('Flushing _readline.rx_buf. Received "%s"' %
                             rx_buf)
             rx_buf = ''
     return None
Beispiel #11
0
    def run(self):
        """The actual thread code"""
        self._log.debug('Starting %s ' % self.name)
        self._running = True
        self._started = True

        while not self._stop_event.isSet():
            if not self._sock_h.is_running():
                self._log.debug(
                    'Depacketize: exiting because sock_h is not running')
                self._stop_event.set()
                continue
            #self._log.debug('Depacketize: polling queue %s' % repr(self._pkt_read_q))
            pkt_buf = utils.q_get(self._pkt_read_q, get_timeout=0.5)
            if pkt_buf is None:
                continue
            #self._log.debug('Depacketize: got a pkt')
            pkt = ProxyPkt(pkt_buf)
            pkt_type = pkt.get_type()
            if pkt_type == ProxyPkt.PASSTHROUGH:
                #self._log.debug('Depacketize: a PASSTHROUGH pkt')
                self._send_stream_data(pkt)
                continue
            if pkt_type == ProxyPkt.CONNECT:
                # Got a CONNECT pkt from the other proxy.
                #  Ignore since we are already connected.
                continue
            if pkt_type == ProxyPkt.DISCONNECT:
                # Got a DISCONNECT pkt from the other proxy
                self._log.debug('Depacketize: Got DISCONNECT')
                self._got_disconnect_pkt = True
                self._stop_event.set()
                self._log.debug(
                    'Depacketize: exiting because got a DISCONNECT')
                continue

        self._running = False
        #self._log.debug('Stopping %s ' % self.name)
        if self._exit_callback:
            self._exit_callback(self)
        self._log.debug('Exiting %s ' % self.name)
Beispiel #12
0
 def run(self):
     """The actual thread code"""
     self._log.debug('Starting %s' % self.name)
     self._running = True
     self._started = True
     read_pkt_q = self._modem_svr_sock_h.get_read_q()
     write_pkt_q = self._modem_svr_sock_h.get_write_q()
     self._update_connected_flag()
     
     #self._log.debug('ModemSvrConnection: modem_svr_sock_h read q is %s' % repr(read_pkt_q))
     #self._log.debug('ModemSvrConnection: modem_svr_sock_h write q is %s' % repr(write_pkt_q))
     while not self._stop_event.isSet():
         # If modem server connection is lost, exit thread
         if not self._modem_svr_sock_h.is_running():
             self._stop_event.set()
             continue
         pkt_buf = utils.q_get(read_pkt_q, get_timeout=0.5)
         if pkt_buf:
             pkt = ProxyPkt(pkt_buf)
             #self._log.debug('ModemSvrConnection: received pkt')                
             #self._log.debug(pkt.str())                
             pkt_type = pkt.get_type()
             if pkt_type == ProxyPkt.PING:
                 #self._log.debug('Got a PING packet.')
                 self._send_ping(write_pkt_q)
                 continue
             if pkt_type == ProxyPkt.ICCID_REQ:
                 self._xfer_rec.set_time(time.time())
                 self._send_iccid(write_pkt_q)
                 continue
             if (pkt_type == ProxyPkt.PASSTHROUGH) or \
                 (pkt_type == ProxyPkt.DISCONNECT):
                 self._xfer_rec.set_time(time.time())                    
                 self._children_lock.acquire()
                 for ch in self._children:                       
                     if (ch.get_server_port() == pkt.get_dest_port()) and \
                             (ch.get_client_port() == pkt.get_src_port()):
                         ch.send(pkt)
                         break
                 self._children_lock.release()
                 continue                    
                 
             if pkt_type == ProxyPkt.CONNECT:
                 self._xfer_rec.set_time(time.time())
                 self._log.debug('ModemSvrConnection: got CONNECT')
                 sc = ServerConnection(write_pkt_q,
                                     pkt.get_dest_port(),
                                     pkt.get_src_port(),
                                     self._log,
                                     exit_callback=self._my_exit_callback)
                 if sc.is_connected():
                     self._children_lock.acquire()
                     self._children.append(sc)
                     self._children_lock.release()
         
     self._running = False
     self._log.debug('Stopping %s' % self.name)
     self._update_disconnected_flag()
     for ch in self._children:
         ch.stop()
     self._log.debug('Exiting %s' % self.name)