示例#1
0
 def __init__(self, address, port, known_polyps=[]):
     self.known = known_polyps
     self.endpoint = Endpoint(address, port)
     self.messager = polyp_server.MessageHandler()
     self.server = polyp_server.PolypServer(address,
                                            port,
                                            self.messager,
                                            seeds=[],
                                            background=True,
                                            server=False)
     self.polyp_pool = polyp_client.PolypPool(self.endpoint)
示例#2
0
 def send_random(self,endpoints,message,msg_verb):
   if len(endpoints) == 1:
     return
   chosen = random.choice(endpoints)
   if chosen == self.local_key:
     eps = endpoints[:]
     eps.remove(self.local_key)
     chosen = random.choice(eps)
   print >>sys.stderr, 'sending syn to ', chosen
   contact = Endpoint(chosen[0],chosen[1])
   self.send(contact, msg_verb, message)
示例#3
0
 def get_state(self, args):
     if len(args) == 0:
         return "Expected: <polyp index> as arguments"
     idx = int(args.strip())
     if idx >= len(self.known):
         self.show_polyps("")
         return "idx is out of range"
     msg = GetStateMessage(
         [Endpoint(host, port) for host, port in self.known])
     client = self._get_remote_client(self.known[idx][0],
                                      self.known[idx][1])
     client.send_obj("gossip", "getstate", msg)
示例#4
0
def main(args):
  p = optparse.OptionParser("Add arbitraty state to a server")
  p.add_option("--server","-s",help="Server host (localhost)",
    default="localhost")
  p.add_option("--port","-p",help="Server port (9999)",
    type="int",default=9999)
  p.add_option("--key","-k",help="Key for state value to change (REQUIRED)",
    type="string",default=None)
  p.add_option("--value","-v",help="string value of state (REQUIRED)",
    type="string",default=None)
  opts,args = p.parse_args(args)
  if opts.key is None or opts.value is None:
    print "Please specify a state key  and value"
    p.print_help()
    sys.exit(1)
  msg = NewStateMessage(opts.key,opts.value)
  print 'Connecting to %s:%d' % (opts.server,opts.port)
  client_pool = PolypPool(Endpoint("client",0000))
  client = client_pool.get(Endpoint(opts.server,opts.port))
  client.send_obj("gossip","newstate",msg)
  print ""
  print "Message Sent"
示例#5
0
def main(args):
    p = optparse.OptionParser()
    p.add_option("--server", "-s", help="Server host", default="localhost")
    p.add_option("--port", "-p", help="Server port", type="int", default=9999)

    opts, args = p.parse_args(args)
    messages = args
    print 'Connecting to %s:%d' % (opts.server, opts.port)
    client_pool = PolypPool(Endpoint("localhost", 0000))
    client = client_pool.get(Endpoint(opts.server, opts.port))
    if len(messages) == 0:
        print 'Reading messages from stdin one per line'
        messages = sys.stdin
    cnt = 0
    for line in messages:
        dumbHeader = Header(str(cnt), Endpoint("", 0000), "", line.strip())
        client.send_obj("echo", "", dumbHeader)
        cnt += 1
        sys.stdout.write('.')
        if cnt % 50 == 0:
            sys.stdout.write("%d\n" % cnt)
    print ""
    print "Sent %d messages" % cnt
示例#6
0
 def __init__(self,sending_service,local,generation,seeds=[],delay=4.0,auto_synch=True):
   self.generation = generation
   self.local = local
   self.local_key = ep_pair(local)
   self.version_counter = 1
   self.heartbeat = HeartBeatState(generation,self.version_counter)
   self.state = EndpointState(self.local,self.heartbeat,{})
   self.endpoints = {}
   self.endpoints[ep_pair(self.local)] = self.state
   self.unreachable = []
   self.quarantine = {}#{ep_pair(Endpoint) : time.time() of rejoin}
   for seed in seeds:
     if seed == self.local_key:
       continue
     self.endpoints[seed] = \
       EndpointState(Endpoint(seed[0],seed[1]),HeartBeatState(FIRST_GEN,0),{})
   self.living = []
   self.outbound = sending_service
   if auto_synch:
     self.synThread = SynThread(self,delay)
示例#7
0
  def __init__(self, address, port, messager,
  				seeds=[],
				background=False,
				server=True):
    endpoint = Endpoint(address,port)
    gossiper = GossipHandler(PolypPool(endpoint),
					endpoint,
					0,
					seeds,
					auto_synch=server)
    messager.registerHandler('gossip',gossiper)
    messager.registerHandler('echo',EchoHandler)
    processor = Daemon.Processor(messager)
    transport = MyTServerSocket(address,port)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
    def server_thread():
      try:
        server.serve()
      except KeyboardInterrupt, e:
        print "Exiting..."
        sys.exit(0)
示例#8
0
 def _get_remote_client(self, host, port):
     return self.polyp_pool.get(Endpoint(host, port))