-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPLCServer.cs
172 lines (159 loc) · 5.32 KB
/
PLCServer.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
using System;
using System.Threading;
using NetMQ;
using NetMQ.Sockets;
using Newtonsoft.Json;
namespace mujinplccs
{
/// <summary>
/// A ZMQ server that hosts the PLC controller.
/// </summary>
public sealed class PLCServer
{
private PLCService service = null;
private string addr = "";
private bool isok = true;
private Thread thread = null;
private JsonSerializerSettings jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
};
/// <summary>
/// Creates the ZMQ server.
/// </summary>
/// <param name="addr">Endpoint to listen on. For example, "tcp://*:5555".</param>
public PLCServer(string addr) : this(new PLCService(), addr)
{
}
/// <summary>
/// Creates the ZMQ server.
/// </summary>
/// <param name="memory">A custom instance of PLC memory</param>
/// <param name="addr">Endpoint to listen on. For example, "tcp://*:5555".</param>
public PLCServer(PLCMemory memory, string addr) : this(new PLCService(memory), addr)
{
}
/// <summary>
/// Creates the ZMQ server.
/// </summary>
/// <param name="service">A custom instance of PLC service</param>
/// <param name="addr">Endpoint to listen on. For example, "tcp://*:5555".</param>
public PLCServer(PLCService service, string addr)
{
this.addr = addr;
this.service = service;
}
/// <summary>
/// Start the PLC server on a background thread.
/// </summary>
public void Start()
{
this.Stop();
this.isok = true;
this.thread = new Thread(new ThreadStart(this._ServerThread));
this.thread.Start();
}
/// <summary>
/// Stop the PLC server. Will block until the background thread teminates.
/// </summary>
public void Stop()
{
this.isok = false;
if (this.thread != null)
{
this.thread.Join();
this.thread = null;
}
}
/// <summary>
/// Whether ZMQ server is currently running.
/// </summary>
public bool IsRunning
{
get { return this.thread != null && this.thread.IsAlive; }
}
/// <summary>
/// Listening endpoint of the ZMQ server.
/// </summary>
public string Address
{
get { return this.addr; }
}
/// <summary>
/// Underlying service instance.
/// </summary>
public PLCService Service
{
get { return this.service; }
}
/// <summary>
/// Underlying memory instance.
/// </summary>
public PLCMemory Memory
{
get { return this.service.Memory; }
}
private void _ServerThread()
{
while (this.isok)
{
try
{
using (var socket = new ResponseSocket(this.addr))
{
// loop until told to stop or when exception is thrown
while (this.isok)
{
// wait for 50 ms
if (socket.Poll(PollEvents.PollIn, TimeSpan.FromMilliseconds(50)).HasIn())
{
this._RecvAndSend(socket);
}
}
}
}
catch (NetMQException e)
{
// recover from zmq error by re-creating socket
// TODO: log here
Console.WriteLine("Encountered ZMQ error: {0}, will re-create socket.", e.Message);
}
}
}
private void _RecvAndSend(ResponseSocket socket)
{
string received = socket.ReceiveFrameString(System.Text.Encoding.UTF8);
PLCResponse response = null;
try
{
// ask service to handle request
PLCRequest request = JsonConvert.DeserializeObject<PLCRequest>(received, jsonSettings);
response = this.service.Handle(request);
}
catch (PLCService.PLCServiceException e)
{
// reply with an error
response = new PLCResponse
{
Error = new PLCResponse.PLCError { Type = e.Code, Desc = e.Message },
};
}
catch (System.Exception e)
{
// reply with an error
response = new PLCResponse
{
Error = new PLCResponse.PLCError { Type = "unkown", Desc = e.Message },
};
}
// serialize to json and send
if (response == null)
{
response = new PLCResponse();
}
string serialized = JsonConvert.SerializeObject(response, Formatting.None, jsonSettings);
socket.SendFrame(System.Text.Encoding.UTF8.GetBytes(serialized));
}
}
}