Commit 5e43e6206af42a03d6c38b0c858e1fd81cd4d1c0
1 parent
54906678
adding logger_service.anubis
Showing
1 changed file
with
353 additions
and
0 deletions
Show diff stats
calexium_lib/net_services_protocols/logger_service.anubis
0 → 100644
| 1 | +/* | ||
| 2 | + * | ||
| 3 | + * User: David RENE | ||
| 4 | + * Date: 29/07/2007 | ||
| 5 | + * Time: 02:04 | ||
| 6 | + * (c) Calexium | ||
| 7 | + * | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +read tools/basis.anubis | ||
| 11 | +read system/muscle.anubis | ||
| 12 | +read system/data_io.anubis | ||
| 13 | +read system/convert.anubis | ||
| 14 | +read system/string.anubis | ||
| 15 | +read system/logger.anubis | ||
| 16 | +read system/message_queue.anubis | ||
| 17 | +read system/message_transceiver.anubis | ||
| 18 | + | ||
| 19 | +read calexium_lib/CXM_message_constants.anubis | ||
| 20 | +read calexium_lib/net_services/CXM_net_services.anubis | ||
| 21 | +read calexium_lib/net_services/CXM_generic_protocol.anubis | ||
| 22 | + | ||
| 23 | +//read NetBox_types.anubis | ||
| 24 | +//read mailfountain_types.anubis | ||
| 25 | +//read tools/mf_loggers.anubis | ||
| 26 | + | ||
| 27 | +public define Int32 logger_srv_version = 1. | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * has_update scan the given message for finding corresponding available update | ||
| 31 | + * package for the application. The request message come with AppId field which contain | ||
| 32 | + * package or application signature and CurrentVersion field which is current version | ||
| 33 | + * installed on the requester. | ||
| 34 | + * | ||
| 35 | + * --- REQUEST message format | ||
| 36 | + * msg [_CXM_UPD_HAS_UPDATE] | ||
| 37 | + * string "AppId" = hash or unique signature of the package | ||
| 38 | + * string "CurrentVersion" = current version installed on the remote caller | ||
| 39 | + * | ||
| 40 | + * --- ANSWER message format | ||
| 41 | + * | ||
| 42 | + * ERROR case message format | ||
| 43 | + * msg [_CXM_ACK] | ||
| 44 | + * int32 "CMD" = _CXM_UPD_HAS_UPDATE | ||
| 45 | + * int32 "STATUS" = _CXM_ERROR | ||
| 46 | + * | ||
| 47 | + * OK without update available | ||
| 48 | + * msg [_CXM_ACK] | ||
| 49 | + * int32 "CMD" = _CXM_UPD_HAS_UPDATE | ||
| 50 | + * int32 "STATUS" = _CXM_OK | ||
| 51 | + * msg "RESULT" [_CXM_ACK_RESULT_MSG] | ||
| 52 | + * int32 "PackageStatus" = no_update // numerical value = 0 | ||
| 53 | + * | ||
| 54 | + */ | ||
| 55 | + | ||
| 56 | +define Bool | ||
| 57 | + process_log | ||
| 58 | + ( | ||
| 59 | + Logger log, | ||
| 60 | + Message msg | ||
| 61 | + )= | ||
| 62 | + if find_string(msg, "LogString") is | ||
| 63 | + { | ||
| 64 | + failure then println("process_log can't find LogString");false, | ||
| 65 | + success(log_string) then | ||
| 66 | + if find_int32(msg, "Level") is | ||
| 67 | + { | ||
| 68 | + failure then println("process_log can't find Level");false, | ||
| 69 | + success(int_level) then | ||
| 70 | + if find_int32(msg, "Thread") is | ||
| 71 | + { | ||
| 72 | + failure then println("process_log can't find Level");false, | ||
| 73 | + success(thread_id) then | ||
| 74 | + with level = get_LogLevel_from_value(int_level), | ||
| 75 | + if level is logTrace then | ||
| 76 | + if find_string(msg, "LogMask") is | ||
| 77 | + { | ||
| 78 | + failure then println("process_log can't find LogMask");false, | ||
| 79 | + success(log_mask) then logTrace(log, logMask(log_mask), log_string, thread_id); true | ||
| 80 | + } | ||
| 81 | + else doLog(log, log_string, level, thread_id); true | ||
| 82 | +// { | ||
| 83 | +// logNone then true, | ||
| 84 | +// logCriticalError then logCriticalError(log, log_string); true, | ||
| 85 | +// logError then logError(log, log_string); true, | ||
| 86 | +// logWarning then logWarning(log, log_string); true, | ||
| 87 | +// logInfo then logInfo(log, log_string); true, | ||
| 88 | +// logDebug then logDebug(log, log_string); true | ||
| 89 | +// logTrace then | ||
| 90 | +// if find_string(msg, "LogMask") is | ||
| 91 | +// { | ||
| 92 | +// failure then println("process_log can't find LogMask");false, | ||
| 93 | +// success(log_mask) then logTrace(log, logMask(log_mask),log_string); true | ||
| 94 | +// } | ||
| 95 | +// } | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + }. | ||
| 99 | + | ||
| 100 | +define Bool | ||
| 101 | + process_log | ||
| 102 | + ( | ||
| 103 | + List(Logger) logs, | ||
| 104 | + Message msg | ||
| 105 | + )= | ||
| 106 | + if find_string(msg, "LogName") is | ||
| 107 | + { | ||
| 108 | + failure then println("process_log can't find LogName");false, | ||
| 109 | + success(log_name) then | ||
| 110 | + if get_logger(logs, log_name) is | ||
| 111 | + { | ||
| 112 | + failure then println("can't find log ["+log_name+"]");false, | ||
| 113 | + success(log) then process_log(log, msg) | ||
| 114 | + } | ||
| 115 | + }. | ||
| 116 | + | ||
| 117 | + /** message_received is the principal | ||
| 118 | + */ | ||
| 119 | + /* | ||
| 120 | + define One | ||
| 121 | + message_received | ||
| 122 | + ( | ||
| 123 | + MessageQueue queue, | ||
| 124 | + List(Logger) logs, | ||
| 125 | + Message msg | ||
| 126 | + )= | ||
| 127 | + with msg_what = *msg.what, | ||
| 128 | + if msg_what = _CXM_LOGGER_LOG then | ||
| 129 | + send_result(queue, _CXM_LOGGER_LOG, process_log(logs, msg)) | ||
| 130 | + else | ||
| 131 | + println("wrong message_received is not _CXM_LOGGER_LOG ");unique | ||
| 132 | + . | ||
| 133 | + | ||
| 134 | + define One | ||
| 135 | + logger_handler | ||
| 136 | + ( | ||
| 137 | + MessageQueue queue, | ||
| 138 | + List(Logger) logs | ||
| 139 | + )= | ||
| 140 | + if queue.get_next_received_Message(1) is | ||
| 141 | + { | ||
| 142 | + timeout then //println("logger_handler timeout ["+queue.get_name(unique)+"]"); | ||
| 143 | + logger_handler(queue, logs), | ||
| 144 | + closed then //println("logger_handler closed ["+queue.get_name(unique)+"]"); | ||
| 145 | + unique, | ||
| 146 | + msg(msg) then //println("logger_handler msg received ["+queue.get_name(unique)+"]"); | ||
| 147 | + message_received(queue, logs, msg); | ||
| 148 | + logger_handler(queue, logs) | ||
| 149 | + }. | ||
| 150 | + | ||
| 151 | + public define NetService | ||
| 152 | + logger_service | ||
| 153 | + ( | ||
| 154 | + List(Logger) logs | ||
| 155 | + )= | ||
| 156 | + net_service( | ||
| 157 | + logger_srv_version, | ||
| 158 | + _CXM_LOGGER_SERVICE_ID, //id of the service declared in CXM_message_constants | ||
| 159 | + "Logger service", //human readable name of the service | ||
| 160 | + ( | ||
| 161 | + MessageQueue queue, | ||
| 162 | + String peer | ||
| 163 | + ) |-> logger_handler(queue, logs)). | ||
| 164 | + | ||
| 165 | + */ | ||
| 166 | + /****************** LOCAL UDP SERVICE *************/ | ||
| 167 | + | ||
| 168 | + | ||
| 169 | +public define String | ||
| 170 | + data_preview | ||
| 171 | + ( | ||
| 172 | + ByteArray data, | ||
| 173 | + Int32 max | ||
| 174 | + ) = | ||
| 175 | + with l = length(data), | ||
| 176 | + firsts_bytes = extract(data, 0, max), | ||
| 177 | + "Length=" + l + "; String='"+to_string(firsts_bytes)+"' Buffer=[" + to_ascii(firsts_bytes) + (if l > max then "...]" else "]"). | ||
| 178 | + | ||
| 179 | + | ||
| 180 | +define (UDP_Socket, ByteArray, Truncation, Int32, Int32) -> One | ||
| 181 | + make_udp_handler | ||
| 182 | + ( | ||
| 183 | + List(Logger) logs, | ||
| 184 | + Logger debug_logger | ||
| 185 | + ) = | ||
| 186 | + ( | ||
| 187 | + UDP_Socket socket, | ||
| 188 | + ByteArray data, | ||
| 189 | + Truncation truncation, | ||
| 190 | + Int32 ip_address, | ||
| 191 | + Int32 ip_port | ||
| 192 | + ) |-> | ||
| 193 | + with ip = ip_addr_to_string(ip_address), | ||
| 194 | + logTrace(debug_logger, logMask("Logger"), "LoggerServer: data received (" + length(data) + " bytes) from IP " + ip + "... "); | ||
| 195 | + if receive_message_from_io(make_data_io(data)) is | ||
| 196 | + { | ||
| 197 | + failure then logWarning(debug_logger, "LoggerServer: Bad data packet received from IP " + ip + ": " + data_preview(data, 20)), | ||
| 198 | + success(msg) then | ||
| 199 | + if *msg.what = _CXM_LOGGER_LOG then | ||
| 200 | + forget(process_log(logs, msg)) | ||
| 201 | + else | ||
| 202 | + println("wrong message_received is not _CXM_LOGGER_LOG ") | ||
| 203 | + }. | ||
| 204 | + | ||
| 205 | +define One | ||
| 206 | + my_notify | ||
| 207 | + ( | ||
| 208 | + One dummy | ||
| 209 | + ) = | ||
| 210 | + unique. | ||
| 211 | + | ||
| 212 | +public define Maybe(UDP_Server) | ||
| 213 | + start_logger_server | ||
| 214 | + ( | ||
| 215 | + List(Logger) logs, | ||
| 216 | + Logger debug_logger | ||
| 217 | + ) = | ||
| 218 | + if start_udp_server(0, | ||
| 219 | + 33610, | ||
| 220 | + make_udp_handler(logs, debug_logger), | ||
| 221 | + 8*1024, | ||
| 222 | + my_notify) is | ||
| 223 | + { | ||
| 224 | + cannot_create_the_socket then logError(debug_logger, "Cannot create the listening socket."); failure, | ||
| 225 | + cannot_bind_address_port then logError(debug_logger, "Cannot bind to port 33610.");failure, | ||
| 226 | + access_denied then logError(debug_logger, "Cannot listen on port 33610.");failure, | ||
| 227 | + ok(server) then logInfo(debug_logger, "LocalLog Server started on port 33610."); success(server) | ||
| 228 | + }. | ||
| 229 | + | ||
| 230 | + | ||
| 231 | + /****************** CLIENT PART *******************/ | ||
| 232 | + | ||
| 233 | +/* | ||
| 234 | + define One | ||
| 235 | + send_log_msg | ||
| 236 | + ( | ||
| 237 | + MessageQueue queue, | ||
| 238 | + Message log_msg | ||
| 239 | + )= | ||
| 240 | + with test_msg = message(_CXM_REQUEST_FOR_SERVICE), | ||
| 241 | + forget(add_int32(test_msg, "service", _CXM_LOGGER_SERVICE_ID)); | ||
| 242 | + forget(add_int32(test_msg, "version", 1)); | ||
| 243 | + queue.add_Message_to_send(test_msg); | ||
| 244 | + if queue.get_next_received_Message(30) is | ||
| 245 | + { | ||
| 246 | + timeout then println("send_log_msg receive timeout"), | ||
| 247 | + closed then println("send_log_msg socket closed"), | ||
| 248 | + msg(msg) then | ||
| 249 | + if find_int32(msg, "STATUS") is | ||
| 250 | + { | ||
| 251 | + failure then println("status not found"), | ||
| 252 | + success(v) then | ||
| 253 | + if v = _CXM_OK then | ||
| 254 | + queue.add_Message_to_send(log_msg); | ||
| 255 | + forget(wait_for_reply(queue, _CXM_LOGGER_LOG, 30)) | ||
| 256 | + else | ||
| 257 | + println("requested service started won't start") | ||
| 258 | + } | ||
| 259 | + }. | ||
| 260 | +*/ | ||
| 261 | + | ||
| 262 | +define One | ||
| 263 | + local_net_logger | ||
| 264 | + ( | ||
| 265 | + String logger_server, | ||
| 266 | + Message log_message | ||
| 267 | + )= | ||
| 268 | + forget(send_message_by_udp(log_message, ip_address((127,0,0,1)), 33610)). | ||
| 269 | + | ||
| 270 | +/* if find_string(log_message, "LogString") is | ||
| 271 | + { | ||
| 272 | + failure then println("process_log can't find LogString"), | ||
| 273 | + success(log_string) then println(log_string) | ||
| 274 | + }. | ||
| 275 | + if connect( ip_address((127,0,0,1)), 33125) is | ||
| 276 | + { | ||
| 277 | + error(_) then println("can't connect to logger server ["+logger_server+"]"); | ||
| 278 | + forget(process_log(fullLogger, log_message)), | ||
| 279 | + ok(conn) then | ||
| 280 | + with queue = create_MessageQueue("net logger sender"), | ||
| 281 | + message_transceiver(conn, queue); | ||
| 282 | + send_log_msg(queue, log_message); | ||
| 283 | + queue.quit(unique) | ||
| 284 | + }.*/ | ||
| 285 | + | ||
| 286 | +define Message | ||
| 287 | + create_log_msg | ||
| 288 | + ( | ||
| 289 | + String logger_name, | ||
| 290 | + String log_string, | ||
| 291 | + Int32 level | ||
| 292 | + )= | ||
| 293 | + with string = if length(log_string) > 1024 then (if sub_string(log_string, 0, 1024) is success(s) then s else "?") + "..." | ||
| 294 | + else log_string, | ||
| 295 | + with log_msg = message(_CXM_LOGGER_LOG), | ||
| 296 | + forget(add_string(log_msg, "LogName", logger_name)); | ||
| 297 | + forget(add_string(log_msg, "LogString", string)); | ||
| 298 | + forget(add_int32(log_msg, "Level", level)); | ||
| 299 | + forget(add_int32(log_msg, "Thread", virtual_machine_id)); | ||
| 300 | + log_msg | ||
| 301 | + . | ||
| 302 | + | ||
| 303 | +public define One | ||
| 304 | + logNone( | ||
| 305 | + String logger_name, | ||
| 306 | + String log_string | ||
| 307 | + )= | ||
| 308 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 0)). | ||
| 309 | + | ||
| 310 | +public define One | ||
| 311 | + logCriticalError( | ||
| 312 | + String logger_name, | ||
| 313 | + String log_string | ||
| 314 | + )= | ||
| 315 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 1)). | ||
| 316 | + | ||
| 317 | +public define One | ||
| 318 | + logError( | ||
| 319 | + String logger_name, | ||
| 320 | + String log_string | ||
| 321 | + )= | ||
| 322 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 2)). | ||
| 323 | + | ||
| 324 | +public define One | ||
| 325 | + logWarning( | ||
| 326 | + String logger_name, | ||
| 327 | + String log_string | ||
| 328 | + )= | ||
| 329 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 3)). | ||
| 330 | + | ||
| 331 | +public define One | ||
| 332 | + logInfo( | ||
| 333 | + String logger_name, | ||
| 334 | + String log_string | ||
| 335 | + )= | ||
| 336 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 4)). | ||
| 337 | + | ||
| 338 | +public define One | ||
| 339 | + logDebug( | ||
| 340 | + String logger_name, | ||
| 341 | + String log_string | ||
| 342 | + )= | ||
| 343 | + local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 5)). | ||
| 344 | + | ||
| 345 | +public define One | ||
| 346 | + logTrace( | ||
| 347 | + String logger_name, | ||
| 348 | + LogMask log_mask, | ||
| 349 | + String log_string | ||
| 350 | + )= | ||
| 351 | + with log_msg = create_log_msg(logger_name, log_string, 6), | ||
| 352 | + forget(add_string(log_msg, "LogMask", log_mask.mask)); | ||
| 353 | + local_net_logger("127.0.0.1", log_msg). |