Commit e15e93137dfcc1f1fe22d31ff0a0984b9d8622c7
1 parent
0ddf58bc
Improving usability of net_services functions
Fix naming error (STATUS_STR -> STATUS_MSG in ACK message)
Showing
2 changed files
with
49 additions
and
22 deletions
Show diff stats
calexium_lib/net_services/CXM_generic_client.anubis
| @@ -32,6 +32,9 @@ public type NetServiceAnswer: | @@ -32,6 +32,9 @@ public type NetServiceAnswer: | ||
| 32 | // --Generic functions--------------------------------------------------------------------- | 32 | // --Generic functions--------------------------------------------------------------------- |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | +/** | ||
| 36 | + * Sends the message to the server then parses the answer and returns the RESULT message on CMD success. | ||
| 37 | + */ | ||
| 35 | public define Maybe(NetServiceAnswer) | 38 | public define Maybe(NetServiceAnswer) |
| 36 | generic_send_message | 39 | generic_send_message |
| 37 | ( | 40 | ( |
| @@ -57,40 +60,63 @@ public define Maybe(NetServiceAnswer) | @@ -57,40 +60,63 @@ public define Maybe(NetServiceAnswer) | ||
| 57 | if v = _CXM_OK then | 60 | if v = _CXM_OK then |
| 58 | success(netservice_ok(cmd, find_message(msg, "RESULT"))) | 61 | success(netservice_ok(cmd, find_message(msg, "RESULT"))) |
| 59 | else | 62 | else |
| 60 | - with error_string = if find_string(msg, "STATUS_STR") is success(s) then s else "", | 63 | + with error_string = if find_string(msg, "STATUS_MSG") is success(s) then s else "", |
| 61 | success(netservice_error(cmd, v, error_string)) | 64 | success(netservice_error(cmd, v, error_string)) |
| 62 | } | 65 | } |
| 63 | } | 66 | } |
| 64 | }. | 67 | }. |
| 65 | 68 | ||
| 66 | public define Maybe($T) | 69 | public define Maybe($T) |
| 67 | - generic_handler | 70 | + simple_handler |
| 68 | ( | 71 | ( |
| 69 | MessageQueue queue, | 72 | MessageQueue queue, |
| 70 | Int timeout, | 73 | Int timeout, |
| 71 | Message msg_to_send, | 74 | Message msg_to_send, |
| 72 | (Message) -> Maybe($T) handler, | 75 | (Message) -> Maybe($T) handler, |
| 73 | (String) -> One logger | 76 | (String) -> One logger |
| 74 | - )= | ||
| 75 | - queue.add_Message_to_send(msg_to_send); | ||
| 76 | - if queue.get_next_received_Message(timeout) is | 77 | + ) = |
| 78 | + if generic_send_message(queue, msg_to_send, timeout, logger) is | ||
| 77 | { | 79 | { |
| 78 | - timeout then logger("["+queue.get_name(unique)+"]: receive timeout");failure, | ||
| 79 | - closed then logger("["+queue.get_name(unique)+"]: socket closed");failure, | ||
| 80 | - msg(msg) then | ||
| 81 | - if find_int32(msg, "STATUS") is | 80 | + failure then failure, |
| 81 | + success(net_result) then | ||
| 82 | + if net_result is | ||
| 82 | { | 83 | { |
| 83 | - failure then logger("["+queue.get_name(unique)+"]: status not found");failure, | ||
| 84 | - success(v) then | ||
| 85 | - if v = _CXM_OK then | ||
| 86 | - //println("generic_handler("+queue.get_name(unique)+"): message status ok"); | ||
| 87 | - if find_message(msg, "RESULT") is | ||
| 88 | - { | ||
| 89 | - failure then logger("["+queue.get_name(unique)+"]: can't find RESULT");failure, | ||
| 90 | - success(result) then handler(result) | ||
| 91 | - } | ||
| 92 | - else | ||
| 93 | - logger("["+queue.get_name(unique)+"]: message status ERROR");failure | 84 | + netservice_error(cmd, err_code, err_str) then |
| 85 | + logger("["+queue.get_name(unique)+"]: message status ERROR [0x" + to_hexa(err_code) + ", '" + err_str + "']"); | ||
| 86 | + failure, | ||
| 87 | + netservice_ok(cmd, mb_msg) then | ||
| 88 | + if mb_msg is | ||
| 89 | + { | ||
| 90 | + failure then logger("["+queue.get_name(unique)+"]: can't find RESULT message."); failure, | ||
| 91 | + success(result) then handler(result) | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + }. | ||
| 95 | + | ||
| 96 | +public define Maybe(One) | ||
| 97 | + no_result_handler | ||
| 98 | + ( | ||
| 99 | + MessageQueue queue, | ||
| 100 | + Int timeout, | ||
| 101 | + Message msg_to_send, | ||
| 102 | + (String) -> One logger | ||
| 103 | + ) = | ||
| 104 | + if generic_send_message(queue, msg_to_send, timeout, logger) is | ||
| 105 | + { | ||
| 106 | + failure then failure, | ||
| 107 | + success(net_result) then | ||
| 108 | + if net_result is | ||
| 109 | + { | ||
| 110 | + netservice_error(cmd, err_code, err_str) then | ||
| 111 | + logger("["+queue.get_name(unique)+"]: message status ERROR [0x" + to_hexa(err_code) + ", '" + err_str + "']"); | ||
| 112 | + failure, | ||
| 113 | + netservice_ok(cmd, mb_msg) then | ||
| 114 | + if mb_msg is | ||
| 115 | + { | ||
| 116 | + failure then unique, | ||
| 117 | + success(result) then logger("An unattended RESULT msg was found. Ignoring it...") | ||
| 118 | + }; | ||
| 119 | + success(unique) | ||
| 94 | } | 120 | } |
| 95 | }. | 121 | }. |
| 96 | 122 | ||
| @@ -103,7 +129,7 @@ public define (MessageQueue, String) -> Maybe($T) | @@ -103,7 +129,7 @@ public define (MessageQueue, String) -> Maybe($T) | ||
| 103 | (String) -> One logger | 129 | (String) -> One logger |
| 104 | ) = | 130 | ) = |
| 105 | (MessageQueue queue, String timestamp) |-> | 131 | (MessageQueue queue, String timestamp) |-> |
| 106 | - generic_handler(queue, timeout, msg_to_send, handler, logger). | 132 | + simple_handler(queue, timeout, msg_to_send, handler, logger). |
| 107 | 133 | ||
| 108 | define Maybe($T) | 134 | define Maybe($T) |
| 109 | generic_request_for_service | 135 | generic_request_for_service |
calexium_lib/net_services/CXM_generic_protocol.anubis
| @@ -18,6 +18,7 @@ public define Word32 _CXM_OK = 0. | @@ -18,6 +18,7 @@ public define Word32 _CXM_OK = 0. | ||
| 18 | public define Word32 _CXM_ERROR = 1. | 18 | public define Word32 _CXM_ERROR = 1. |
| 19 | public define Word32 _CXM_UNKNOW_CMD = 2. | 19 | public define Word32 _CXM_UNKNOW_CMD = 2. |
| 20 | public define Word32 _CXM_UNKNOW_SERVICE = 3. | 20 | public define Word32 _CXM_UNKNOW_SERVICE = 3. |
| 21 | +public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4. | ||
| 21 | 22 | ||
| 22 | public type ProtocolResult: | 23 | public type ProtocolResult: |
| 23 | failure, | 24 | failure, |
| @@ -38,7 +39,7 @@ public define One | @@ -38,7 +39,7 @@ public define One | ||
| 38 | with err_msg = message(_CXM_ACK), | 39 | with err_msg = message(_CXM_ACK), |
| 39 | forget(add_int32(err_msg, "CMD", cmd_id)); | 40 | forget(add_int32(err_msg, "CMD", cmd_id)); |
| 40 | forget(add_int32(err_msg, "STATUS", error_code)); | 41 | forget(add_int32(err_msg, "STATUS", error_code)); |
| 41 | - (if error_string /= "" then forget(add_string(err_msg, "STATUS_STR", error_string)) | 42 | + (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string)) |
| 42 | else unique); | 43 | else unique); |
| 43 | forget(queue.add_Message_to_send(err_msg)). | 44 | forget(queue.add_Message_to_send(err_msg)). |
| 44 | 45 |