indexing description: "[ This is the client side of the client-server implementation of the multiplayer framework. This is a client that has the ability to join an EM_NET_SERVER. ]" date: "$Date$" revision: "$Revision$" class EM_NET_CLIENT[TYPES -> EM_NET_OBJECT_TYPES create make_and_initialize_factory end] inherit EM_NET_BASE[TYPES] rename send_update as send_update_to_server redefine make end create make feature -- Initialisation make (a_port: INTEGER) is -- Initialize a network client. do Precursor (a_port) create join_timeout_event protocol.enable_time_sync subscribe_by_type_id (object_types.em_net_create_object_response, agent on_create_object (?)) -- Init client attributes nickname := "no nickname" end feature -- Connection handling join is -- Join to the `server'. -- Subscribe to EM_NET_JOIN_RESPONSE in advance of calling `join' to get -- notfied if the join attempt was successful. -- Subscribe to `join_timeout_event' to get notified if the server was not reachable. require server_not_void: server /= Void server_resolved: server.is_resolved local a_join_request: EM_NET_JOIN_REQUEST do check has_server_group: group_exists_by_name (server.to_hex_string) end a_join_request := object_types.create_em_net_join_request event_2pc_id_manager.set_unique_id_for_object (a_join_request) a_join_request.set_resend_time (500) a_join_request.set_resends_left (5) a_join_request.set_timeout_agent (agent on_join_timeout (?)) a_join_request.set_nick_name (nickname) publish (a_join_request) end leave is -- Leave the `server' properly. local a_leave_event: EM_NET_LEAVE_EVENT do a_leave_event := object_types.create_em_net_leave_event event_2pc_id_manager.set_unique_id_for_object (a_leave_event) a_leave_event.set_resend_time (200) a_leave_event.set_resends_left (5) publish (a_leave_event) ensure disconnected: not is_connected end feature -- Events join_timeout_event: EM_EVENT_CHANNEL[TUPLE[]] -- Event that is called if the clients join request timeouted feature -- Setters set_server_address (a_server: EM_INET_SOCKET_ADDRESS) is -- Set `server_address' to `a_server'. do if server /= Void then check connection_exists: connections.has (server.to_hex_string) end delete_connection (connections.item (server.to_hex_string)) end server := a_server create_connection (a_server) last_created_connection.join (standard_group) end set_nickname (a_nickname: STRING) is -- Set `nickname' to `a_nickname'. require a_nickname_not_void: a_nickname /= Void do nickname := a_nickname ensure nickname_set: nickname = a_nickname end feature -- Status information is_connected: BOOLEAN -- Is the client connected to `server'? nickname: STRING -- Clients nickname server: EM_INET_SOCKET_ADDRESS -- Server socket address address: EM_INET_SOCKET_ADDRESS -- Address where the current client can be reached feature {NONE} -- Event handling on_new_connection (a_socket: EM_INET_SOCKET_ADDRESS) is -- A sofar unknown peer sent us a packet. do -- do nothing, we're a client! end on_create_object (a_response: EM_NET_CREATE_OBJECT_RESPONSE) is -- Handle object creation response (ie create the object). local a_group: EM_NET_GROUP do a_group := group (a_response.create_in_group_name) a_group.add_object (a_response.object_to_create) end on_destroy_object (a_response: EM_NET_DESTROY_OBJECT_RESPONSE) is -- Handle object destruction response (ie destroy the object). local old_object: EM_NET_OBJECT a_group: EM_NET_GROUP do old_object := objects.item (a_response.destroy_id) a_group := old_object.group a_group.remove_object (old_object) end feature {NONE}-- Implementation on_join_timeout (timouted_connections: DS_LINKED_LIST[EM_NET_CONNECTION]) is -- Event that is called if the join request timeouted. do join_timeout_event.publish ([]) end end