Beispiel #1
0
 def _receive_response(self, response_obj, timeout_secs):
     # If our response object is fixed width, then just read the number of bytes
     # we need, parse the response, and return.
     if response_obj.isFixedWidth():
         len_obj = response_obj.getWidth()
         data = self.connection.read(len_obj, timeout_secs)
         match_obj = response_obj.isMatch(data, 0)
         # If we got a match, then return success!
         if match_obj.isMatch() == 1:
             return 1
         # Apparently we didn't get the response we were expecting.  Return failure.
         return 0
     else:
         # Our response object is not fixed width, so read data one byte at a time
         # until we either get a full match, a definitive non-match or a timeout.
         data = ''
         st_time = gdutil.get_time()
         while 1:
             curtime = gdutil.get_time()
             if curtime - st_time > timeout_secs:
                 raise gdutil.GDTimeoutException("Got trying to read matching data")
             new_data = self.connection.read(1, timeout_secs)
             if new_data:
                 data += new_data
             if self.debug > 2:
                 print 'calling isMatch with %s' % gdutil.dump_binary_str(data)
             match_obj = response_obj.isMatch(data, 0)
             # If we got a match, then return success!
             if match_obj.isMatch() == 1:
                 return 1
             # If we got a definitive non-match, return failure.
             if match_obj.isNotMatch() == 1:
                 return 0        
    def test_get_time(self):
        t1 = gu.get_time()
        time.sleep(1.1)
        t2 = gu.get_time()

        diff = t2 - t1

        if diff < 1 or diff > 2:
            raise "Difference out of range.  Should be ~1.1, got %f" % diff
Beispiel #3
0
    def test_get_time(self):
        t1 = gu.get_time()
        time.sleep(1.1)
        t2 = gu.get_time()

        diff = t2 - t1

        if diff < 1 or diff > 2:
            raise "Difference out of range.  Should be ~1.1, got %f" % diff
Beispiel #4
0
 def read(self, num_bytes, timeout_secs):
     st_time = gdutil.get_time()
     msg = ''
     while len(msg) < num_bytes:
         curtime = gdutil.get_time()
         if curtime - st_time > timeout_secs:
             raise gdutil.GDTimeoutException("Timeout reading data")
         chunk = self.sock.recv(num_bytes-len(msg))
         if chunk == '':
             raise gdutil.GDConnectionClosedException("Socket connection broken while reading")
         msg = msg + chunk
     return msg
Beispiel #5
0
 def write(self, msg, num_bytes, timeout_secs):
     st_time = gdutil.get_time()
     totalsent = 0
     while totalsent < num_bytes:
         # @fixme: Only send at most the appropriate number of remaining bytes
         #         (num_bytes - totalsent)
         curtime = gdutil.get_time()
         if curtime - st_time > timeout_secs:
             raise gdutil.GDTimeoutException("Timeout writing data")
         sent = self.sock.send(msg[totalsent:])
         if sent == 0:
             raise gdutil.GDConnectionClosedException("Socket connection broken while writing")
         totalsent = totalsent + sent
         print 'totalsent', totalsent
Beispiel #6
0
    def read(self, num_bytes, timeout_secs):
        buffer = array.array('B')

        st_time = gdutil.get_time()
        
        while len(buffer) < num_bytes:
            curtime = gdutil.get_time()
            if curtime - st_time > timeout_secs:
                raise gdutil.GDTimeoutException("Timeout reading data")

            bytes_read = self.port_node.read(buffer, num_bytes, timeout_secs)

            if len(buffer) == num_bytes:
                return buffer.tostring()
Beispiel #7
0
 def get(self, skipCache=0):
     doget = 0
     if skipCache:
         doget = 1
     if self.last_read_time == 0:
         doget = 1
     if not doget:
         if self.refresh_secs == 0:
             doget = 1
     if not doget:
         curtime = gdutil.get_time()
         if curtime - self.last_read_time > self.refresh_secs:
             # Looks like our value needs to be refreshed.
             doget = 1
     if doget:
         new_val = self._getValue(self.lh, self.po_obj)
         #
         if not new_val is None:
             self.value = new_val
             self.last_read_time = gdutil.get_time()
         return new_val
     return self.value
Beispiel #8
0
 def _setValue(self, new_value):
     self.value = new_value
     self.last_read_time = gdutil.get_time()