def test_send_and_receive_message(self): p = BasicProtocol() p.transport = Transport() session_factory = SessionFactory(ASession) p.set_session_factory(session_factory) self.assertFalse(p.send_message("123")) msg = MessageHello() self.assertFalse(p.send_message(msg)) p.connectionMade() self.assertTrue(p.send_message(msg)) self.assertEqual(len(p.transport.buff), 1) p.dataReceived(p.transport.buff[0]) self.assertIsInstance(p.session.msgs[0], MessageHello) self.assertEquals(msg.timestamp, p.session.msgs[0].timestamp) time.sleep(1) msg = MessageHello() self.assertNotEquals(msg.timestamp, p.session.msgs[0].timestamp) self.assertTrue(p.send_message(msg)) self.assertEqual(len(p.transport.buff), 2) db = DataBuffer() db.append_string(p.transport.buff[1]) m = Message.deserialize(db)[0] self.assertEqual(m.timestamp, msg.timestamp) p.connectionLost() self.assertNotIn('session', p.__dict__)
def __init__(self, config_desc, keys_auth, client, use_ipv6=False): self.client = client self.keys_auth = keys_auth self.resources_to_send = [] self.resources_to_get = [] self.res_send_it = 0 self.peers_it = 0 self.dir_manager = DirManager(client.datadir) self.resource_manager = DistributedResourceManager( self.dir_manager.get_resource_dir()) self.use_ipv6 = use_ipv6 network = TCPNetwork( ProtocolFactory(FilesProtocol, self, SessionFactory(ResourceSession)), use_ipv6) PendingConnectionsServer.__init__(self, config_desc, network) self.resource_peers = {} self.waiting_tasks = {} self.waiting_tasks_to_compute = {} self.waiting_resources = {} self.last_get_resource_peers_time = time.time() self.get_resource_peers_interval = 5.0 self.sessions = [] self.last_message_time_threshold = config_desc.resource_session_timeout
def test_send_and_receive_message(self): p = BasicProtocol() p.transport = Transport() session_factory = SessionFactory(ASession) p.set_session_factory(session_factory) self.assertFalse(p.send_message("123")) msg = message.base.Hello() self.assertFalse(p.send_message(msg)) p.connectionMade() self.assertTrue(p.send_message(msg)) self.assertEqual(len(p.transport.buff), 1) p.dataReceived(p.transport.buff[0]) self.assertIsInstance(p.session.msgs[0], message.base.Hello) self.assertEqual(msg.timestamp, p.session.msgs[0].timestamp) time.sleep(1) msg = message.base.Hello() self.assertNotEqual(msg.timestamp, p.session.msgs[0].timestamp) self.assertTrue(p.send_message(msg)) self.assertEqual(len(p.transport.buff), 2) db = p.db db.append_bytes(p.transport.buff[1]) m = p._data_to_messages()[0] self.assertEqual(m.timestamp, msg.timestamp) p.connectionLost() self.assertNotIn('session', p.__dict__)
def test_connection_made(self): p = ServerProtocol(Server()) session_factory = SessionFactory(ASession) p.set_session_factory(session_factory) p.connectionMade() self.assertEquals(len(p.server.sessions), 1) p.connectionLost() self.assertNotIn('session', p.__dict__)
def setUp(self): logging.basicConfig(level=logging.DEBUG) self.listen_success = None self.connect_success = None self.stop_listening_success = None self.port = None session_factory = SessionFactory(ASession) protocol_factory = ProtocolFactory(SafeProtocol, Server(), session_factory) self.network = TCPNetwork(protocol_factory)
def __init__(self, node, config_desc, keys_auth, client, use_ipv6=False, use_docker_machine_manager=True): self.client = client self.keys_auth = keys_auth self.config_desc = config_desc self.node = node self.task_keeper = TaskHeaderKeeper(client.environments_manager, min_price=config_desc.min_price) self.task_manager = TaskManager( config_desc.node_name, self.node, self.keys_auth, root_path=TaskServer.__get_task_manager_root(client.datadir), use_distributed_resources=config_desc. use_distributed_resource_management, tasks_dir=os.path.join(client.datadir, 'tasks')) self.task_computer = TaskComputer( config_desc.node_name, task_server=self, use_docker_machine_manager=use_docker_machine_manager) self.task_connections_helper = TaskConnectionsHelper() self.task_connections_helper.task_server = self self.task_sessions = {} self.task_sessions_incoming = WeakList() self.max_trust = 1.0 self.min_trust = 0.0 self.last_messages = [] self.last_message_time_threshold = config_desc.task_session_timeout self.results_to_send = {} self.failures_to_send = {} self.use_ipv6 = use_ipv6 self.forwarded_session_request_timeout = config_desc.waiting_for_task_session_timeout self.forwarded_session_requests = {} self.response_list = {} self.deny_set = get_deny_set(datadir=client.datadir) network = TCPNetwork( ProtocolFactory(MidAndFilesProtocol, self, SessionFactory(TaskSession)), use_ipv6) PendingConnectionsServer.__init__(self, config_desc, network)
def test_send_and_receive_message(self): p = SafeProtocol(Server()) p.transport = Transport() session_factory = SessionFactory(ASession) p.set_session_factory(session_factory) self.assertFalse(p.send_message("123")) msg = message.base.Hello() self.assertIsNone(msg.sig) self.assertFalse(p.send_message(msg)) p.connectionMade() self.assertTrue(p.send_message(msg)) self.assertEqual(len(p.transport.buff), 1) p.dataReceived(p.transport.buff[0]) self.assertIsInstance(p.session.msgs[0], message.base.Hello) p.connectionLost() self.assertNotIn('session', p.__dict__)
def test_connection_made(self): prt = [ BasicProtocol(), ServerProtocol(Server()), SafeProtocol(Server()) ] for p in prt: p.transport = Transport() session_factory = SessionFactory(ASession) p.set_session_factory(session_factory) self.assertFalse(p.opened) p.connectionMade() self.assertTrue(p.opened) self.assertFalse(p.session.dropped_called) p.connectionLost() self.assertFalse(p.opened) self.assertNotIn('session', p.__dict__)
def __init__(self, node, config_desc: ClientConfigDescriptor, client, use_ipv6=False, use_docker_manager=True, task_archiver=None, apps_manager=AppsManager(), task_finished_cb=None) -> None: self.client = client self.keys_auth = client.keys_auth self.config_desc = config_desc self.node = node self.task_archiver = task_archiver self.task_keeper = TaskHeaderKeeper( environments_manager=client.environments_manager, node=self.node, min_price=config_desc.min_price, task_archiver=task_archiver) self.task_manager = TaskManager( config_desc.node_name, self.node, self.keys_auth, root_path=TaskServer.__get_task_manager_root(client.datadir), use_distributed_resources=config_desc. use_distributed_resource_management, tasks_dir=os.path.join(client.datadir, 'tasks'), apps_manager=apps_manager, finished_cb=task_finished_cb, ) benchmarks = self.task_manager.apps_manager.get_benchmarks() self.benchmark_manager = BenchmarkManager( node_name=config_desc.node_name, task_server=self, root_path=self.get_task_computer_root(), benchmarks=benchmarks) self.task_computer = TaskComputer( task_server=self, use_docker_manager=use_docker_manager, finished_cb=task_finished_cb) self.task_connections_helper = TaskConnectionsHelper() self.task_connections_helper.task_server = self self.task_sessions = {} self.task_sessions_incoming = weakref.WeakSet() self.max_trust = 1.0 self.min_trust = 0.0 self.last_messages = [] self.last_message_time_threshold = config_desc.task_session_timeout self.results_to_send = {} self.failures_to_send = {} self.use_ipv6 = use_ipv6 self.forwarded_session_request_timeout = \ config_desc.waiting_for_task_session_timeout self.forwarded_session_requests = {} self.response_list = {} self.acl = get_acl(Path(client.datadir)) self.resource_handshakes = {} network = TCPNetwork( ProtocolFactory(SafeProtocol, self, SessionFactory(TaskSession)), use_ipv6) PendingConnectionsServer.__init__(self, config_desc, network) # instantiate ReceivedMessageHandler connected to self # to register in golem.network.concent.handlers_library from golem.network.concent import \ received_handler as concent_received_handler self.concent_handler = \ concent_received_handler.TaskServerMessageHandler(self) dispatcher.connect(self.income_listener, signal='golem.income') dispatcher.connect(self.finished_task_listener, signal='golem.taskmanager')
def __init__(self, node, config_desc, keys_auth, connect_to_known_hosts=True): """Create new P2P Server. Listen on port for connections and connect to other peers. Keeps up-to-date list of peers information and optimal number of open connections. :param Node node: Information about this node :param ClientConfigDescriptor config_desc: configuration options :param KeysAuth keys_auth: authorization manager """ network = tcpnetwork.TCPNetwork(ProtocolFactory( tcpnetwork.SafeProtocol, self, SessionFactory(PeerSession)), config_desc.use_ipv6, limit_connection_rate=True) tcpserver.PendingConnectionsServer.__init__(self, config_desc, network) self.node = node self.keys_auth = keys_auth self.peer_keeper = PeerKeeper(keys_auth.key_id) self.task_server = None self.resource_server = None self.metadata_manager = None self.resource_port = 0 self.suggested_address = {} self.suggested_conn_reverse = {} self.gossip_keeper = GossipManager() self.manager_session = None self.metadata_providers: Dict[str, Callable[[], Any]] = {} # Useful config options self.node_name = self.config_desc.node_name self.last_message_time_threshold = self.config_desc.p2p_session_timeout self.last_message_buffer_len = LAST_MESSAGE_BUFFER_LEN self.last_time_tried_connect_with_seed = 0 self.reconnect_with_seed_threshold = RECONNECT_WITH_SEED_THRESHOLD self.should_solve_challenge = SOLVE_CHALLENGE self.challenge_history = deque(maxlen=HISTORY_LEN) self.last_challenge = "" self.base_difficulty = BASE_DIFFICULTY self.connect_to_known_hosts = connect_to_known_hosts self.key_difficulty = config_desc.key_difficulty # Peers options self.peers = {} # active peers self.peer_order = [] # peer connection order self.incoming_peers = {} # known peers with connections self.free_peers = [] # peers to which we're not connected self.seeds = set() self.used_seeds = set() self.bootstrap_seeds = P2P_SEEDS self._peer_lock = Lock() try: self.__remove_redundant_hosts_from_db() self._sync_seeds() except Exception as exc: logger.error("Error reading seed addresses: {}".format(exc)) # Timers now = time.time() self.last_peers_request = now self.last_tasks_request = now self.last_refresh_peers = now self.last_forward_request = now self.last_random_disconnect = now self.last_seeds_sync = time.time() self.last_messages = [] random.seed()