Exemple #1
0
  def read (self):
    """
      reads blockingly from the socket and parses the data into a list
      returns: args
        if args==None, nothing read, socket's dead or closed or something.
        else, args is a list with whatever came down the line.
        if that list is empty, then the other side closed.
    """
    args= None
    try:
      # self.log ('[%d]<-- loading...' % self._socket.fileno ())
      size= self.readData (8)
      if len (size)==8:
        size= int(size)
        line= self.readData (size)
        # self.log ('[%d]<-- read' % self.fileno ())
        args= csvParse (line)
      else:
        # EOF
        args= []

      # self.log ("[%d]<-- %s" % (self._socket.fileno (), args))
      self.debug (1, "[%d]<-- %s" % (self.fileno (), args))
    except Exception, e:
      # self.debug (5, 'exception %s' % e)
      # self.debug (5, '[%d] dead!' % (self._socket.fileno ()))
      raise UmbDead
Exemple #2
0
 def dirContents(self, ino, contents=None):
     if not contents == None:
         self.data["%d" % ino] = csvPrettyPrint(contents)
         ans = contents
     else:
         ans = csvParse(self.data["%d" % ino])
     return ans
Exemple #3
0
 def dirContents (self, ino, contents=None):
   if not contents==None:
     self.data["%d" % ino]= csvPrettyPrint (contents)
     ans= contents
   else:
     ans= csvParse (self.data["%d" % ino])
   return ans
Exemple #4
0
  def discover (self, net, skip=None):
    """
      broadcasts messages for discovering the peers
      returns: the peer to which we should connect to.
    """
    shouter= socket (AF_INET, SOCK_DGRAM)
    # set socket option
    shouter.setsockopt (SOL_SOCKET, SO_BROADCAST, 1)
    server= None

    # we need to be root and a default gw here!
    # shouter.sendto ('any body there?', ('<broadcast>', consts.chalPort))
    # no if we broadcast the net only...
    self.debug (1, 'Ping %s' % net)
    shouter.sendto ('any body there?', (net, consts.chalPort))
    # wait for answers
    sleep (consts.shoutTimeOut)
    # now wait for at least one

    # but let things go on in the case the shout was not heard
    # nope, navels that doesn't find others should start their own ring

    # but vices should wait till they find one,
    # so navels and vices do different things

    i= select ([shouter],[],[], 0.1)[0]
    # collect *any* answer
    while len(i)>0:
      ans= csvParse (shouter.recv (1024))
      self.debug (1, 'found %s' % ans)
      (url, key)= ans[0]
      new= self._peers.getNavel (url, key)
      if skip:
        if new!=skip:
          server= new
      else:
        server= new
      i= select ([shouter],[],[], 0.1)[0]

    self.debug (1, 'returning %s' % server)
    return server
Exemple #5
0
  def readPoll (self):
    """
      Tries to read a line from the file.
      If it timeouts, returns None.
      If it fails (EOF) return [].
      Otherwise, returns the data read once eval()'ed.
    """
    # self.debug (1, '[%d]<-- reading...' % self.fileno (), 2)
    args= None
    try:
      i= select ([self.fileno ()], [], [], 5)[0]
      # self.debug (1, '[%d]<-- selected...' % self.fileno (), 3)
      if len (i)>0:
        # self.debug (5, '[%d]<-- loading...' % self.fileno (), 4)
        # self.log ('[%d]<-- reading...' % self.fileno ())

        size= self.readData (8)
        # self.debug (2, 'about to read >%s< bytes' % size)
        size= int(size)
        line= self.readData (size)

        # line= self._file.readline ()
        # line= chomp (line)
        # self.log ('[%d]<-- read' % self.fileno ())
        if line:
          args= csvParse (line)
        else:
          # EOF
          args= []

        # using pickles (blej!)
        # args= load (self._file)
        # args= load (self._socket)

        # self.log ("[%d]<-- %s" % (self.fileno (), args))
        self.debug (1, "[%d]<-- %s" % (self.fileno (), args))
      else:
        # self.debug (2, 'timeout!')
        pass
    except Exception, e:
      raise UmbDead