Пример #1
0
    def read_magic_hi(self):
        """read and parse magic_hi from data socket, set version

         if version > 0, set nextra """

        # if self.verb > 3: self.peek_at_next_bytes(8)

        data = self.read_nbytes_from_data_socket(g_magic_len)
        if not data: return 1

        odata = [ord(v) for v in data]
        if self.verb > 2:
            print '++ recieved as magic_hi: %s' % UTIL.data_to_hex_str(odata)

        # test whether we have magic, start by ignoring the last byte
        for ind in range(g_magic_len - 1):
            if odata[ind] != g_magic_hi[ind]:
                print '** HELLO string is not magic, want %s but have %s' \
                   % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata))
                return 1

        # now check the last byte for HELLO and version
        self.version = odata[g_magic_len - 1] - g_magic_hi[g_magic_len - 1]
        if self.verb > 2:
            print '-- hello version is %d' % self.version

        # and deal with the version number

        if self.version == 0: pass  # we're good to go

        elif self.version == 1 or self.version == 2:
            # ------------------------------------------------------------
            # read the next 4-byte int to determine the number of extra data
            # values received each TR
            #    version 1: receive num_extra int over socket
            #    version 2: receive num_voxels (for 8 vals each) int over socket

            ilist = self.read_ints_from_socket(1)
            if ilist == None: return 1

            if ilist[0] < 0:
                print '** received invalid num_extra = %d' % ilist[0]
            elif self.version == 1:
                self.nextra = ilist[0]
            else:  # version = 2
                self.nextra = ilist[0] * 8

            if self.verb > 2:
                print '-- num extra = %d' % self.nextra

        else:  # bad, naughty version!
            print '** HELLO string trailer is not magic, want %s but have %s' \
               % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata))
            return 1

        # todo - show_time()

        return 0
Пример #2
0
   def read_magic_hi(self):
      """read and parse magic_hi from data socket, set version

         if version > 0, set nextra """

      # if self.verb > 3: self.peek_at_next_bytes(8)

      data = self.read_nbytes_from_data_socket(g_magic_len)
      if not data: return 1

      odata = [ord(v) for v in data]
      if self.verb > 2:
         print '++ recieved as magic_hi: %s' % UTIL.data_to_hex_str(odata)

      # test whether we have magic, start by ignoring the last byte
      for ind in range(g_magic_len-1):
         if odata[ind] != g_magic_hi[ind]:
            print '** HELLO string is not magic, want %s but have %s' \
               % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata))
            return 1

      # now check the last byte for HELLO and version
      self.version = odata[g_magic_len-1] - g_magic_hi[g_magic_len-1]
      if self.verb > 2:
         print '-- hello version is %d' % self.version

      # and deal with the version number

      if self.version == 0: pass        # we're good to go

      elif self.version == 1 or self.version == 2:
         # ------------------------------------------------------------
         # read the next 4-byte int to determine the number of extra data
         # values received each TR
         #    version 1: receive num_extra int over socket
         #    version 2: receive num_voxels (for 8 vals each) int over socket

         ilist = self.read_ints_from_socket(1)
         if ilist == None: return 1

         if ilist[0] < 0: print '** received invalid num_extra = %d' % ilist[0]
         elif self.version == 1:
            self.nextra = ilist[0]
         else: # version = 2
            self.nextra = ilist[0] * 8

         if self.verb > 2:
            print '-- num extra = %d' % self.nextra
Пример #3
0
   def read_nbytes_from_data_socket(self, nbytes, flag=socket.MSG_WAITALL):
      """try to read nbytes from data socket, reporting any errors"""
      errs = 0

      # It is important to specify the list of exceptions to trap here,
      # otherwise it would catch a ctrl-c and continue after sys.exit().
      try: data = self.data_sock.recv(nbytes, flag)
      except(socket.error, socket.herror, socket.gaierror, socket.timeout):
         if self.verb > 0: print '** recv() exception on data socket'
         errs = 1
      if errs: return None
      if not data:
         if self.verb > 0:
            print '** failed recv() of %d bytes from data socket' % nbytes
         return None

      if self.verb > 4:
         print "++ read %d bytes from socket: %s" \
               % (nbytes, UTIL.data_to_hex_str([ord(v) for v in data]))

      if len(data) != nbytes:
         print '** read only %d of %d bytes from data socket'%(len(data),nbytes)
         return None

      return data
Пример #4
0
    def read_nbytes_from_data_socket(self, nbytes, flag=socket.MSG_WAITALL):
        """try to read nbytes from data socket, reporting any errors"""
        errs = 0

        # It is important to specify the list of exceptions to trap here,
        # otherwise it would catch a ctrl-c and continue after sys.exit().
        try:
            data = self.data_sock.recv(nbytes, flag)
        except (socket.error, socket.herror, socket.gaierror, socket.timeout):
            if self.verb > 0: print '** recv() exception on data socket'
            errs = 1
        if errs: return None
        if not data:
            if self.verb > 0:
                print '** failed recv() of %d bytes from data socket' % nbytes
            return None

        if self.verb > 4:
            print "++ read %d bytes from socket: %s" \
                  % (nbytes, UTIL.data_to_hex_str([ord(v) for v in data]))

        if len(data) != nbytes:
            print '** read only %d of %d bytes from data socket' % (len(data),
                                                                    nbytes)
            return None

        return data
Пример #5
0
   def peek_at_next_bytes(self, nbytes, show=0):
      """peek at and print out the next nbytes of data"""
      data = self.read_nbytes_from_data_socket(nbytes,flag=socket.MSG_PEEK)
      if not data:
         print '** failed to peek ahead'
         return

      if show or self.verb > 4:
         odata = [ord(v) for v in data]
         print '== peek ahead data: %s' % UTIL.data_to_hex_str(odata)

      return data
Пример #6
0
    def peek_at_next_bytes(self, nbytes, show=0):
        """peek at and print out the next nbytes of data"""
        data = self.read_nbytes_from_data_socket(nbytes, flag=socket.MSG_PEEK)
        if not data:
            print '** failed to peek ahead'
            return

        if show or self.verb > 4:
            odata = [ord(v) for v in data]
            print '== peek ahead data: %s' % UTIL.data_to_hex_str(odata)

        return data
Пример #7
0
   def write_4byte_data(self, data):
      """write all floats/ints to the serial port"""

      if not self.data_port: return
      if not self.data_port.isOpen(): return

      if self.verb > 4: print '++ writing data to serial port:', data

      dstring = struct.pack('f'*len(data), *data)
      if self.swap: UTIL.swap4(dstring)

      if self.verb > 5: print '++ hex data to serial port:',    \
                        UTIL.data_to_hex_str([ord(v) for v in dstring])

      self.data_port.write(dstring)

      del(dstring)

      return 0
Пример #8
0
    def write_4byte_data(self, data):
        """write all floats/ints to the serial port"""

        if not self.data_port: return
        if not self.data_port.isOpen(): return

        if self.verb > 4: print '++ writing data to serial port:', data

        dstring = struct.pack('f' * len(data), *data)
        if self.swap: UTIL.swap4(dstring)

        if self.verb > 5:            print '++ hex data to serial port:',    \
            UTIL.data_to_hex_str([ord(v) for v in dstring])

        self.data_port.write(dstring)

        del (dstring)

        return 0
Пример #9
0
   def socket_has_closed(self):
      """peek ahead for close message"""

      data = self.peek_at_next_bytes(g_magic_len)
      if not data:
         if self.verb > 0: print '** socket has gone dead, restarting...'
         return 1

      odata = [ord(v) for v in data]
      if self.verb > 3:
         print '++ testing as magic_bye: %s' % UTIL.data_to_hex_str(odata)

      # if not magic bye, return a negative
      for ind in range(g_magic_len-1):
         if odata[ind] != g_magic_bye[ind]: return 0

      if self.verb > 0: print '++ found close request for run %d, TRs = %d' \
                              % (self.nconnects, self.nread)

      return 1
Пример #10
0
    def socket_has_closed(self):
        """peek ahead for close message"""

        data = self.peek_at_next_bytes(g_magic_len)
        if not data:
            if self.verb > 0: print '** socket has gone dead, restarting...'
            return 1

        odata = [ord(v) for v in data]
        if self.verb > 3:
            print '++ testing as magic_bye: %s' % UTIL.data_to_hex_str(odata)

        # if not magic bye, return a negative
        for ind in range(g_magic_len - 1):
            if odata[ind] != g_magic_bye[ind]: return 0

        if self.verb > 0:            print '++ found close request for run %d, TRs = %d' \
                  % (self.nconnects, self.nread)

        return 1
Пример #11
0
         ilist = self.read_ints_from_socket(1)
         if ilist == None: return 1

         if ilist[0] < 0: print '** received invalid num_extra = %d' % ilist[0]
         elif self.version == 1:
            self.nextra = ilist[0]
         else: # version = 2
            self.nextra = ilist[0] * 8

         if self.verb > 2:
            print '-- num extra = %d' % self.nextra

      else:     # bad, naughty version!
         print '** HELLO string trailer is not magic, want %s but have %s' \
            % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata))
         return 1

      # todo - show_time()

      return 0

   def wait_for_socket(self):
      """wait for a talk request from the AFNI real-time plugin

         client should send magic_hi string"""
      global g_start_time

      self.data_sock, self.data_address = self.server_sock.accept()

      if self.data_sock == None:
Пример #12
0
   def read_magic_hi(self):
      """read and parse magic_hi from data socket, set version

         if version > 0, set nextra """

      # if self.verb > 3: self.peek_at_next_bytes(8)

      data = self.read_nbytes_from_data_socket(g_magic_len)
      if not data: return 1

      odata = self.bytes_to_ord(data)
      if self.verb > 2:
         print('++ recieved as magic_hi: %s' % UTIL.data_to_hex_str(odata))

      # test whether we have magic, start by ignoring the last byte
      for ind in range(g_magic_len-1):
         if odata[ind] != g_magic_hi[ind]:
            print('** HELLO string is not magic, want %s but have %s' \
               % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata)))
            return 1

      # now check the last byte for HELLO and version
      self.version = odata[g_magic_len-1] - g_magic_hi[g_magic_len-1]
      if self.verb > 2:
         print('-- hello version is %d' % self.version)

      # and deal with the version number

      if self.version == 0: pass        # we're good to go

      elif self.version >= 1 and self.version <= 3:
         # ------------------------------------------------------------
         # read the next 4-byte int to determine the number of extra data
         # values received each TR
         #    version 1: receive num_extra int over socket
         #    version 2: receive num_voxels (for 8 vals each) int over socket

         ilist = self.read_ints_from_socket(1)
         if ilist is None: return 1

         if ilist[0] < 0: print('** received invalid num_extra = %d' % ilist[0])
         elif self.version == 1:
            self.nextra = ilist[0]
         elif self.version == 2:
            self.nextra = ilist[0] * 8
         else: # version = 2
            self.nextra = ilist[0]

         if self.verb > 2:
            print('-- num extra = %d' % self.nextra)

      elif self.version == 4:
         ilist = self.read_ints_from_socket(2)
         if ilist is None: return 1
         if len(ilist) < 2:
            print('** HELLO version 4: could not read 2 ints from socket')
            return 1

         if ilist[0] < 0: print('** received invalid num_extra = %d' % ilist[0])
         if ilist[1] < 0: print('** received invalid num_ones = %d' % ilist[1])
         self.nextra  = ilist[0]     # number of ROI means
         self.nextra2 = ilist[1]     # number of mask==1 voxel values

         if self.verb > 2:
            print('-- num extra = %d, nextra2 = %d' \
                  % (self.nextra, self.nextra2))

      else:     # bad, naughty version!
         print('** HELLO string trailer is not magic, want %s but have %s' \
            % (UTIL.data_to_hex_str(g_magic_hi),UTIL.data_to_hex_str(odata)))
         return 1

      # todo - show_time()

      return 0