Commit b326dd65564aeb14ef3656dade2370192d273ea6
1 parent
6eef9c2f
add net_services support
Showing
3 changed files
with
238 additions
and
3 deletions
Show diff stats
calexium_lib/CXM_message_constants.anubis
| @@ -18,7 +18,23 @@ public define Int32 _CXM_PKG_S_ADD_FILE = 0x33770404. // script action to | @@ -18,7 +18,23 @@ public define Int32 _CXM_PKG_S_ADD_FILE = 0x33770404. // script action to | ||
| 18 | public define Int32 _CXM_PKG_INDEX_PART = 0x33770440. // ID of package Index part | 18 | public define Int32 _CXM_PKG_INDEX_PART = 0x33770440. // ID of package Index part |
| 19 | public define Int32 _CXM_PKG_IDX_ENTRY = 0x33770441. // Index entry information | 19 | public define Int32 _CXM_PKG_IDX_ENTRY = 0x33770441. // Index entry information |
| 20 | 20 | ||
| 21 | - // **************** UPDATE SERVER *************** | 21 | + // **************** UPDATE SERVICE *************** |
| 22 | 22 | ||
| 23 | -public define Int32 _CXM_UPDATE_SERVER_MSG_BASIS = 0x33770800. // | ||
| 24 | -public define Int32 _CXM_UPD_HAS_UPDATE = 0x33770801. // | 23 | +public define Int32 _CXM_UPDATE_SERVICE_MSG_BASIS = 0x33770800. // |
| 24 | +public define Int32 _CXM_UPD_SERVICE_ID = 0x33770801. | ||
| 25 | +public define Int32 _CXM_UPD_HAS_UPDATE = 0x33770802. // | ||
| 26 | + | ||
| 27 | + // **************** SERVICE NEGOCIATION *************** | ||
| 28 | + | ||
| 29 | +public define Int32 _CXM_SERVICE_NEGOCIATION_BASIS = 0x33770C00. // | ||
| 30 | +public define Int32 _CXM_REQUEST_FOR_SERVICE = 0x33770C01. // | ||
| 31 | + | ||
| 32 | + // **************** GENERIC PROTOCOL *************** | ||
| 33 | + | ||
| 34 | +public define Int32 _CXM_GENERIC_PROTOCOL_BASIS = 0x33771000. // | ||
| 35 | +public define Int32 _CXM_ACK = 0x33771001. // | ||
| 36 | + | ||
| 37 | + // **************** FTP SERVICE *************** | ||
| 38 | + | ||
| 39 | +public define Int32 _CXM_FTP_SERVICE_MSG_BASIS = 0x33771400. // | ||
| 40 | +public define Int32 _CXM_FTP_SERVICE_ID = 0x33771401. |
calexium_lib/net_services/CXM_generic_protocol.anubis
0 → 100644
| 1 | +/* | ||
| 2 | + * | ||
| 3 | + * User: David RENE | ||
| 4 | + * Date: 25/04/2007 | ||
| 5 | + * Time: 16:20 | ||
| 6 | + * (c) Calexium | ||
| 7 | + * | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +read system/muscle.anubis | ||
| 11 | +read system/data_io.anubis | ||
| 12 | +read tools/basis.anubis | ||
| 13 | + | ||
| 14 | +read calexium_lib/CXM_message_constants.anubis | ||
| 15 | + | ||
| 16 | +public define Int32 _CXM_ERROR = 0. | ||
| 17 | +public define Int32 _CXM_OK = 1. | ||
| 18 | +public define Int32 _CXM_UNKNOW_CMD = 2. | ||
| 19 | + | ||
| 20 | +public define One | ||
| 21 | + send_ACK_error | ||
| 22 | + ( | ||
| 23 | + RWStream conn, | ||
| 24 | + Int32 cmd_id | ||
| 25 | + )= | ||
| 26 | + with err_msg = message(_CXM_ACK), | ||
| 27 | + forget(add_int32(err_msg, "_CMD", cmd_id)); | ||
| 28 | + forget(add_int32(err_msg, "_STATUS", _CXM_ERROR)); | ||
| 29 | + forget(send_message_by_Stream(conn, err_msg)) | ||
| 30 | + . | ||
| 31 | + | ||
| 32 | +public define One | ||
| 33 | + send_ACK_ok | ||
| 34 | + ( | ||
| 35 | + RWStream conn, | ||
| 36 | + Int32 cmd_id | ||
| 37 | + )= | ||
| 38 | + with ok_msg = message(_CXM_ACK), | ||
| 39 | + forget(add_int32(ok_msg, "_CMD", cmd_id)); | ||
| 40 | + forget(add_int32(ok_msg, "_STATUS", _CXM_OK)); | ||
| 41 | + forget(send_message_by_Stream(conn, ok_msg)) | ||
| 42 | + . | ||
| 43 | + | ||
| 44 | +public define Maybe(Message) | ||
| 45 | + wait_for_reply | ||
| 46 | + ( | ||
| 47 | + RWStream conn | ||
| 48 | + ) = | ||
| 49 | + if unflatten_message(make_data_io(weaken(conn))) is | ||
| 50 | + { | ||
| 51 | + failure then | ||
| 52 | + sleep(1); | ||
| 53 | + wait_for_reply(conn), | ||
| 54 | + success(msg) then success(msg) | ||
| 55 | + }. |
| 1 | +/* | ||
| 2 | + * | ||
| 3 | + * User: David RENE | ||
| 4 | + * Date: 25/04/2007 | ||
| 5 | + * Time: 11:01 | ||
| 6 | + * (c) Calexium | ||
| 7 | + * | ||
| 8 | + */ | ||
| 9 | +read tools/basis.anubis | ||
| 10 | +read system/convert.anubis | ||
| 11 | +read system/string.anubis | ||
| 12 | +read system/muscle.anubis | ||
| 13 | +read system/data_io.anubis | ||
| 14 | + | ||
| 15 | +read CXM_generic_protocol.anubis | ||
| 16 | +read calexium_lib/CXM_message_constants.anubis | ||
| 17 | + | ||
| 18 | +public type NetService: | ||
| 19 | + net_service( | ||
| 20 | + Int32 version, | ||
| 21 | + Int32 id, | ||
| 22 | + String name, | ||
| 23 | + (RWStream) -> One handler | ||
| 24 | + ). | ||
| 25 | + | ||
| 26 | +define One | ||
| 27 | + print_services | ||
| 28 | + ( | ||
| 29 | + List(NetService) net_services | ||
| 30 | + ) = | ||
| 31 | + map_forget((NetService net_s)|-> | ||
| 32 | + println(" id : 0x"+ to_hexa(net_s.id)); | ||
| 33 | + println(" version : " + to_String(net_s.version)); | ||
| 34 | + println(" name : "+ net_s.name ); | ||
| 35 | + println("----------------------------------------") | ||
| 36 | + ,net_services). | ||
| 37 | + | ||
| 38 | + /** Try to find the service_id in services_list. If the service is found in that list | ||
| 39 | + * the corresponding NetService object is return | ||
| 40 | + */ | ||
| 41 | +define Maybe(NetService) | ||
| 42 | + find_service | ||
| 43 | + ( | ||
| 44 | + List(NetService) services_list, | ||
| 45 | + Int32 service_id, | ||
| 46 | + Int32 service_version | ||
| 47 | + )= | ||
| 48 | + if services_list is | ||
| 49 | + { | ||
| 50 | + [] then failure, | ||
| 51 | + [h.t] then | ||
| 52 | + if h.id = service_id & h.version >= service_version then | ||
| 53 | + success(h) | ||
| 54 | + else | ||
| 55 | + find_service(t, service_id, service_version) | ||
| 56 | + }. | ||
| 57 | + | ||
| 58 | + /** Check if the muscle message msg has the correct fields for requesting a net_services | ||
| 59 | + * if we found "service" and "version" fields on the message, we try to find if the service | ||
| 60 | + * referenced in "service" is available in net_services list | ||
| 61 | + */ | ||
| 62 | +define Maybe(NetService) | ||
| 63 | + has_service | ||
| 64 | + ( | ||
| 65 | + RWStream conn, | ||
| 66 | + Message msg, | ||
| 67 | + List(NetService) net_services | ||
| 68 | + )= | ||
| 69 | + if find_int32(msg, "service") is | ||
| 70 | + { | ||
| 71 | + failure then send_ACK_error(conn, _CXM_REQUEST_FOR_SERVICE); failure, | ||
| 72 | + success(service_id) then | ||
| 73 | + if find_int32(msg, "version") is | ||
| 74 | + { | ||
| 75 | + failure then send_ACK_error(conn, _CXM_REQUEST_FOR_SERVICE);failure, | ||
| 76 | + success(service_version) then find_service(net_services, service_id, service_version) | ||
| 77 | + } | ||
| 78 | + }. | ||
| 79 | + | ||
| 80 | + /** This message_received function just handle the negociation process the available net_services. | ||
| 81 | + * In other words, it only recognize the _CXM_REQUEST_FOR_SERVICE message and try to launch the | ||
| 82 | + * corresponding servcice | ||
| 83 | + */ | ||
| 84 | + | ||
| 85 | +define One | ||
| 86 | + service_negociation | ||
| 87 | + ( | ||
| 88 | + RWStream conn, | ||
| 89 | + Message msg, | ||
| 90 | + List(NetService) net_services | ||
| 91 | + )= | ||
| 92 | + print("MESSAGE [" + to_ascii(*msg.what) + "] received\n"); | ||
| 93 | + if * msg.what = _CXM_REQUEST_FOR_SERVICE then | ||
| 94 | + if has_service(conn, msg, net_services) is | ||
| 95 | + { | ||
| 96 | + failure then unique, | ||
| 97 | + success(net_service) then | ||
| 98 | + send_ACK_ok(conn, _CXM_REQUEST_FOR_SERVICE); | ||
| 99 | + net_service.handler(conn) | ||
| 100 | + } | ||
| 101 | + else | ||
| 102 | + unique | ||
| 103 | + . | ||
| 104 | + | ||
| 105 | + /** this function unflatten muscle message and give the correct message to service_negociation function | ||
| 106 | + * | ||
| 107 | + */ | ||
| 108 | + //TODO At this time, the function is in infinite loop, we must change to provide a mechanism for quitting | ||
| 109 | + //TODO when shutdown_required is true | ||
| 110 | +define One | ||
| 111 | + message_receiver | ||
| 112 | + ( | ||
| 113 | + RWStream conn, | ||
| 114 | + List(NetService) net_services | ||
| 115 | + ) = | ||
| 116 | + | ||
| 117 | + if unflatten_message(make_data_io(weaken(conn))) is | ||
| 118 | + { | ||
| 119 | + failure then | ||
| 120 | + //print("can't unflatten any message\n"); | ||
| 121 | + sleep(1); | ||
| 122 | + message_receiver(conn, net_services), | ||
| 123 | + success(msg) then | ||
| 124 | + service_negociation(conn, msg, net_services); | ||
| 125 | + message_receiver(conn, net_services) | ||
| 126 | + }. | ||
| 127 | + | ||
| 128 | +define Server -> (RWStream) -> One | ||
| 129 | + net_services_handler | ||
| 130 | + ( | ||
| 131 | + List(NetService) net_services, | ||
| 132 | + ) = | ||
| 133 | + (Server server) |-> (RWStream conn) |-> | ||
| 134 | + if remote_IP_address_and_port(conn) is (num_peer,_) then | ||
| 135 | + //convert IP address of the client to string | ||
| 136 | + // with peer = ip_addr_to_string(num_peer), | ||
| 137 | + // print("NET SERVICES Accepting connection with "+peer+"\n"); | ||
| 138 | + | ||
| 139 | + //now managing the list of SERVICES | ||
| 140 | + message_receiver(conn, net_services). | ||
| 141 | + | ||
| 142 | + | ||
| 143 | +public define Maybe(Server) | ||
| 144 | + start_net_services | ||
| 145 | + ( | ||
| 146 | + List(NetService) net_services, | ||
| 147 | + Int32 network_port, | ||
| 148 | + Var(Bool) shutdown_required | ||
| 149 | + )= | ||
| 150 | + //TODO change the port number in the real world | ||
| 151 | + if start_server(0, | ||
| 152 | + network_port, | ||
| 153 | + net_services_handler(net_services), | ||
| 154 | + (One u) |-> unique) is | ||
| 155 | + { | ||
| 156 | + cannot_create_the_socket then println("Cannot create the listening socket."); failure, | ||
| 157 | + cannot_bind_to_port then println("Cannot bind to port " + network_port ); failure, | ||
| 158 | + cannot_listen_on_port then println("Cannot listen on port " + network_port); failure, | ||
| 159 | + ok(server) then | ||
| 160 | + println("Net services started on port " + network_port); | ||
| 161 | + println("------ Available services ------"); | ||
| 162 | + print_services(net_services); | ||
| 163 | + success(server) | ||
| 164 | + }. |