-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCI2000Connection.cs
More file actions
431 lines (382 loc) · 13.7 KB
/
BCI2000Connection.cs
File metadata and controls
431 lines (382 loc) · 13.7 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
///////////////////////////////////////////////////////////////////////
// Author: tytbutler@yahoo.com
// Description: A class for connecting to BCI2000 Remotely.
// See BCI2000Remote for a more in-depth description
//
// Adapted from the C++ BCI2000Connection
//
//
// (C) 2000-2021, BCI2000 Project
// http://www.bci2000.org
///////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Text;
using System.Net.Sockets;
namespace BCI2000RemoteNET
{
public class BCI2000Connection
{
private const int defaultTimeout = 1000;
private const string defaultTelnetIp = "127.0.0.1";
private const Int32 defaultTelnetPort = 3999;
private const int defaultWindowVisible = 1;
private const string defaultWindowTitle = "";
private const string defaultLogFile = "remoteLog.txt";
private const bool defaultLogStates = false;
private const bool defaultLogPrompts = false;
private TcpClient tcp;
//response stuff
private const string ReadlineTag = "\\AwaitingInput:";
private const string AckTag = "\\AcknowledgedInput";
private const string ExitCodeTag = "\\ExitCode";
private const string TerminationTag = "\\Terminating";
private const string Prompt = ">";
private string logFile;
public string LogFile
{
get
{
return logFile;
}
set
{
logFile = value;
if (Log == null)
return;
if (Log != null)
Log.Close();
}
}
protected StreamWriter Log { get; set; }
private bool LastLogState { get; set; } //was the last thing sent a command to set state
public bool LogStates { get; set; } //sets whether to log commands to set state, along with the received prompts afterwards
public bool LogPrompts { get; set; } //sets whether to log all received prompts
//changes to these will only take effect on Connect()
public int Timeout { get; set; } //send and recieve timeout in ms
public string TelnetIp { get; set; }
public Int32 TelnetPort { get; set; }
public string OperatorPath { get; set; }
private string result; //three properties for results and logging
public string Result
{
get
{
return result;
}
protected set
{
result = value;
WriteLog("Result: ", value);
}
}
private string sending;
public string Sending
{
get
{
return sending;
}
set
{
sending = value;
if (value.IndexOf("set state", StringComparison.OrdinalIgnoreCase) >= 0)
{
LastLogState = true;
if (!LogStates)// if logging states is disabled
return;
}
WriteLog("Sent: ", value);
}
}
private string received;
public string Received
{
get
{
return received;
}
private set
{
received = value;
if (value.IndexOf(Prompt) == 0)
{
if (LastLogState)
{
LastLogState = false;
if (!LogStates) //don't log prompts as received from 'set state'
{
return;
}
}
if (!LogPrompts) //don't log prompts
return;
}
WriteLog("Received: ", value);
}
}
public string Response { get; protected set; }
//result is set by methods in this class, response is the response from BCI2000, as Execute() shouldn't overwrite Result
private bool TerminateOperator { get; set; }
//Changes to these will result in a call to the Operator if connected
private string windowTitle;
public string WindowTitle
{
get
{
return windowTitle;
}
set
{
windowTitle = value;
if (Connected())
Execute("set title \"" + value + "\"");
}
}
private int windowVisible;
public int WindowVisible
{
get
{
return windowVisible;
}
set
{
windowVisible = value;
if (Connected())
{
if (value == 0)
Execute("hide window");
if (value == 1)
Execute("show window");
}
}
}
public BCI2000Connection()
{
Timeout = defaultTimeout;
TelnetIp = defaultTelnetIp;
TelnetPort = defaultTelnetPort;
WindowVisible = defaultWindowVisible;
WindowTitle = defaultWindowTitle;
LogFile = defaultLogFile;
LogStates = defaultLogStates;
LogPrompts = defaultLogPrompts;
}
//Ends connection to operator, terminates operator if it was started by a previous Connect() call
public bool Disconnect()
{
Result = "";
if (TerminateOperator)
{
TerminateOperator = false;
if (Connected())
Quit();
}
if (tcp != null)
tcp.Close();
return true;
}
public virtual bool Connect() //Connects to operator module, starts operator if not running
{
if (String.IsNullOrEmpty(TelnetIp))
TelnetIp = defaultTelnetIp;
if (TelnetPort == 0)
TelnetPort = defaultTelnetPort;
tcp = new TcpClient();
bool success = true;
try
{
tcp.Connect(TelnetIp, TelnetPort);
}
catch (SocketException ex)
{
Result = "Failed to connect to " + TelnetIp + ":" + TelnetPort + ", " + ex.Message;
success = false;
}
if (!success && (String.IsNullOrEmpty(OperatorPath)))
{
Result = "Failed to connect to " + TelnetIp + ":" + TelnetPort;
return false;
}
if (!success) //tcp has not connected to running operator, so it will try to open a new operator and connect
{
tcp.Close();
tcp = new TcpClient();
try //run operator, must be local
{
StringBuilder arguments = new StringBuilder();
arguments.Append("--Telnet \"127.0.0.1" + ':' + TelnetPort.ToString() + "\" ");
arguments.Append("--StartupIdle ");
if (WindowTitle != "" && WindowTitle != null)
arguments.Append("--Title \"" + WindowTitle + "\" ");
if (WindowVisible != 1)
arguments.Append("--Hide ");
System.Diagnostics.Process.Start(OperatorPath, arguments.ToString());
}
catch (InvalidOperationException)
{
Result = "Failed to run operator at " + OperatorPath;
return false;
}
try//connect to started operator
{
tcp.Connect("127.0.0.1", TelnetPort);
}
catch (SocketException ex)
{
Result = "Failed to connect to operator at 127.0.0.1:" + TelnetPort + ", " + ex.Message;
return false;
}
TerminateOperator = true;
}
tcp.SendTimeout = Timeout;
tcp.ReceiveTimeout = Timeout;
WindowTitle = windowTitle;
WindowVisible = windowVisible;
Execute("change directory $BCI2000LAUNCHDIR");
return true;
}
public bool Execute(string command)
{
int unused = 0;
return Execute(command, ref unused);
}
public bool Execute(string command, ref int outCode)
{
Result = "";
if (!Connected())
{
Result = "Not connected, call BCI2000Connection.Connect() to connect.";
return false;
}
string responseUnprocessed;
Sending = command;
try
{
tcp.Client.Send(stringToBytes(command + "\r\n"));
Byte[] responseData = new Byte[1024];
tcp.Client.Receive(responseData);
responseUnprocessed = bytesToString(responseData);
}
catch (SocketException ex)
{
Result = "SocketException: " + ex + ", socket error code " + ex.SocketErrorCode;
return false;
}
Received = responseUnprocessed;
return ProcessResponse(responseUnprocessed, ref outCode);
}
public bool ProcessResponse(string responseUnprocessed, ref int outCode)
{
Response = "";
byte[] buffer = new byte[1024];
while (Connected() && (!(responseUnprocessed.IndexOf(Prompt, StringComparison.OrdinalIgnoreCase) >= 0)) || tcp.Client.Available > 0)
{
if (responseUnprocessed.Contains(ReadlineTag))
{
string input = "";
if (OnInput())
{
tcp.Client.Send(stringToBytes(input + "\r\n"));
Byte[] recvBuf = new byte[1024];
tcp.Client.Receive(recvBuf);
if (!bytesToString(recvBuf).Contains(AckTag))
{
Result = "Did not receive input acknowledgement";
return false;
}
}
else
{
Result = "Could not handle request for input: Override BCI2000Connection.OnInput to handle input";
return false;
}
}
else if (responseUnprocessed.Contains(ExitCodeTag))
{
outCode = responseUnprocessed.Length;
}
else if (responseUnprocessed.Contains(TerminationTag))
{
tcp.Client.Close();
tcp.Close();
TerminateOperator = false;
return true;
}
else
{
if (!OnOutput(responseUnprocessed))
{
Response = responseUnprocessed;
}
if (responseUnprocessed.IndexOf("true", StringComparison.OrdinalIgnoreCase) >= 0)//this is rewritten from the original code, but changed so that 1 reflects true and 0 reflects false
outCode = 1;
else if (responseUnprocessed.IndexOf("false", StringComparison.OrdinalIgnoreCase) >= 0)
outCode = 0;
else if (String.IsNullOrWhiteSpace(responseUnprocessed))
outCode = 1;
else
outCode = -1;
}
Array.Clear(buffer, 0, 1024);
tcp.Client.Receive(buffer);
responseUnprocessed = responseUnprocessed + bytesToString(buffer);
}
if (!Connected())
{
Result = "Lost Connection to BCI2000";
return false;
}
if (responseUnprocessed.IndexOf(Prompt, StringComparison.OrdinalIgnoreCase) >= 0)
{
Response = responseUnprocessed;
outCode = 1;
return true;
}
return true;
}
public bool Connected()
{
if (tcp != null)
return tcp.Connected;
else
return false;
}
public bool Quit()
{
Execute("quit");
return String.IsNullOrWhiteSpace(Response);
}
public virtual bool OnInput()//input and output handler methods to be overridden
{
return false;
}
public virtual bool OnOutput(string output)
{
return false;
}
public void WriteLog(string toLog)//for writing to log from outside the class
{
WriteLog("External: ", toLog);
}
private void WriteLog(string logPreface, string toLog)
{
if (Log == null)
Log = new StreamWriter(LogFile);
if (Log != null && !String.IsNullOrWhiteSpace(toLog))
{
Log.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + ' ' + logPreface + ' ' + toLog);
Log.Flush();
}
}
private Byte[] stringToBytes(string str)//utility methods
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
private string bytesToString(Byte[] bytes)
{
return System.Text.Encoding.ASCII.GetString(bytes).Replace("\0", String.Empty);
}
}
}