indexing description: "Core version of the origo message service." author: "Patrick Ruckstuhl " date: "$Date$" revision: "$Revision$" class P2P_MODULE_CORE inherit O_P2P_MODULE redefine process_discovery_response, process_rendezvous_event, init end create init feature {NONE} -- Initialization init (a_group: like peer_group; an_id: like module_id; an_advertisement: like implementation_advertisement) -- Initialize module do Precursor {O_P2P_MODULE}(a_group, an_id, an_advertisement) create peers.make (10) create peers_lock.make end feature -- Access node_list: LIST [STRING] is -- List of known nodes. do peers_lock.acquire_read_lock create {ARRAYED_LIST [STRING]}Result.make_from_array (peers.current_keys) peers_lock.release_reader_lock end feature -- Basic operation send_message_node (a_message: O_MESSAGE; a_node: STRING): BOOLEAN is -- Send a_message to a_node, return true on success. require a_message_ok: a_message /= Void a_node_ok: a_node /= Void and then not a_node.is_empty local l_peer: P2P_PEER_ID do -- if we have the node, send message peers_lock.acquire_read_lock l_peer := peers.item (a_node) peers_lock.release_reader_lock if l_peer /= Void then Result := send_message_peer (a_message, l_peer) -- if didn't succeed, remove the node if not Result then peers_lock.acquire_write_lock peers.remove (a_node) peers_lock.release_writer_lock end end end feature {NONE} -- Message transport process_discovery_response (a_response: P2P_DISCOVERY_RESPONSE) is -- Process discovery response local l_adv: P2P_PEER_ADVERTISEMENT do l_adv := a_response.peer_advertisement if l_adv /= Void then -- store/update peer peers_lock.acquire_write_lock peers.force (l_adv.peer_id, l_adv.name) peers_lock.release_writer_lock -- publish peer adv locally discovery.publish_advertisement_locally (l_adv) origo_logger.info ("Received peer discovery " + l_adv.peer_id.out + " from " + l_adv.name) end end process_rendezvous_event (an_event: P2P_RENDEZVOUS_EVENT) is -- Publish this node as soon as we're rdv or have rdv connection local l_adv: P2P_PEER_ADVERTISEMENT do if an_event.type = {P2P_RENDEZVOUS_EVENT}.type_connected_to_rendezvous or an_event.type = {P2P_RENDEZVOUS_EVENT}.type_became_rendezvous or an_event.type = {P2P_RENDEZVOUS_EVENT}.type_reconnected_to_rendezvous then -- send a peer adv to the rdv server l_adv := peer_group.peer_advertisement -- lasts for 30 minutes l_adv.set_expiration_time (30*60*1000) discovery.publish_advertisement_remotely (l_adv, an_event.peer_id) origo_logger.info ("Received rendezvous from "+an_event.peer_id.out) end end feature {NONE} -- Implementation peers: HASH_TABLE [P2P_PEER_ID, STRING] -- Table of known peers. peers_lock: READ_WRITE_LOCK invariant peers_not_void: peers /= Void peers_lock_not_void: peers_lock /= Void end