-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kqml.cs
264 lines (227 loc) · 10.6 KB
/
Kqml.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NU.Kiosk.SharedObject;
using log4net;
using System.Reflection;
using System.Threading;
namespace NU.Kqml
{
using Microsoft.Psi;
using Microsoft.Psi.Components;
public class SocketEchoer
{
private readonly string facilitatorIp;
private readonly int facilitatorPort;
private readonly int localPort;
//private readonly string name = "echoer";
//private bool ready = false;
private int messageCounter = 0;
private SimpleSocketServer listener;
private SimpleSocket facilitator = null;
public SocketEchoer(string facilitatorIp = "127.0.0.1", int facilitatorPort = 9000, int localPort = 6000)
{
this.facilitatorIp = facilitatorIp;
this.facilitatorPort = facilitatorPort;
this.localPort = localPort;
// start listening
this.listener = new SimpleSocketServer(this.localPort);
this.listener.OnMessage = this.ProcessMessageFromUpstream; // push the data downstream
this.listener.StartListening();
facilitator = new SimpleSocket(this.facilitatorIp, facilitatorPort);
facilitator.OnMessage = this.ProcessMessageFromUpstream;
//this.ready = true;
// register with facilitator
//facilitator = new SimpleSocket(this.facilitatorIp, facilitatorPort);
//facilitator.OnMessage = this.ProcessMessageFromUpstream;
//facilitator.Connect();
//var registermsg = $"(register :sender {this.name} :receiver facilitator :content (\"socket://127.0.0.1:{this.localPort}\" nil nil {this.localPort}))";
//facilitator.Send(registermsg);
//facilitator.Close();
}
private void ProcessMessageFromUpstream(string data, AbstractSimpleSocket socket)
{
KQMLMessage msg = (new KQMLMessageParser()).parse(data);
socket.Send(KQMLMessage.createTell(msg.receiver, msg.sender, msg.reply_with, "echoer", ":ok").ToString());
if (msg.performative != "tell" && msg.performative != "register")
{
Console.WriteLine($"[Echoer] Recieved data: {data}.");// ; Parsed content: {msg.content}");
string temp = msg.receiver;
msg.receiver = msg.sender;
msg.sender = temp;
facilitator.Connect();
var outbound_msg = msg.ToString();
//Console.WriteLine($"[Echoer] Outbound message: {outbound_msg}");
facilitator.Send(outbound_msg);
facilitator.Close();
}
}
private String nextMsgId()
{
return $"echoer-id{this.messageCounter++}";
}
}
public class SocketStringConsumer : ConsumerProducer<string, NU.Kiosk.SharedObject.Action>
{
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly int localPort;
private readonly string facilitatorIp;
private readonly int facilitatorPort;
private readonly string default_achieve_destination;
private readonly Pipeline pipeline;
private bool ready = false;
private SimpleSocketServer listener;
private SimpleSocket facilitator;
private string name = "psi";
private int messageCounter = 0;
private List<KQMLMessage> receivedMsgs = new List<KQMLMessage>();
public SocketStringConsumer(Pipeline pipeline, string fIP, int fP, int localP, string default_achieve_destination = "interaction-manager")
: base(pipeline)
{
pipeline.RegisterPipelineStartHandler(this, this.OnPipelineStart);
this.pipeline = pipeline;
this.localPort = localP;
this.facilitatorIp = fIP;
this.facilitatorPort = fP;
this.default_achieve_destination = default_achieve_destination;
}
protected override void Receive(string message, Envelope e)
{
//Console.WriteLine($"[SocketStringConsumer] Consuming: {message}");
if (ready && message.Length > 5)
{
var kqml = KQMLMessage.createAchieve(name, this.default_achieve_destination, nextMsgId(), null, $"(task :action (processKioskUtterance \"{message}\"))");
//facilitator = new SimpleSocket(this.facilitatorIp, facilitatorPort);
//facilitator.OnMessage = this.ProcessMessageFromUpstream;
facilitator.Connect();
Console.WriteLine($"Sending: {kqml.ToString()}");
facilitator.Send(kqml.ToString());
//facilitator.Close();
}
}
public void OnPipelineStart()
{
// start listening
this.listener = new SimpleSocketServer(this.localPort);
this.listener.OnMessage = this.ProcessMessageFromUpstream; // push the data downstream
this.listener.StartListening();
// register with facilitator
facilitator = new SimpleSocket(this.facilitatorIp, facilitatorPort);
facilitator.OnMessage = this.ProcessMessageFromUpstream;
facilitator.Connect();
var registermsg = $"(register :sender {this.name} :receiver facilitator :content (\"socket://{this.facilitatorIp}:{this.localPort}\" nil nil {this.localPort}))";
//Console.WriteLine($"IP and port: '{this.facilitatorIp}, {facilitatorPort}, {this.localPort}'; Register Message: '{registermsg}'");
facilitator.Send(registermsg);
facilitator.Close();
// advertise - skipped
//string fn = "test";
//string msgid = nextMsgId();
//var admsg = $"(advertise :sender {this.name} :receiver facilitator :reply-with {msgid} " +
// $":content (ask-all :receiver {this.name} :in-reply-to {msgid} :content {fn}))";
//facilitator.Connect();
//facilitator.Send(admsg);
//facilitator.Close();
this.ready = true;
Console.WriteLine($"***Ready***\n");
}
public void Stop()
{
listener.Close();
}
private String nextMsgId()
{
return $"psi-id{this.messageCounter++}";
}
private void ProcessMessageFromUpstream(string data, AbstractSimpleSocket socket)
{
// push this into Out
Console.WriteLine($"Facilitator says: '{data}' - length {data.Length}");
if (data.Length > 3)
{
KQMLMessage kqml = (new KQMLMessageParser()).parse(data);
if (kqml != null && ready)
{
switch (kqml.performative)
{
case "ping":
case "common-lisp-user::ping":
handlePing(kqml, socket);
break;
case "achieve":
case "common-lisp-user::achieve":
handleAchieve(kqml, socket);
break;
case "tell":
case "common-lisp-user::tell":
handleTell(kqml, socket);
break;
case "error":
_log.Error($"Error: {kqml.ToString()}");
break;
case "ask_all":
case "ask_one":
case "advertise":
case "untell":
case "subscribe":
default:
_log.Error($"Unknown KQML Performative: {kqml.performative}");
break;
}
}
} else
{
_log.Warn($"Facilitator message ignored: {data}");
}
}
// performative handlers
private void handlePing(KQMLMessage msg, AbstractSimpleSocket socket)
{
socket.Send($"(update :sender {this.name} :receiver facilitator :in-reply-to {msg.reply_with} :content (:agent psi :uptime 12h :state guess))");
}
private void handleTell(KQMLMessage msg, AbstractSimpleSocket socket)
{
receivedMsgs.Add(msg); // is this really necessary?
Console.WriteLine($"Received tell message: {msg.ToString()}");
socket.Send(KQMLMessage.createTell(this.name, msg.sender, this.nextMsgId(), msg.reply_with, ":ok").ToString());
//this.Out.Post(msg.content.ToString(), DateTime.Now); // utilize... later
}
private void handleAchieve(KQMLMessage msg, AbstractSimpleSocket socket)
{
Console.WriteLine($"handleAchieve picked up a message: {msg.ToString()}");
receivedMsgs.Add(msg); // is this really necessary? --> change to logging in the future
if (msg.content is string || msg.content is KQMLMessage)
{
KQMLMessage msg_content;
if (msg.content is string)
{
msg_content = (new KQMLMessageParser()).parse((string)msg.content);
}
else {
msg_content = (KQMLMessage)msg.content;
}
// msg_content should be in the form of "(TheSet (psikiShowMap ...) (psikiSayText "...") ...)"
foreach (object o in msg_content.unaffiliated_obj_and_strings)
{
KQMLMessage individual_message = (KQMLMessage)o;
List<object> object_list = individual_message.unaffiliated_obj_and_strings;
List<string> string_list = new List<string>();
for (int j = 0, size = object_list.Count; j < size; j++)
{
var strr = object_list[j].ToString().Trim('"');
string_list.Add(strr);
}
var a = new NU.Kiosk.SharedObject.Action(individual_message.performative, string_list.ToArray<string>());
Thread.Sleep(1);
this.Out.Post(a, DateTime.Now);
socket.Send(KQMLMessage.createTell(this.name, msg.sender, this.nextMsgId(), msg.reply_with, ":ok").ToString());
}
}
else {
_log.Error($"Cannot handleAchieve: {msg.content} is of type {msg.content.GetType()}");
}
}
}
}