From b326dd65564aeb14ef3656dade2370192d273ea6 Mon Sep 17 00:00:00 2001 From: David RENE Date: Thu, 26 Apr 2007 13:18:32 +0000 Subject: [PATCH] add net_services support --- calexium_lib/CXM_message_constants.anubis | 22 +++++++++++++++++++--- calexium_lib/net_services/CXM_generic_protocol.anubis | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ calexium_lib/net_services/CXM_net_services.anubis | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 calexium_lib/net_services/CXM_generic_protocol.anubis create mode 100644 calexium_lib/net_services/CXM_net_services.anubis diff --git a/calexium_lib/CXM_message_constants.anubis b/calexium_lib/CXM_message_constants.anubis index 2ed5906..ce44aa3 100644 --- a/calexium_lib/CXM_message_constants.anubis +++ b/calexium_lib/CXM_message_constants.anubis @@ -18,7 +18,23 @@ public define Int32 _CXM_PKG_S_ADD_FILE = 0x33770404. // script action to public define Int32 _CXM_PKG_INDEX_PART = 0x33770440. // ID of package Index part public define Int32 _CXM_PKG_IDX_ENTRY = 0x33770441. // Index entry information - // **************** UPDATE SERVER *************** + // **************** UPDATE SERVICE *************** -public define Int32 _CXM_UPDATE_SERVER_MSG_BASIS = 0x33770800. // -public define Int32 _CXM_UPD_HAS_UPDATE = 0x33770801. // +public define Int32 _CXM_UPDATE_SERVICE_MSG_BASIS = 0x33770800. // +public define Int32 _CXM_UPD_SERVICE_ID = 0x33770801. +public define Int32 _CXM_UPD_HAS_UPDATE = 0x33770802. // + + // **************** SERVICE NEGOCIATION *************** + +public define Int32 _CXM_SERVICE_NEGOCIATION_BASIS = 0x33770C00. // +public define Int32 _CXM_REQUEST_FOR_SERVICE = 0x33770C01. // + + // **************** GENERIC PROTOCOL *************** + +public define Int32 _CXM_GENERIC_PROTOCOL_BASIS = 0x33771000. // +public define Int32 _CXM_ACK = 0x33771001. // + + // **************** FTP SERVICE *************** + +public define Int32 _CXM_FTP_SERVICE_MSG_BASIS = 0x33771400. // +public define Int32 _CXM_FTP_SERVICE_ID = 0x33771401. diff --git a/calexium_lib/net_services/CXM_generic_protocol.anubis b/calexium_lib/net_services/CXM_generic_protocol.anubis new file mode 100644 index 0000000..8ce65de --- /dev/null +++ b/calexium_lib/net_services/CXM_generic_protocol.anubis @@ -0,0 +1,55 @@ +/* + * + * User: David RENE + * Date: 25/04/2007 + * Time: 16:20 + * (c) Calexium + * + */ + +read system/muscle.anubis +read system/data_io.anubis +read tools/basis.anubis + +read calexium_lib/CXM_message_constants.anubis + +public define Int32 _CXM_ERROR = 0. +public define Int32 _CXM_OK = 1. +public define Int32 _CXM_UNKNOW_CMD = 2. + +public define One + send_ACK_error + ( + RWStream conn, + Int32 cmd_id + )= + with err_msg = message(_CXM_ACK), + forget(add_int32(err_msg, "_CMD", cmd_id)); + forget(add_int32(err_msg, "_STATUS", _CXM_ERROR)); + forget(send_message_by_Stream(conn, err_msg)) + . + +public define One + send_ACK_ok + ( + RWStream conn, + Int32 cmd_id + )= + with ok_msg = message(_CXM_ACK), + forget(add_int32(ok_msg, "_CMD", cmd_id)); + forget(add_int32(ok_msg, "_STATUS", _CXM_OK)); + forget(send_message_by_Stream(conn, ok_msg)) + . + +public define Maybe(Message) + wait_for_reply + ( + RWStream conn + ) = + if unflatten_message(make_data_io(weaken(conn))) is + { + failure then + sleep(1); + wait_for_reply(conn), + success(msg) then success(msg) + }. diff --git a/calexium_lib/net_services/CXM_net_services.anubis b/calexium_lib/net_services/CXM_net_services.anubis new file mode 100644 index 0000000..45c1220 --- /dev/null +++ b/calexium_lib/net_services/CXM_net_services.anubis @@ -0,0 +1,164 @@ +/* + * + * User: David RENE + * Date: 25/04/2007 + * Time: 11:01 + * (c) Calexium + * + */ +read tools/basis.anubis +read system/convert.anubis +read system/string.anubis +read system/muscle.anubis +read system/data_io.anubis + +read CXM_generic_protocol.anubis +read calexium_lib/CXM_message_constants.anubis + +public type NetService: + net_service( + Int32 version, + Int32 id, + String name, + (RWStream) -> One handler + ). + +define One + print_services + ( + List(NetService) net_services + ) = + map_forget((NetService net_s)|-> + println(" id : 0x"+ to_hexa(net_s.id)); + println(" version : " + to_String(net_s.version)); + println(" name : "+ net_s.name ); + println("----------------------------------------") + ,net_services). + + /** Try to find the service_id in services_list. If the service is found in that list + * the corresponding NetService object is return + */ +define Maybe(NetService) + find_service + ( + List(NetService) services_list, + Int32 service_id, + Int32 service_version + )= + if services_list is + { + [] then failure, + [h.t] then + if h.id = service_id & h.version >= service_version then + success(h) + else + find_service(t, service_id, service_version) + }. + + /** Check if the muscle message msg has the correct fields for requesting a net_services + * if we found "service" and "version" fields on the message, we try to find if the service + * referenced in "service" is available in net_services list + */ +define Maybe(NetService) + has_service + ( + RWStream conn, + Message msg, + List(NetService) net_services + )= + if find_int32(msg, "service") is + { + failure then send_ACK_error(conn, _CXM_REQUEST_FOR_SERVICE); failure, + success(service_id) then + if find_int32(msg, "version") is + { + failure then send_ACK_error(conn, _CXM_REQUEST_FOR_SERVICE);failure, + success(service_version) then find_service(net_services, service_id, service_version) + } + }. + + /** This message_received function just handle the negociation process the available net_services. + * In other words, it only recognize the _CXM_REQUEST_FOR_SERVICE message and try to launch the + * corresponding servcice + */ + +define One + service_negociation + ( + RWStream conn, + Message msg, + List(NetService) net_services + )= + print("MESSAGE [" + to_ascii(*msg.what) + "] received\n"); + if * msg.what = _CXM_REQUEST_FOR_SERVICE then + if has_service(conn, msg, net_services) is + { + failure then unique, + success(net_service) then + send_ACK_ok(conn, _CXM_REQUEST_FOR_SERVICE); + net_service.handler(conn) + } + else + unique + . + + /** this function unflatten muscle message and give the correct message to service_negociation function + * + */ + //TODO At this time, the function is in infinite loop, we must change to provide a mechanism for quitting + //TODO when shutdown_required is true +define One + message_receiver + ( + RWStream conn, + List(NetService) net_services + ) = + + if unflatten_message(make_data_io(weaken(conn))) is + { + failure then + //print("can't unflatten any message\n"); + sleep(1); + message_receiver(conn, net_services), + success(msg) then + service_negociation(conn, msg, net_services); + message_receiver(conn, net_services) + }. + +define Server -> (RWStream) -> One + net_services_handler + ( + List(NetService) net_services, + ) = + (Server server) |-> (RWStream conn) |-> + if remote_IP_address_and_port(conn) is (num_peer,_) then + //convert IP address of the client to string + // with peer = ip_addr_to_string(num_peer), + // print("NET SERVICES Accepting connection with "+peer+"\n"); + + //now managing the list of SERVICES + message_receiver(conn, net_services). + + +public define Maybe(Server) + start_net_services + ( + List(NetService) net_services, + Int32 network_port, + Var(Bool) shutdown_required + )= + //TODO change the port number in the real world + if start_server(0, + network_port, + net_services_handler(net_services), + (One u) |-> unique) is + { + cannot_create_the_socket then println("Cannot create the listening socket."); failure, + cannot_bind_to_port then println("Cannot bind to port " + network_port ); failure, + cannot_listen_on_port then println("Cannot listen on port " + network_port); failure, + ok(server) then + println("Net services started on port " + network_port); + println("------ Available services ------"); + print_services(net_services); + success(server) + }. -- libgit2 0.21.4