Exemple #1
0
    def get_signature(self, ith_witness, request, reply_queue):
        request.witnessCount = ith_witness
        serialized_request = request.SerializeToString()
        witness = self.select_witness(request.transaction, ith_witness)

        socket = gevent.socket.create_connection(self.addressList[witness])
        send_msg(socket, b'\x10' + serialized_request)
        try:
            if select.select([socket], [], [], 1)[0]:
                data = recv_msg(socket)
                if not data:
                    socket.close()
                else:

                    if data[:1] == b'\x11':
                        result = data[1:]
                        reply_queue.put(result)
                    socket.shutdown(gevent.socket.SHUT_RDWR)
            else:
                # self.log("signature collection handler timed out")
                socket.close()
                reply_queue.put(None)

        except (ConnectionAbortedError, ConnectionResetError) as e:
            reply_queue.put(None)
Exemple #2
0
 def message_handler(self, socket, addres):
     while True:
         inputready, outputready, exceptready = select.select([socket], [],
                                                              [], 0.1)
         if inputready:
             data = recv_msg(socket)
             if not data:
                 socket.close()
                 break
             if data[:1] == b"\xf0":
                 socket.shutdown(gevent.socket.SHUT_WR)
                 received_node = trustchain_pb2.Node()
                 received_node.ParseFromString(data[1:])
                 if addres[0] == "127.0.0.1":
                     received_node.IP = self.myIP
                 else:
                     received_node.IP = addres[0]
                 self.nodeList.nodes.add().CopyFrom(received_node)
                 print("{} connected from {}".format(
                     received_node.friendlyName, received_node.IP))
         else:
             print("Tiny master timeout, but that's oke ;)")
             socket.close()
             break
     self.send_peerlist()
Exemple #3
0
 def send_to_master(self, payload):
     socket = gevent.socket.create_connection(self.master_address)
     send_msg(socket, b'\xf1' + payload.encode('utf-8'))
     if select.select([socket], [], [], 1)[0]:
         data = recv_msg(socket)
         socket.shutdown(gevent.socket.SHUT_RDWR)
         if not data:
             socket.close()
             self.state = 0
             return
Exemple #4
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            ds = self.context.get_data_source(name)
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemple #5
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            ds = self.context.get_data_source(name)
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemple #6
0
    def handle(self, socket, address):
        fd = socket.makefile()
        while True:
            line = fd.readline()
            if not line:
                break

            name, value, timestamp = line.split()
            _, _, datapoint = name.partition('.')
            ds = self.context.get_data_source(
                'localhost.{0}'.format(datapoint))
            ds.submit(int(timestamp), float(value))

        socket.shutdown(gevent.socket.SHUT_RDWR)
        socket.close()
Exemple #7
0
def test_drop_node_on_disconnect():
    node_name = 'node'
    cm = ClusterManagerMock()
    cm.is_leader = True
    with cluster.PeerServer(cm, ('127.0.0.1', port)) as s:
        socket = gevent.socket.create_connection(('127.0.0.1', port))
        sockfile = socket.makefile()
        line = sockfile.readline() # cluster list pre-join
        socket.send("%s\n" % node_name)
        line = sockfile.readline() # cluster list post-join
        assert node_name in cm.cluster, \
            "Leader did not add host to cluster: %s" % line
        socket.shutdown(0)
        # Make sure to cleanup file descriptors:
        del sockfile
        del socket
        yield_(2) # Yield to let PeerServer catch the disconnect
        assert not node_name in cm.cluster, \
            "Leader did not remove host from cluster."
 def handle(self, socket, address):
     socket.sendall("hello and goodbye!")
     socket.shutdown(0)
Exemple #9
0
 def handle(self, socket, address):
     socket.sendall(''.join([one_line for n in xrange(number_lines)]))
     socket.sendall('\n')
     socket.shutdown(0)
Exemple #10
0
 def handle(self, socket, address):
     socket.sendall(one_line)
     socket.shutdown(0)
 def handle(self, socket, address):
     socket.sendall(one_line)
     socket.shutdown(0)
 def handle(self, socket, address):
     socket.sendall(''.join([one_line for n in xrange(number_lines)]))
     socket.sendall('\n')
     socket.shutdown(0)
Exemple #13
0
    def command_server(self, socket, address):
        self.log('New connection from %s:%s' % address)
        socket.sendall('Welcome to the Democratic Daemon server!\n')
        help_message = 'Commands are "stop", "list", "json" and "approve"\n'
        socket.sendall(help_message)
        fileobj = socket.makefile()
        while True:
            try:
                line = fileobj.readline()
                if not line:
                    self.log("client disconnected")
                    return

                command = line.decode().strip().lower()
                single_cmd = False
                if command.startswith('$'):
                    command = command[1:]
                    single_cmd = True
                    self.log('Received single command ' + repr(command))

                if command == 'stop':
                    self.log("client told server to stop")
                    fileobj.write(('STOPPING SERVER\n').encode())
                    fileobj.flush()
                    self.quit_event.set()
                    self.server.stop()
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return

                elif command == 'list':
                    empty = True
                    for repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            fileobj.write(pr.pretty_str().encode())
                            empty = False
                    if empty:
                        fileobj.write('No pull requests\n'.encode())

                elif command == 'json':
                    data = prs_to_json(self.pr_db.pull_requests()).encode()
                    fileobj.write(data)

                elif command.startswith('approve'):
                    r = re.match(
                        'approve\s+(?P<repo>\S+)\s+(?P<issue_id>\d+)\s*$',
                        command)
                    issue_id = None
                    repo = None
                    if r:
                        repo = r.group('repo')
                        try:
                            issue_id = int(r.group('issue_id'))
                        except Exception as e:
                            fileobj.write(
                                ('error parsing integer issue number\n' +
                                 str(e) + '\n').encode())
                    else:
                        fileobj.write(
                            ('error - usage is "approve [repo] [issue_id]"\n'
                             ).encode())

                    found_pr = None
                    if issue_id and repo and repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            if pr.state == pr.state_idx('COMMENTED'):
                                if pr.issue_id == issue_id:
                                    found_pr = pr
                                    break

                    if found_pr:
                        fileobj.write(('PULL REQUEST APPROVED\n').encode())
                        fileobj.write(found_pr.pretty_str().encode())

                        found_pr.set_state('APPROVED')
                        self.pr_db.write_pull_requests(found_pr.repo)
                        self.build_queue.put((found_pr.repo, found_pr.key()))

                    else:
                        fileobj.write(
                            ('No pull request "' + str(repo) + '/issue #' +
                             str(issue_id) + '" ready for merging\n').encode())

                else:
                    fileobj.write(
                        ('Unknown command "' + command + '"\n').encode())
                    fileobj.write(help_message.encode())

                fileobj.flush()
                if single_cmd:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return

            except Exception as e:
                self.log(e)
                try:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                except Exception as e2:
                    self.log(e2)
                return
Exemple #14
0
    def start_transaction(self, peerID, Value=-1):
        logging.info("Initiated transaction with {}, value: {}".format(self.friendlyNameList[peerID], Value))
        if peerID not in self.addressList:
            logging.error("Supplied ID not know by this node.")
            raise ValueError("Supplied ID not know by this node.")

        # if self.balance < 10:
        #     return

        if not self.transaction_mutex.acquire(blocking=False):
            self.trade_refusal_count += 1
            return

        self.reset_session()

        try:
            socket = gevent.socket.create_connection(self.addressList[peerID])
        except OSError as e:

            self.trade_refusal_count += 1
            self.transaction_mutex.release()
            logging.exception("Tried to connect to {} at{}, {}\n".format(self.friendlyNameList[peerID], *self.addressList[peerID]))
            return

        self.counter_party = peerID
        self.state = 1

        trade_request = trustchain_pb2.TradeRequest()
        trade_request.publicKey = bytes(self.signing_key.verify_key)

        if Value >= 0:
            self.trade_value = Value
        else:
            self.trade_value = random.randint(1, self.balance)

        trade_request.value = self.trade_value
        trade_request.blocks.append(self.chain[self.chain[-1]])
        message = b'\x01' + trade_request.SerializeToString()
        send_msg(socket, message)
        try:
            while True:
                    if select.select([socket], [], [], 20)[0]:
                        data = recv_msg(socket)
                        if not data:
                            socket.close()
                            break
                        if data == b'\00':
                            try:
                                socket.shutdown(gevent.socket.SHUT_WR)
                            except OSError as e:
                                pass
                        else:
                            reply = self.fsm(data)
                            if not reply:
                                socket.shutdown(gevent.socket.SHUT_WR)
                            else:
                                send_msg(socket, reply)
                                if reply[:1] == b'\x05':
                                    socket.shutdown(gevent.socket.SHUT_WR)
                    else:
                        # print("Start transaction Timed out")
                        logging.error("socket timed out in 'startTranscation()")
                        socket.close()
        except (ConnectionAbortedError, ConnectionResetError) as e: # gevent.socket.error,
            if not socket.closed:
                socket.close()
            pass
            # print("Hmm we experience : {}".format(e))

        self.reset_session()
        self.transaction_mutex.release()
Exemple #15
0
    def message_handler(self, socket, address):
        if select.select([socket], [], [], 2)[0]:
            data = recv_msg(socket)
            # Not sure if this makes sense, question is, is this function called when the connection is started
            # or when data is send.
            if not data:
                socket.close()

            # Trade request
            elif data[:1] == b'\x01':
                # A new session may only begin if state == 0
                if not self.transaction_mutex.acquire(blocking=False):
                    send_msg(socket, b'\x00')
                    socket.close()
                    return
                else:
                    self.reset_session()
                    result = self.fsm(data)
                    send_msg(socket, result)

                # TODO: Understant this loop
                while True:
                    try:
                        if select.select([socket], [], [], 1)[0]:
                            data = recv_msg(socket)
                            if not data:
                                socket.close()
                                break
                            if data[:1] == b'\x00':
                                logging.info("Received nack: sending socket shutdown signal!")
                                try:
                                    socket.shutdown(gevent.socket.SHUT_RDWR)
                                except OSError as e:
                                    logging.exception("Error directly after receiving a nack\n")
                                    pass
                                break
                            result = self.fsm(data)

                            # When according to the fsm, nothing needs to be done,
                            if not result:
                                socket.shutdown(gevent.socket.SHUT_WR)
                            else:
                                send_msg(socket, result)
                        else:
                            # self.log("Message Handler Timed out")
                            logging.error("Message handler timed out")
                            socket.close()
                    except (ConnectionAbortedError, ConnectionResetError, ValueError, BrokenPipeError):
                        logging.exception("Connection error in message handler\n")
                        if not socket.closed:
                            socket.close()
                        break

                self.reset_session()
                self.transaction_mutex.release()

            # Signing request
            elif data[:1] == b"\x10":
                self.witness_count += 1
                result = b"\x11" + self.witness_transaction(data[1:])
                send_msg(socket, result)
                # ON debian it appaers that sending a shutdown mgith fail
                try:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                except OSError as e:
                    pass
            # Recieved peerlist
            elif data[:1] == b"\xf2":
                self.importPeerlist(data[1:])

            ## I think all these funtions are for benchmarking, we dont have the benchmarking code ##
            # Randomized partner start
            elif data[:1] == b"\xf3":
                logging.info("received command to start random partner simulation")
                runtime = int.from_bytes(data[1:5], byteorder="little")
                sleeptime = int.from_bytes(data[5:10], byteorder="little")
                self.start_simulation(runtime, sleeptime)
            # Fixed_parter_Start
            elif data[:1] == b"\xf4":
                logging.info("received command to start fixed partner simulation")
                runtime = int.from_bytes(data[1:5], byteorder="little")
                self.start_simulation(runtime)
            # Shutdown
            elif data[:1] == b"\xff":
                logging.info("Received shutdown message.")
                self.reset_session()
                self.workersRunning = False
                self.server.stop()

        if not socket.closed:
            socket.close()
Exemple #16
0
 def handle(self, socket, address):
     socket.sendall("hello and goodbye!")
     socket.shutdown(0)
Exemple #17
0
 def send_peerlist(self):
     serialized_nodelist = self.nodeList.SerializeToString()
     for node in self.nodeList.nodes:
         socket = gevent.socket.create_connection((node.IP, node.port))
         send_msg(socket, b'\xf2' + serialized_nodelist)
         socket.shutdown(gevent.socket.SHUT_RDWR)
Exemple #18
0
    def command_server(self, socket, address):
        self.log('New connection from %s:%s' % address)
        socket.sendall('Welcome to the Democratic Daemon server!\n')
        help_message = 'Commands are "stop", "list", "json" and "approve"\n'
        socket.sendall(help_message)
        fileobj = socket.makefile()
        while True:
            try:
                line = fileobj.readline()                    
                if not line:
                    self.log("client disconnected")
                    return
                    
                command = line.decode().strip().lower()
                single_cmd = False
                if command.startswith('$'):
                    command = command[1:]
                    single_cmd = True
                    self.log('Received single command ' + repr(command))
                    
                if command == 'stop':
                    self.log("client told server to stop")
                    fileobj.write(('STOPPING SERVER\n').encode())
                    fileobj.flush()
                    self.quit_event.set()
                    self.server.stop()
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return
                    
                elif command == 'list':
                    empty = True
                    for repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            fileobj.write(pr.pretty_str().encode())
                            empty = False
                    if empty:
                        fileobj.write('No pull requests\n'.encode())
                        
                elif command == 'json':
                    data = prs_to_json(self.pr_db.pull_requests()).encode()
                    fileobj.write(data)

                elif command.startswith('approve'):
                    r = re.match('approve\s+(?P<repo>\S+)\s+(?P<issue_id>\d+)\s*$', command)
                    issue_id = None
                    repo = None
                    if r:
                        repo = r.group('repo')
                        try:
                            issue_id = int(r.group('issue_id'))
                        except Exception as e:
                            fileobj.write(('error parsing integer issue number\n' + str(e) + '\n').encode())
                    else:
                        fileobj.write(('error - usage is "approve [repo] [issue_id]"\n').encode())
                                            
                    found_pr = None
                    if issue_id and repo and repo in self.pr_db.repos():
                        for pr in self.pr_db.pull_requests(repo):
                            if pr.state == pr.state_idx('COMMENTED'):
                                if pr.issue_id == issue_id:
                                    found_pr = pr
                                    break
                                    
                    if found_pr:
                        fileobj.write(('PULL REQUEST APPROVED\n').encode())
                        fileobj.write(found_pr.pretty_str().encode())
                        
                        found_pr.set_state('APPROVED')
                        self.pr_db.write_pull_requests(found_pr.repo)
                        self.build_queue.put((found_pr.repo, found_pr.key()))
                        
                    else:
                        fileobj.write(('No pull request "' + str(repo) + '/issue #' + str(issue_id) + '" ready for merging\n').encode())
                    
                else:
                    fileobj.write(('Unknown command "' + command + '"\n').encode())
                    fileobj.write(help_message.encode())
                    
                fileobj.flush()
                if single_cmd:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                    return
                    
            except Exception as e:
                self.log(e)
                try:
                    socket.shutdown(gevent.socket.SHUT_RDWR)
                except Exception as e2:
                    self.log(e2)
                return