-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathamqp.re
181 lines (171 loc) · 4.42 KB
/
amqp.re
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
169
170
171
172
173
174
175
176
177
178
179
180
181
# rules for amqp
# requires python and pika
# send a msg
amqpSend: string * string * ? -> integer
amqpSend(*Host, *Queue, *Msg) {
writeLine("serverLog", "ampqSend: sending message '*Msg'");
*HostArg = execCmdArg("amqp://*Host/*Queue");
*MsgArg = execCmdArg(*Msg);
msiExecCmd("amqpsend.py", "*HostArg *MsgArg", "null", "null", "null", *Out);
}
# receive a msg
amqpRecv: string * string * output boolean * output string -> integer
amqpRecv(*Host, *Queue, *Emp, *Msg) {
*HostArg = execCmdArg(*Host);
*QueueArg = execCmdArg(*Queue);
msiExecCmd("amqprecv.py", "*HostArg *QueueArg", "null", "null", "null", *Out);
msiGetStdoutInExecCmdOut(*Out, *Msg);
*Emp = strlen(*Msg) == 0;
if(!*Emp) {
*Msg = trimr(*Msg, "\n");
}
}
# Xmsg to AMQP bridge
# Messages are of the format "Host:Queue:Msg", assuming that there is no ":" in Host or Queue
startXmsgAmqpBridge(*Tic, *Log) {
delay("<EF>30s</EF>") {
XmsgAmqpBridge(*Tic, *Log);
}}
XmsgAmqpBridge(*Tic, *Log) {
*Found = false;
foreach(*A in listcorerules()) {
if(*A == "amqpSend") {
*Found = true;
break;
}
}
foreach(*A in listapprules()) {
if(*A == "amqpSend") {
*Found = true;
break;
}
}
if(!*Found) {
msiAdmAddAppRuleStruct("amqp", "", "");
}
# msiXmsgServerConnect(*Conn);
while(true) {
if(*Log) {
writeLine("serverLog", "waiting for message with ticket *Tic");
}
*ErrorCode = errorcode(readXMsg(str(*Tic), "", *MNum, *SNum, *MHdr, *XMsg, *MUser, *MAddr));
if(*ErrorCode < 0) {
if(*ErrorCode == -63000) {
writeLine("serverLog", "no more xmessages");
break;
} else {
fail(*ErrorCode);
}
} else {
if(*Log) {
writeLine("serverLog", "received xmessage '*XMsg'");
}
*QueueMsg = triml(*XMsg, ":");
*Host = substr(*XMsg, 0, strlen(*XMsg) - strlen(*QueueMsg) - 1);
*Msg = triml(*QueueMsg, ":");
*Queue = substr(*QueueMsg, 0, strlen(*QueueMsg) - strlen(*Msg) - 1);
if(*Log) {
writeLine("serverLog", "sending amqp message '*Host:*Queue:*Msg'");
}
amqpSend(*Host, *Queue, *Msg);
}
}
# msiXmsgServerDisConnect(*Conn);
}
@("logging", "false")
# AMQP to Xmsg bridge
# Messages are read from *Queue on *Host, and written to stream with ticket *Tic
startAmqpXmsgBridge(*Host, *Queue, *Tic, *Log) {
delay("<EF>30s</EF>") {
AmqpXmsgBridge(*Host, *Queue, *Tic, *Log);
}
}
AmqpXmsgBridge(*Host, *Queue, *Tic, *Log) {
*Found = false;
foreach(*A in listcorerules()) {
if(*A == "amqpRecv") {
*Found = true;
break;
}
}
foreach(*A in listapprules()) {
if(*A == "amqpRecv") {
*Found = true;
break;
}
}
if(!*Found) {
msiAdmAddAppRuleStruct("amqp", "", "");
}
while(true) {
if(*Log) {
writeLine("serverLog", "waiting for message from *Host:*Queue");
}
*ErrorCode = errorcode(amqpRecv(*Host, *Queue, *Emp, *Msg));
if(*ErrorCode < 0) {
writeLine("serverLog", "AMQP receive error");
fail(*ErrorCode);
} else if(*Emp) {
if(*Log) {
writeLine("serverLog", "no AMQP message");
}
break;
} else {
if(*Log) {
writeLine("serverLog", "received AMQP message '*Msg'");
}
if(*Log) {
writeLine("serverLog", "sending Xmessage '*Msg' with ticket *Tic");
}
writeXMsg(str(*Tic), *Queue, *Msg);
}
}
# msiXmsgServerDisConnect(*Conn);
}
@("logging", "false")
# Xmsg to AMQP bridge which sents all Xmsgs from a channel to a queue
startXmsgAmqpBridgeOneQueue(*Tic, *Host, *Queue, *Log) {
delay("<EF>30s</EF>") {
XmsgAmqpBridgeOneQueue(*Tic, *Host, *Queue, *Log);
}
}
XmsgAmqpBridgeOneQueue(*Tic, *Host, *Queue, *Log) {
*Found = false;
foreach(*A in listcorerules()) {
if(*A == "amqpSend") {
*Found = true;
break;
}
}
foreach(*A in listapprules()) {
if(*A == "amqpSend") {
*Found = true;
break;
}
}
if(!*Found) {
msiAdmAddAppRuleStruct("amqp", "", "");
}
# msiXmsgServerConnect(*Conn);
while(true) {
if(*Log) {
writeLine("serverLog", "waiting for message with ticket *Tic");
}
*ErrorCode = errorcode(readXMsg(str(*Tic), "", *MNum, *SNum, *MHdr, *XMsg, *MUser, *MAddr));
if(*ErrorCode < 0) {
if(*ErrorCode == -63000) {
writeLine("serverLog", "no more xmessages");
break;
} else {
fail(*ErrorCode);
}
} else {
if(*Log) {
writeLine("serverLog", "received xmessage '*XMsg'");
}
amqpSend(*Host, *Queue, *XMsg);
}
}
# msiXmsgServerDisConnect(*Conn);
}
@("logging", "false")