Ejemplo n.º 1
0
def init_server(id):
    global RL
    try:
        ip, port = RL[id]
    except IndexError as E:
        quit("%s Ran out of replica list. No more server config to try" % E)
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    # host = socket.gethostname()
    host = "0.0.0.0"
    s.bind((host, port))  # on EC2 we cannot directly bind on public addr
    s.listen(50)
    s.setblocking(0)
    _logger.debug("Server [%s] -- listening on port %s" % (id, port))
    _logger.debug("IP: %s" % ip)
    return s
Ejemplo n.º 2
0
 def try_client(self, from_server_id):
     ip, port = client_address
     _logger.debug("--> Trying client %s from server ID: %s " % \
                   (client_address, from_server_id))
     while True:
         # time.sleep(5)
         self.clientlock.acquire()
         if len(self.clientbuff) > 0:
             try:
                 print("trying client again...", ip, port)
                 client_sock.connect((ip, port))
                 client_sock.send(self.clientbuff)
                 client_sock.close()
                 self.clientbuff = bytes()
             except:
                 pass
         self.clientlock.release()
Ejemplo n.º 3
0
 def init_replica_map(self, socket_obj):
     self.fdmap[socket_obj.fileno()] = socket_obj
     self.p.register(socket_obj, recv_mask)
     self.replica_map[self.id] = socket_obj
     #self.replica_map = {}
     #for i in range(self.id+1, self.N)[::-1]:
     for i in range(self.N):
         if i == self.id:
             continue
         # r = socket.socket()
         # import pdb; pdb.set_trace()
         remote_ip, remote_port = RL[i]
         _logger.debug("trying to connect to replica list -- %s" % RL)
         retry = True
         # retry = False
         count = 0
         while retry:  # re-trying will not cause a deadlock.
             count += 1
             try:
                 r = socks.socksocket()
                 # import pdb; pdb.set_trace()
                 # r.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", config.TOR_SOCKSPORT[self.id], True)
                 # r.setblocking(0)
                 r.connect((remote_ip, remote_port))
                 _logger.info("CONNECTED from %s to remote %s:%s" % (self.id, remote_ip, remote_port))
             except Exception as e:
                 time.sleep(0.5)
                 r.close()
                 _logger.debug("trying to connect [%s] to %s : %d, caused by %s" %
                       (count, remote_ip, remote_port, str(e)))
                 #if count == 10000:
                  #   raise
                 continue
             retry = False
        #r.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
         self.replica_map[i] = r
         self.fdmap[r.fileno()] = r
         self.buffmap[r.fileno()] = bytes()
         self.outbuffmap[r.fileno()] = bytes()
         self.p.register(r, recv_mask)
         #m = message.add_sig(K, "INIT " + str(ID), ID)
         #m = add_sig(0,"INIT",str(ID))
         #r.send(m.SerializeToString())
         m = self.create_request("INIT", 0, str(self.id))
         self.safe_send(r, m)
         print("init connection to replica " + str(i) + " on fd " + str(r.fileno()))
Ejemplo n.º 4
0
def check(key, req):
    id = req.inner.id
    #key = sig.get_signing_key(id)

    digest_recv = req.dig
    sig_recv = req.sig
    msg = req.inner.msg

    i = req.inner.SerializeToString()
    h = SHA256.new()
    h.update(i)
    digest = h.digest()
    _logger.debug("check(%s) against (%s)" % (key, req))
    s = (sig.verify_proto_key(key, sig_recv, digest_recv)
         and digest == digest_recv)
    if s:
        return req
    else:
        return None
    return req
Ejemplo n.º 5
0
    def server_loop(self):
        """
        call flow graph:

        -> server_loop() -> parse_request() ->
        self.request_types[req.inner.type]() -> [] process_client_request() ->
        execute_in_order() -> execute() ->
            - self.bank.process_request()
            - client_sock.send()
            - record()
        -> suicide() when Max Requests reached..
        """
        counter = 0

        # self.fdmap[s.fileno] = s
        # self.p.register(s, recv_mask)
        s = self.replica_map[self.id]
        _logger.debug("\n%s INIT SERVER LOOP [ID: %s] %s" % ('-'*20, self.id, '-'*20))
        t = Timer(5, self.try_client, args=[self.id])
        t.start()
        while True:
            #print(counter)
            events = self.p.poll()
            _logger.debug("Polling events queue -> %s" % events)
            #cstart = time.time()

            # import pdb; pdb.set_trace()
            for fd, event in events:
                counter += 1
                # need the flag for "Service temporarilly unavailable" exception
                data = None
                recv_flag = False
                if fd is s.fileno():
                    c, addr = s.accept()
                    _logger.debug("Got connection from [%s:%s]" % addr)
                    #print("Got connection from " + str(addr))
                    c.setblocking(0)
                    #c.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
                    self.p.register(c, recv_mask)
                    self.fdmap[c.fileno()] = c
                    self.buffmap[c.fileno()] = bytes()
                    self.outbuffmap[c.fileno()] = bytes()
                    self.connections += 1
                else:
                    # if we have a write event
                    if event & send_mask != 0:
                        if len(self.outbuffmap[fd]) > 0:
                            try:
                                rc = self.fdmap[fd].send(self.outbuffmap[fd])
                                self.outbuffmap[fd] = self.outbuffmap[fd][rc:]
                                if len(self.outbuffmap[fd]) == 0:
                                    self.p.modify(fd, recv_mask)
                            except:
                                #raise
                                self.clean(fd)
                            continue

                    if event & recv_mask != 0:
                        try:
                            data = self.fdmap[fd].recv(BUF_SIZE)
                            recv_flag = True
                        except Exception as E:
                            _logger.debug(E)
                            self.clean(fd)
                            continue
                        #except socket.error, serr:
                        #print("exception...")
                        #print(serr)
                        #self.clean(fd)
                    if not data and recv_flag:
                        try:
                            peer_address = ":".join(str(i) for i in self.fdmap[fd].getpeername())
                            _logger.debug("Closing connection from: event fd [%s] - ID [%s] - Address %s" % (fd, self.id, peer_address))
                        except Exception as E:
                            _logger.debug("Closing connection %s error - %s" % (fd, E))
                        self.clean(fd)
                    elif recv_flag:
                        _logger.debug("Chunk Length: %s" % len(data))  # .decode('latin-1')))
                        self.buffmap[fd] += data  # .decode('latin-1')
                        while(len(self.buffmap[fd]) > 3):
                            try:
                                size = struct.unpack("!I", self.buffmap[fd][:4])[0]
                            except Exception as E:
                                _logger.debug(E)
                                break
                                # import pdb; pdb.set_trace()

                            if len(self.buffmap[fd]) >= size+4:
                                self.parse_request(self.buffmap[fd][4:size+4], fd)
                                if fd not in self.buffmap:
                                    break
                                self.buffmap[fd] = self.buffmap[fd][size+4:]
                            else:
                                # TODO: check if remaining buffmap of slice
                                # less than size+4 as leftover crumbs
                                break
            if self.kill_flag:
                sys.exit()