Commit 027344b67de0e80bc2647eaf7a01295df795a1fb

Authored by Cédric RICARD
1 parent 79a56edb

Nothing

Showing 1 changed file with 170 additions and 170 deletions   Show diff stats
calexium_lib/net_services/CXM_net_services.anubis
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   -read system/message_queue.anubis
15   -read system/message_transceiver.anubis
16   -read CXM_generic_protocol.anubis
17   -read calexium_lib/CXM_message_constants.anubis
18   -
19   -public type NetService:
20   - net_service(
21   - Int32 version,
22   - Int32 id,
23   - String name,
24   - (MessageQueue) -> One handler
25   - ).
26   -
27   -define One
28   - print_services
29   - (
30   - List(NetService) net_services
31   - ) =
32   - map_forget((NetService net_s)|->
33   - println(" id : 0x"+ to_hexa(net_s.id));
34   - println(" version : " + to_String(net_s.version));
35   - println(" name : "+ net_s.name );
36   - println("----------------------------------------")
37   - ,net_services).
38   -
39   - /** Try to find the service_id in services_list. If the service is found in that list
40   - * the corresponding NetService object is return
41   - */
42   -define Maybe(NetService)
43   - find_service
44   - (
45   - List(NetService) services_list,
46   - Int32 service_id,
47   - Int32 service_version
48   - )=
49   - if services_list is
50   - {
51   - [] then failure,
52   - [h.t] then
53   - if h.id = service_id & h.version >= service_version then
54   - success(h)
55   - else
56   - find_service(t, service_id, service_version)
57   - }.
58   -
59   - /** Check if the muscle message msg has the correct fields for requesting a net_services
60   - * if we found "service" and "version" fields on the message, we try to find if the service
61   - * referenced in "service" is available in net_services list
62   - */
63   -define Maybe(NetService)
64   - has_service
65   - (
66   - MessageQueue queue,
67   - Message msg,
68   - List(NetService) net_services
69   - )=
70   - if find_int32(msg, "service") is
71   - {
72   - failure then send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE); failure,
73   - success(service_id) then
74   - if find_int32(msg, "version") is
75   - {
76   - failure then send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE);failure,
77   - success(service_version) then find_service(net_services, service_id, service_version)
78   - }
79   - }.
80   -
81   - /** This message_received function just handle the negociation process the available net_services.
82   - * In other words, it only recognize the _CXM_REQUEST_FOR_SERVICE message and try to launch the
83   - * corresponding servcice
84   - */
85   -
86   -define One
87   - service_negociation
88   - (
89   - MessageQueue queue,
90   - Message msg,
91   - List(NetService) net_services
92   - )=
93   - //TODO DR add a real management of error
94   - //println("Service NEGOCIATION [" + to_hexa(*msg.what) + "] received");
95   - if * msg.what = _CXM_REQUEST_FOR_SERVICE then
96   - if has_service(queue, msg, net_services) is
97   - {
98   - failure then
99   - send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE),
100   - success(net_service) then
101   - send_ACK_ok(queue, _CXM_REQUEST_FOR_SERVICE);
102   - net_service.handler(queue)
103   - }
104   - else
105   - unique
106   - .
107   -
108   - /**
109   - * this function unflatten muscle message and give the correct message to service_negociation function
110   - */
111   -define One
112   - message_receiver
113   - (
114   - MessageQueue queue,
115   - List(NetService) net_services
116   - ) =
117   - if queue.quit_requested(unique) then
118   - unique
119   - else
120   - //println("PRE SERVICE message_receiver "+"["+virtual_machine_id + "]");
121   - if queue.get_next_received_Message(1) is
122   - {
123   - timeout then //println("PRE timeout");
124   - message_receiver(queue, net_services),
125   - closed then //println("PRE closed");
126   - unique,
127   - msg(msg) then unique; //println("PRE negociation");
128   - service_negociation(queue, msg, net_services);
129   - message_receiver(queue, net_services)
130   - }.
131   -
132   -define Server -> (RWStream) -> One
133   - net_services_handler
134   - (
135   - List(NetService) net_services,
136   - ) =
137   - (Server server) |-> (RWStream conn) |->
138   - if remote_IP_address_and_port(conn) is (num_peer,_) then
139   - //convert IP address of the client to string
140   - with peer = ip_addr_to_string(num_peer),
141   - //println("NET SERVICES Accepting connection with "+peer);
142   -
143   - //now managing the list of SERVICES
144   - with queue = create_MessageQueue("CXM Net Services"),
145   - message_transceiver(conn, queue);
146   - message_receiver(queue, net_services).
147   -
148   -
149   -public define Maybe(Server)
150   - start_net_services
151   - (
152   - List(NetService) net_services,
153   - Int32 network_port,
154   - Var(Bool) shutdown_required
155   - )=
156   - //TODO change the port number in the real world
157   - if start_server(0,
158   - network_port,
159   - net_services_handler(net_services),
160   - (One u) |-> unique) is
161   - {
162   - cannot_create_the_socket then println("Cannot create the listening socket."); failure,
163   - cannot_bind_to_port then println("Cannot bind to port " + network_port ); failure,
164   - cannot_listen_on_port then println("Cannot listen on port " + network_port); failure,
165   - ok(server) then
166   - println("Net services started on port " + network_port);
167   - println("------ Available services ------");
168   - print_services(net_services);
169   - success(server)
170   - }.
  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 +read system/message_queue.anubis
  15 +read system/message_transceiver.anubis
  16 +read CXM_generic_protocol.anubis
  17 +read calexium_lib/CXM_message_constants.anubis
  18 +
  19 +public type NetService:
  20 + net_service(
  21 + Int32 version,
  22 + Int32 id,
  23 + String name,
  24 + (MessageQueue) -> One handler
  25 + ).
  26 +
  27 +define One
  28 + print_services
  29 + (
  30 + List(NetService) net_services
  31 + ) =
  32 + map_forget((NetService net_s)|->
  33 + println(" id : 0x"+ to_hexa(net_s.id));
  34 + println(" version : " + to_String(net_s.version));
  35 + println(" name : "+ net_s.name );
  36 + println("----------------------------------------")
  37 + ,net_services).
  38 +
  39 + /** Try to find the service_id in services_list. If the service is found in that list
  40 + * the corresponding NetService object is return
  41 + */
  42 +define Maybe(NetService)
  43 + find_service
  44 + (
  45 + List(NetService) services_list,
  46 + Int32 service_id,
  47 + Int32 service_version
  48 + )=
  49 + if services_list is
  50 + {
  51 + [] then failure,
  52 + [h.t] then
  53 + if h.id = service_id & h.version >= service_version then
  54 + success(h)
  55 + else
  56 + find_service(t, service_id, service_version)
  57 + }.
  58 +
  59 + /** Check if the muscle message msg has the correct fields for requesting a net_services
  60 + * if we found "service" and "version" fields on the message, we try to find if the service
  61 + * referenced in "service" is available in net_services list
  62 + */
  63 +define Maybe(NetService)
  64 + has_service
  65 + (
  66 + MessageQueue queue,
  67 + Message msg,
  68 + List(NetService) net_services
  69 + )=
  70 + if find_int32(msg, "service") is
  71 + {
  72 + failure then send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE); failure,
  73 + success(service_id) then
  74 + if find_int32(msg, "version") is
  75 + {
  76 + failure then send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE);failure,
  77 + success(service_version) then find_service(net_services, service_id, service_version)
  78 + }
  79 + }.
  80 +
  81 + /** This message_received function just handle the negociation process the available net_services.
  82 + * In other words, it only recognize the _CXM_REQUEST_FOR_SERVICE message and try to launch the
  83 + * corresponding servcice
  84 + */
  85 +
  86 +define One
  87 + service_negociation
  88 + (
  89 + MessageQueue queue,
  90 + Message msg,
  91 + List(NetService) net_services
  92 + )=
  93 + //TODO DR add a real management of error
  94 + //println("Service NEGOCIATION [" + to_hexa(*msg.what) + "] received");
  95 + if * msg.what = _CXM_REQUEST_FOR_SERVICE then
  96 + if has_service(queue, msg, net_services) is
  97 + {
  98 + failure then
  99 + send_ACK_error(queue, _CXM_REQUEST_FOR_SERVICE),
  100 + success(net_service) then
  101 + send_ACK_ok(queue, _CXM_REQUEST_FOR_SERVICE);
  102 + net_service.handler(queue)
  103 + }
  104 + else
  105 + unique
  106 + .
  107 +
  108 + /**
  109 + * this function unflatten muscle message and give the correct message to service_negociation function
  110 + */
  111 +define One
  112 + message_receiver
  113 + (
  114 + MessageQueue queue,
  115 + List(NetService) net_services
  116 + ) =
  117 + if queue.quit_requested(unique) then
  118 + unique
  119 + else
  120 + //println("PRE SERVICE message_receiver "+"["+virtual_machine_id + "]");
  121 + if queue.get_next_received_Message(1) is
  122 + {
  123 + timeout then //println("PRE timeout");
  124 + message_receiver(queue, net_services),
  125 + closed then //println("PRE closed");
  126 + unique,
  127 + msg(msg) then unique; //println("PRE negociation");
  128 + service_negociation(queue, msg, net_services);
  129 + message_receiver(queue, net_services)
  130 + }.
  131 +
  132 +define Server -> (RWStream) -> One
  133 + net_services_handler
  134 + (
  135 + List(NetService) net_services,
  136 + ) =
  137 + (Server server) |-> (RWStream conn) |->
  138 + if remote_IP_address_and_port(conn) is (num_peer,_) then
  139 + //convert IP address of the client to string
  140 + with peer = ip_addr_to_string(num_peer),
  141 + //println("NET SERVICES Accepting connection with "+peer);
  142 +
  143 + //now managing the list of SERVICES
  144 + with queue = create_MessageQueue("CXM Net Services"),
  145 + message_transceiver(conn, queue);
  146 + message_receiver(queue, net_services).
  147 +
  148 +
  149 +public define Maybe(Server)
  150 + start_net_services
  151 + (
  152 + List(NetService) net_services,
  153 + Int32 network_port,
  154 + Var(Bool) shutdown_required
  155 + )=
  156 + //TODO change the port number in the real world
  157 + if start_server(0,
  158 + network_port,
  159 + net_services_handler(net_services),
  160 + (One u) |-> unique) is
  161 + {
  162 + cannot_create_the_socket then println("Cannot create the listening socket."); failure,
  163 + cannot_bind_to_port then println("Cannot bind to port " + network_port ); failure,
  164 + cannot_listen_on_port then println("Cannot listen on port " + network_port); failure,
  165 + ok(server) then
  166 + println("Net services started on port " + network_port);
  167 + println("------ Available services ------");
  168 + print_services(net_services);
  169 + success(server)
  170 + }.
... ...