CXM_generic_client.anubis
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Created by PyramIDE.
* User: ricard
* Date: 02/02/2008
* Time: 11:47
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
read tools/basis.anubis
read system/muscle.anubis
read system/data_io.anubis
read system/convert.anubis
read system/string.anubis
read system/message_queue.anubis
read system/message_transceiver.anubis
read calexium_lib/CXM_message_constants.anubis
read calexium_lib/net_services/CXM_net_services.anubis
read calexium_lib/net_services/CXM_generic_protocol.anubis
// --Generic types---------------------------------------------------------------------
public type NetServiceAnswer:
netservice_error (Int32 cmd,
Int32 result_code,
String result_string),
netservice_ok (Int32 cmd,
Maybe(Message) result_msg).
// --Generic functions---------------------------------------------------------------------
public define Maybe(NetServiceAnswer)
generic_send_message
(
MessageQueue queue,
Message msg_to_send,
Int32 timeout,
(String) -> One logger
)=
queue.add_Message_to_send(msg_to_send);
if queue.get_next_received_Message(timeout) is
{
timeout then logger("["+queue.get_name(unique)+"]: receive timeout");failure,
closed then logger("["+queue.get_name(unique)+"]: socket closed");failure,
msg(msg) then
if find_int32(msg, "CMD") is
{
failure then logger("["+queue.get_name(unique)+"]: CMD field not found"); failure,
success(cmd) then
if find_int32(msg, "STATUS") is
{
failure then logger("["+queue.get_name(unique)+"]: STATUS field not found"); failure,
success(v) then
if v = _CXM_OK then
success(netservice_ok(cmd, find_message(msg, "RESULT")))
else
with error_string = if find_string(msg, "STATUS_STR") is success(s) then s else "",
success(netservice_error(cmd, v, error_string))
}
}
}.
public define Maybe($T)
generic_handler
(
MessageQueue queue,
Int32 timeout,
Message msg_to_send,
(Message) -> Maybe($T) handler,
(String) -> One logger
)=
queue.add_Message_to_send(msg_to_send);
if queue.get_next_received_Message(timeout) is
{
timeout then logger("["+queue.get_name(unique)+"]: receive timeout");failure,
closed then logger("["+queue.get_name(unique)+"]: socket closed");failure,
msg(msg) then
if find_int32(msg, "STATUS") is
{
failure then logger("["+queue.get_name(unique)+"]: status not found");failure,
success(v) then
if v = _CXM_OK then
//println("generic_handler("+queue.get_name(unique)+"): message status ok");
if find_message(msg, "RESULT") is
{
failure then logger("["+queue.get_name(unique)+"]: can't find RESULT");failure,
success(result) then handler(result)
}
else
logger("["+queue.get_name(unique)+"]: message status ERROR");failure
}
}.
public define (MessageQueue, String) -> Maybe($T)
make_generic_handler
(
Int32 timeout,
Message msg_to_send,
(Message) -> Maybe($T) handler,
(String) -> One logger
) =
(MessageQueue queue, String timestamp) |->
generic_handler(queue, timeout, msg_to_send, handler, logger).
define Maybe($T)
generic_request_for_service
(
MessageQueue queue,
Int32 service_id,
Int32 service_version,
(MessageQueue, String) -> Maybe($T) handler,
(String) -> One logger
)=
with test_msg = message(_CXM_REQUEST_FOR_SERVICE),
forget(add_int32(test_msg, "SERVICE", service_id));
forget(add_int32(test_msg, "VERSION", service_version));
queue.add_Message_to_send(test_msg);
if queue.get_next_received_Message(10) is
{
timeout then logger("["+queue.get_name(unique)+"]: requesting service receive timeout");failure,
closed then logger("["+queue.get_name(unique)+"]: requesting service socket closed");failure,
msg(msg) then
if find_int32(msg, "STATUS") is
{
failure then logger("["+queue.get_name(unique)+"]: requesting service STATUS not found");failure,
success(v) then
if v = _CXM_OK then
if find_message(msg, "RESULT") is
{
failure then logger("["+queue.get_name(unique)+"]: requesting service RESULT not found");failure,
success(result) then
with timestamp = if find_string(result, "TIMESTAMP") is
{
failure then logger("["+queue.get_name(unique)+"]: requesting service TIMESTAMP not found"); "",
success(timestamp) then timestamp
},
handler(queue, timestamp)
}
else
logger("["+queue.get_name(unique)+"]: requested service started won't start");failure
}
}.
public define Maybe($T)
generic_connect_to_net_service
(
String queue_name,
Int32 server,
Int32 port,
Int32 service_id,
Int32 service_version,
(MessageQueue, String) -> Maybe($T) handler,
(String) -> One logger
)
=
if connect( server, port) is
{
error(_) then logger(queue_name + ": Can't connect to domain manager ["+ip_addr_to_string(server)+":"+port+"]");failure,
ok(conn) then
with queue = create_MessageQueue(queue_name),
message_transceiver(conn, queue);
with result = generic_request_for_service(queue, service_id, service_version, handler, logger),
queue.quit(unique);
//logInfo(debug_log,"domain_manager client quit");
result
}.