Commit b2e81805e590890105faf3bd0d1b0082595f2913
1 parent
d12dd7c9
[ADD] add get and send file_ref in generic protocol
Showing
2 changed files
with
284 additions
and
189 deletions
Show diff stats
CXM_message_constants.anubis
| ... | ... | @@ -46,9 +46,10 @@ public define Word32 _CXM_LOGIN = 0x33770C02. // |
| 46 | 46 | |
| 47 | 47 | // **************** GENERIC PROTOCOL *************** |
| 48 | 48 | |
| 49 | -public define Word32 _CXM_GENERIC_PROTOCOL_BASIS = 0x33771000. // | |
| 50 | -public define Word32 _CXM_ACK = 0x33771001. // | |
| 51 | -public define Word32 _CXM_ACK_RESULT_MSG = 0x33771002. // | |
| 49 | +public define Word32 _CXM_GENERIC_PROTOCOL_BASIS = 0x33771000. // | |
| 50 | +public define Word32 _CXM_ACK = 0x33771001. // | |
| 51 | +public define Word32 _CXM_ACK_RESULT_MSG = 0x33771002. // | |
| 52 | +public define Word32 _CXM_GET_FILE_REF = 0x33771003. //get local or remote file without FTP service | |
| 52 | 53 | |
| 53 | 54 | // **************** FTP SERVICE *************** |
| 54 | 55 | ... | ... |
net_services/CXM_generic_protocol.anubis
| 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 system/string.anubis | |
| 13 | -read tools/basis.anubis | |
| 14 | -read system/message_queue.anubis | |
| 15 | -read calexium_lib/CXM_message_constants.anubis | |
| 16 | - | |
| 17 | -public define Word32 _CXM_OK = 0. | |
| 18 | -public define Word32 _CXM_ERROR = 1. | |
| 19 | -public define Word32 _CXM_UNKNOW_CMD = 2. | |
| 20 | -public define Word32 _CXM_UNKNOW_SERVICE = 3. | |
| 21 | -public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4. | |
| 22 | -public define Word32 _CXM_FORBIDDEN = 5. | |
| 23 | -public define Word32 _CXM_BAD_AUTHENTICATION = 6. | |
| 24 | -public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later | |
| 25 | - | |
| 26 | -public type ProtocolResult: | |
| 27 | - failure, | |
| 28 | - timeout, | |
| 29 | - unknow_cmd, | |
| 30 | - error, | |
| 31 | - error(Word32, String), | |
| 32 | - ok, | |
| 33 | - ok_msg(Message). | |
| 34 | - | |
| 35 | -public define One | |
| 36 | - send_ACK_error | |
| 37 | - ( | |
| 38 | - MessageQueue queue, | |
| 39 | - Word32 cmd_id, | |
| 40 | - Word32 error_code, | |
| 41 | - String error_string, | |
| 42 | - )= | |
| 43 | - with err_msg = message(_CXM_ACK), | |
| 44 | - forget(add_int32(err_msg, "CMD", cmd_id)); | |
| 45 | - forget(add_int32(err_msg, "STATUS", error_code)); | |
| 46 | - (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string)) | |
| 47 | - else unique); | |
| 48 | - forget(queue.add_Message_to_send(err_msg)). | |
| 49 | - | |
| 50 | -public define One | |
| 51 | - send_ACK_error | |
| 52 | - ( | |
| 53 | - MessageQueue queue, | |
| 54 | - Word32 cmd_id | |
| 55 | - )= | |
| 56 | - send_ACK_error(queue, cmd_id, _CXM_ERROR, ""). | |
| 57 | - | |
| 58 | -public define One | |
| 59 | - send_ACK_ok | |
| 60 | - ( | |
| 61 | - MessageQueue queue, | |
| 62 | - Word32 cmd_id | |
| 63 | - )= | |
| 64 | - with ok_msg = message(_CXM_ACK), | |
| 65 | - forget(add_int32(ok_msg, "CMD", cmd_id)); | |
| 66 | - forget(add_int32(ok_msg, "STATUS", _CXM_OK)); | |
| 67 | - forget(queue.add_Message_to_send(ok_msg)) | |
| 68 | - . | |
| 69 | - | |
| 70 | -public define One | |
| 71 | - send_ACK_ok | |
| 72 | - ( | |
| 73 | - MessageQueue queue, | |
| 74 | - Word32 cmd_id, | |
| 75 | - Message result | |
| 76 | - )= | |
| 77 | - with ok_msg = message(_CXM_ACK), | |
| 78 | - forget(add_int32(ok_msg, "CMD", cmd_id)); | |
| 79 | - forget(add_int32(ok_msg, "STATUS", _CXM_OK)); | |
| 80 | - forget(add_message(ok_msg, "RESULT", result)); | |
| 81 | - forget(queue.add_Message_to_send(ok_msg)) | |
| 82 | - . | |
| 83 | - | |
| 84 | -public define One | |
| 85 | - send_result | |
| 86 | - ( | |
| 87 | - MessageQueue queue, | |
| 88 | - Word32 cmd_id, | |
| 89 | - Maybe(Message) mb_msg | |
| 90 | - )= | |
| 91 | - if mb_msg is | |
| 92 | - { | |
| 93 | - failure then send_ACK_error(queue, cmd_id), | |
| 94 | - success(msg) then send_ACK_ok(queue, cmd_id, msg) | |
| 95 | - }. | |
| 96 | - | |
| 97 | -public define One | |
| 98 | - send_result | |
| 99 | - ( | |
| 100 | - MessageQueue queue, | |
| 101 | - Word32 cmd_id, | |
| 102 | - Result((Word32, String), Message) mb_msg | |
| 103 | - )= | |
| 104 | - if mb_msg is | |
| 105 | - { | |
| 106 | - error(err) then | |
| 107 | - if err is (err_code, err_string) then | |
| 108 | - send_ACK_error(queue, cmd_id, err_code, err_string), | |
| 109 | - ok(msg) then send_ACK_ok(queue, cmd_id, msg) | |
| 110 | - }. | |
| 111 | - | |
| 112 | -public define One | |
| 113 | - send_result | |
| 114 | - ( | |
| 115 | - MessageQueue queue, | |
| 116 | - Word32 cmd_id, | |
| 117 | - Bool result | |
| 118 | - )= | |
| 119 | - if result then | |
| 120 | - send_ACK_ok(queue, cmd_id) | |
| 121 | - else | |
| 122 | - send_ACK_error(queue, cmd_id). | |
| 123 | - | |
| 124 | -public define ProtocolResult | |
| 125 | - wait_for_reply | |
| 126 | - ( | |
| 127 | - MessageQueue mQ, | |
| 128 | - Word32 wait_cmd, | |
| 129 | - Int t_out | |
| 130 | - ) = | |
| 131 | - if mQ.get_next_received_Message(t_out) is | |
| 132 | - { | |
| 133 | - timeout then timeout, | |
| 134 | - closed then failure, //println("wait_for_reply closed"); | |
| 135 | - | |
| 136 | - msg(_msg) then | |
| 137 | - if *_msg.what = _CXM_ACK then | |
| 138 | - if find_int32(_msg, "CMD") is | |
| 139 | - { | |
| 140 | - failure then failure, //println("wait_for_reply CMD"); | |
| 141 | - success(cmd) then | |
| 142 | -// println("wait_for_reply CMD="+to_hexa(cmd)); | |
| 143 | - if find_int32(_msg, "STATUS") is | |
| 144 | - { | |
| 145 | - failure then failure, //println("wait_for_reply STATUS"); | |
| 146 | - success(status) then | |
| 147 | - if cmd = wait_cmd then | |
| 148 | - ( | |
| 149 | - if status = _CXM_OK then | |
| 150 | - if find_message(_msg, "RESULT") is | |
| 151 | - { | |
| 152 | - failure then ok, | |
| 153 | - success(ok_message) then ok_msg(ok_message) | |
| 154 | - } | |
| 155 | - else if status = _CXM_ERROR then | |
| 156 | - error | |
| 157 | - else if status = _CXM_UNKNOW_CMD then | |
| 158 | - unknow_cmd | |
| 159 | - else | |
| 160 | - error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "") | |
| 161 | - ) | |
| 162 | - else | |
| 163 | - failure //println("wait_for_reply "); | |
| 164 | - } | |
| 165 | - } | |
| 166 | - else | |
| 167 | - failure //println("wait_for_reply not ACK"); | |
| 168 | - }. | |
| 169 | - | |
| 170 | -public define Bool | |
| 171 | - simple_wait_for_reply | |
| 172 | - ( | |
| 173 | - MessageQueue mQ, | |
| 174 | - Word32 wait_cmd, | |
| 175 | - Int t_out | |
| 176 | - ) = | |
| 177 | - if wait_for_reply(mQ, wait_cmd, t_out) is | |
| 178 | - { | |
| 179 | - failure then false, | |
| 180 | - timeout then false, | |
| 181 | - unknow_cmd then false, | |
| 182 | - error then false, | |
| 183 | - error(_, _) then false, | |
| 184 | - ok then true, | |
| 185 | - ok_msg(msg) then true | |
| 186 | - }. | |
| 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 system/string.anubis | |
| 13 | +read system/files.anubis | |
| 14 | +read tools/basis.anubis | |
| 15 | +read system/message_queue.anubis | |
| 16 | +read calexium_lib/CXM_message_constants.anubis | |
| 17 | +read calexium_lib/types/generated/file_ref.anubis | |
| 18 | + | |
| 19 | + | |
| 20 | +public define Word32 _CXM_OK = 0. | |
| 21 | +public define Word32 _CXM_ERROR = 1. | |
| 22 | +public define Word32 _CXM_UNKNOW_CMD = 2. | |
| 23 | +public define Word32 _CXM_UNKNOW_SERVICE = 3. | |
| 24 | +public define Word32 _CXM_MISSING_REQUIRED_FIELD = 4. | |
| 25 | +public define Word32 _CXM_FORBIDDEN = 5. | |
| 26 | +public define Word32 _CXM_BAD_AUTHENTICATION = 6. | |
| 27 | +public define Word32 _CXM_TEMPORARY_ERROR = 7. // When received, the client should try later | |
| 28 | + | |
| 29 | +public type ProtocolResult: | |
| 30 | + failure, | |
| 31 | + timeout, | |
| 32 | + unknow_cmd, | |
| 33 | + error, | |
| 34 | + error(Word32, String), | |
| 35 | + ok, | |
| 36 | + ok_msg(Message). | |
| 37 | + | |
| 38 | +public define One | |
| 39 | + send_ACK_error | |
| 40 | + ( | |
| 41 | + MessageQueue queue, | |
| 42 | + Word32 cmd_id, | |
| 43 | + Word32 error_code, | |
| 44 | + String error_string, | |
| 45 | + )= | |
| 46 | + with err_msg = message(_CXM_ACK), | |
| 47 | + forget(add_int32(err_msg, "CMD", cmd_id)); | |
| 48 | + forget(add_int32(err_msg, "STATUS", error_code)); | |
| 49 | + (if error_string /= "" then forget(add_string(err_msg, "STATUS_MSG", error_string)) | |
| 50 | + else unique); | |
| 51 | + forget(queue.add_Message_to_send(err_msg)). | |
| 52 | + | |
| 53 | +public define One | |
| 54 | + send_ACK_error | |
| 55 | + ( | |
| 56 | + MessageQueue queue, | |
| 57 | + Word32 cmd_id | |
| 58 | + )= | |
| 59 | + send_ACK_error(queue, cmd_id, _CXM_ERROR, ""). | |
| 60 | + | |
| 61 | +public define One | |
| 62 | + send_ACK_ok | |
| 63 | + ( | |
| 64 | + MessageQueue queue, | |
| 65 | + Word32 cmd_id | |
| 66 | + )= | |
| 67 | + with ok_msg = message(_CXM_ACK), | |
| 68 | + forget(add_int32(ok_msg, "CMD", cmd_id)); | |
| 69 | + forget(add_int32(ok_msg, "STATUS", _CXM_OK)); | |
| 70 | + forget(queue.add_Message_to_send(ok_msg)) | |
| 71 | + . | |
| 72 | + | |
| 73 | +public define One | |
| 74 | + send_ACK_ok | |
| 75 | + ( | |
| 76 | + MessageQueue queue, | |
| 77 | + Word32 cmd_id, | |
| 78 | + Message result | |
| 79 | + )= | |
| 80 | + with ok_msg = message(_CXM_ACK), | |
| 81 | + forget(add_int32(ok_msg, "CMD", cmd_id)); | |
| 82 | + forget(add_int32(ok_msg, "STATUS", _CXM_OK)); | |
| 83 | + forget(add_message(ok_msg, "RESULT", result)); | |
| 84 | + forget(queue.add_Message_to_send(ok_msg)) | |
| 85 | + . | |
| 86 | + | |
| 87 | +public define One | |
| 88 | + send_result | |
| 89 | + ( | |
| 90 | + MessageQueue queue, | |
| 91 | + Word32 cmd_id, | |
| 92 | + Maybe(Message) mb_msg | |
| 93 | + )= | |
| 94 | + if mb_msg is | |
| 95 | + { | |
| 96 | + failure then send_ACK_error(queue, cmd_id), | |
| 97 | + success(msg) then send_ACK_ok(queue, cmd_id, msg) | |
| 98 | + }. | |
| 99 | + | |
| 100 | +public define One | |
| 101 | + send_result | |
| 102 | + ( | |
| 103 | + MessageQueue queue, | |
| 104 | + Word32 cmd_id, | |
| 105 | + Result((Word32, String), Message) mb_msg | |
| 106 | + )= | |
| 107 | + if mb_msg is | |
| 108 | + { | |
| 109 | + error(err) then | |
| 110 | + if err is (err_code, err_string) then | |
| 111 | + send_ACK_error(queue, cmd_id, err_code, err_string), | |
| 112 | + ok(msg) then send_ACK_ok(queue, cmd_id, msg) | |
| 113 | + }. | |
| 114 | + | |
| 115 | +public define One | |
| 116 | + send_result | |
| 117 | + ( | |
| 118 | + MessageQueue queue, | |
| 119 | + Word32 cmd_id, | |
| 120 | + Bool result | |
| 121 | + )= | |
| 122 | + if result then | |
| 123 | + send_ACK_ok(queue, cmd_id) | |
| 124 | + else | |
| 125 | + send_ACK_error(queue, cmd_id). | |
| 126 | + | |
| 127 | +public define ProtocolResult | |
| 128 | + wait_for_reply | |
| 129 | + ( | |
| 130 | + MessageQueue mQ, | |
| 131 | + Word32 wait_cmd, | |
| 132 | + Int t_out | |
| 133 | + ) = | |
| 134 | + if mQ.get_next_received_Message(t_out) is | |
| 135 | + { | |
| 136 | + timeout then timeout, | |
| 137 | + closed then failure, //println("wait_for_reply closed"); | |
| 138 | + | |
| 139 | + msg(_msg) then | |
| 140 | + if *_msg.what = _CXM_ACK then | |
| 141 | + if find_int32(_msg, "CMD") is | |
| 142 | + { | |
| 143 | + failure then failure, //println("wait_for_reply CMD"); | |
| 144 | + success(cmd) then | |
| 145 | +// println("wait_for_reply CMD="+to_hexa(cmd)); | |
| 146 | + if find_int32(_msg, "STATUS") is | |
| 147 | + { | |
| 148 | + failure then failure, //println("wait_for_reply STATUS"); | |
| 149 | + success(status) then | |
| 150 | + if cmd = wait_cmd then | |
| 151 | + ( | |
| 152 | + if status = _CXM_OK then | |
| 153 | + if find_message(_msg, "RESULT") is | |
| 154 | + { | |
| 155 | + failure then ok, | |
| 156 | + success(ok_message) then ok_msg(ok_message) | |
| 157 | + } | |
| 158 | + else if status = _CXM_ERROR then | |
| 159 | + error | |
| 160 | + else if status = _CXM_UNKNOW_CMD then | |
| 161 | + unknow_cmd | |
| 162 | + else | |
| 163 | + error(status, if find_string(_msg, "STATUS_MSG") is success(txt) then txt else "") | |
| 164 | + ) | |
| 165 | + else | |
| 166 | + failure //println("wait_for_reply "); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + else | |
| 170 | + failure //println("wait_for_reply not ACK"); | |
| 171 | + }. | |
| 172 | + | |
| 173 | +public define Bool | |
| 174 | + simple_wait_for_reply | |
| 175 | + ( | |
| 176 | + MessageQueue mQ, | |
| 177 | + Word32 wait_cmd, | |
| 178 | + Int t_out | |
| 179 | + ) = | |
| 180 | + if wait_for_reply(mQ, wait_cmd, t_out) is | |
| 181 | + { | |
| 182 | + failure then false, | |
| 183 | + timeout then false, | |
| 184 | + unknow_cmd then false, | |
| 185 | + error then false, | |
| 186 | + error(_, _) then false, | |
| 187 | + ok then true, | |
| 188 | + ok_msg(msg) then true | |
| 189 | + }. | |
| 190 | + | |
| 191 | +public define Bool | |
| 192 | + get_file_ref | |
| 193 | + ( | |
| 194 | + MessageQueue mQ, | |
| 195 | + File_ref f_ref, | |
| 196 | + String tmp_path | |
| 197 | + )= | |
| 198 | + //create the target | |
| 199 | + with target_file = tmp_path + "/" + f_ref.name, | |
| 200 | + | |
| 201 | + with msg = message(_CXM_GET_FILE_REF), | |
| 202 | + forget(add_message(msg, "GET_FILE_REF", to_Message(f_ref))); | |
| 203 | + forget(mQ.add_Message_to_send(msg)); | |
| 204 | + | |
| 205 | + if wait_for_reply(mQ, _CXM_GET_FILE_REF, 10) is | |
| 206 | + { | |
| 207 | + failure then false, | |
| 208 | + timeout then false, | |
| 209 | + unknow_cmd then false, | |
| 210 | + error then false, | |
| 211 | + error(_, _) then false, | |
| 212 | + ok then false, //false because OK without result message is not allowed | |
| 213 | + ok_msg(rmsg) then | |
| 214 | + with size = if find_int64(rmsg, "SIZE") is {failure then f_ref.size, success(i64) then to_Int([i64])}, | |
| 215 | + with mode = find_string(rmsg, "MODE", "NEW"), | |
| 216 | + if mode = "APPEND" then | |
| 217 | + if (Maybe(RWStream))file(target_file, append) is | |
| 218 | + { | |
| 219 | + failure then println("can't create target file"+target_file);false, //nothing to write | |
| 220 | + success(target) then | |
| 221 | + if copy_file_to_Connection(mQ.get_connection(unique), file(target), size) is | |
| 222 | + { | |
| 223 | + failure then false, | |
| 224 | + success(_) then true | |
| 225 | + } | |
| 226 | + } | |
| 227 | + else //by default the mode is new | |
| 228 | + if (Maybe(RWStream))file(target_file, append) is | |
| 229 | + { | |
| 230 | + failure then println("can't create target file"+target_file);false, //nothing to write | |
| 231 | + success(target) then | |
| 232 | + if copy_file_to_Connection(mQ.get_connection(unique), file(target), size) is | |
| 233 | + { | |
| 234 | + failure then false, | |
| 235 | + success(_) then true | |
| 236 | + } | |
| 237 | + } | |
| 238 | + } | |
| 239 | +. | |
| 240 | + | |
| 241 | +public define Bool | |
| 242 | + send_file_ref | |
| 243 | + ( | |
| 244 | + MessageQueue mQ, | |
| 245 | + Message msg, | |
| 246 | + )= | |
| 247 | + | |
| 248 | + if *msg.what = _CXM_GET_FILE_REF then | |
| 249 | + if find_message(msg, "GET_FILE_REF") is { | |
| 250 | + failure then false | |
| 251 | + success(file_ref_obj_msg) then | |
| 252 | + if (Maybe(File_ref))from_Message(file_ref_obj_msg) is | |
| 253 | + { | |
| 254 | + failure then false, | |
| 255 | + success(f_ref) then | |
| 256 | + | |
| 257 | + //create the target | |
| 258 | + with source_file = f_ref.path + "/" + f_ref.name, | |
| 259 | + with msg = message(0), | |
| 260 | + with size = file_size(source_file), | |
| 261 | + | |
| 262 | + forget(add_string(msg, "MODE", "NEW")); | |
| 263 | + forget(add_int64(msg, "SIZE", to_Word64(size))); | |
| 264 | + | |
| 265 | + if (Maybe(RStream))file(source_file, read) is | |
| 266 | + { | |
| 267 | + failure then println("can't open source file"+source_file);false, //nothing to write | |
| 268 | + success(source) then | |
| 269 | + send_ACK_ok(mQ, _CXM_GET_FILE_REF, msg); | |
| 270 | + if copy_file_to_Connection(file(source), mQ.get_connection(unique), size) is | |
| 271 | + { | |
| 272 | + failure then false, | |
| 273 | + success(_) then true | |
| 274 | + } | |
| 275 | + } | |
| 276 | + } | |
| 277 | + } | |
| 278 | + else //not _CXM_GET_FILE_REF | |
| 279 | + false | |
| 280 | +. | ... | ... |