-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
306 lines (256 loc) · 9.83 KB
/
MainForm.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
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
using System;
using SimpleTCP;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace AtisMosGateway
{
public partial class MainForm : Form
{
WebServerThread ws;
public int numHttpRequests = 0;
private ClientsManager clientsManager;
SimpleTcpServer serverL;
SimpleTcpServer serverU;
public MainForm()
{
InitializeComponent();
if (!startServers())
{
Environment.Exit(1);
}
ws = new WebServerThread(this, logBox);
clientsManager = new ClientsManager();
initMosClients();
}
delegate void SetTextCallback(string text);
public void logText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (logBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(logText);
//this.Invoke(d, new object[] { text });
logBox.Invoke(d, new object[] { text });
}
else
{
//logBox.Text += text + "\r\n";
logBox.AppendText(text + "\r\n");
}
}
public void logText1(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (textBox1.InvokeRequired)
{
SetTextCallback d1 = new SetTextCallback(logText);
//this.Invoke(d, new object[] { text });
textBox1.Invoke(d1, new object[] { text });
}
else
{
//logBox.Text += text + "\r\n";
textBox1.AppendText(text + "\r\n");
}
}
public void setXPR_upper_on() {
if (checkBox_xpr_upper.InvokeRequired)
{
//SetTextCallback
}
//checkBox_xpr_upper.Checked = true;
}
public void setXPR_lower_on()
{
//checkBox_xpr_lower.Checked = true;
}
private bool startServers()
{
bool startOk = true;
serverL = new SimpleTcpServer();
serverL.ClientConnected += tcpClientConnected;
serverL.ClientDisconnected += tcpClientDisconnected;
serverL.DataReceived += tcpServerDataReceived;
try
{
serverL.Start(10540);
}
catch (InvalidOperationException e)
{
startOk = false;
MessageBox.Show("Port 10540 is in use. AtisMosGateway will now exit!");
}
serverU = new SimpleTcpServer();
serverU.ClientConnected += tcpClientConnected;
serverU.ClientDisconnected += tcpClientDisconnected;
serverU.DataReceived += tcpServerDataReceived;
try
{
serverU.Start(10541);
}
catch (InvalidOperationException e)
{
startOk = false;
MessageBox.Show("Port 10541 is in use. AtisMosGateway will now exit!");
}
return startOk;
}
private void initMosClients()
{
// XPRESSION MOS GW address
string mosgw_xpression_ip = Properties.Settings.Default.xpression_ip; //get ip from config
if (mosgw_xpression_ip != "") {
MosClientThread xpMos = new MosClientThread(this, "XPRESSION", mosgw_xpression_ip, "CG", "XPRESSION");
clientsManager.addClient(xpMos);
label_xpr_ip.Text = mosgw_xpression_ip;
}
// PROMPTER MOS address
string mosgw_prompter_ip = Properties.Settings.Default.prompter_ip; //get ip from config
if (mosgw_prompter_ip != "") {
MosClientThread xpPrpt = new MosClientThread(this, "PROMPTER", mosgw_prompter_ip, "CG", "PORTAPROMPT");
clientsManager.addClient(xpPrpt);
}
// EVS MOSGW address
string mosgw_evs_ip = Properties.Settings.Default.evs_ip; //get ip from config
if (mosgw_evs_ip != "") {
MosClientThread evsMos = new MosClientThread(this, "ipd.evs.mos", mosgw_evs_ip, "VIDEO", "EVS-XS");
clientsManager.addClient(evsMos);
label_evs_ip.Text = mosgw_evs_ip;
}
}
private void tcpClientConnected(object sender, TcpClient tcpClient)
{
logText($"Client ({tcpClient.Client.RemoteEndPoint}) connected!");
MosClientThread client = clientsManager.getClientByIp(getIPfromEndpoint(tcpClient.Client.RemoteEndPoint));
if (client != null)
{
if (((SimpleTcpServer)sender) == serverL)
{
client.inConL.Add(tcpClient);
}
if (((SimpleTcpServer)sender) == serverU)
{
client.inConU.Add(tcpClient);
}
}
}
private void tcpClientDisconnected(object sender, TcpClient tcpClient)
{
logText($"Client ({tcpClient.Client.RemoteEndPoint}) disconnected!");
MosClientThread client = clientsManager.getClientByIp(getIPfromEndpoint(tcpClient.Client.RemoteEndPoint));
if (client != null)
{
if (((SimpleTcpServer)sender) == serverL)
{
client.inConL.Remove(tcpClient);
}
if (((SimpleTcpServer)sender) == serverU)
{
client.inConU.Remove(tcpClient);
}
}
}
private void tcpServerDataReceived(object sender, SimpleTCP.Message msg)
{
//var ep = e.TcpClient.Client.RemoteEndPoint;
MosClientThread client = clientsManager.getClientByIp(getIPfromEndpoint(msg.TcpClient.Client.RemoteEndPoint));
if (client != null)
{
//avem mesaj de la clientul X (lower sau upper)
var msgData = Encoding.BigEndianUnicode.GetString(msg.Data);
if (((SimpleTcpServer)sender) == serverL)
{
client.lastConnectionL = msg.TcpClient;
client.sendNcsMessage(MosPort.NcsLower, msgData);
}
if (((SimpleTcpServer)sender) == serverU)
{
client.lastConnectionU = msg.TcpClient;
client.sendNcsMessage(MosPort.NcsUpper, msgData);
}
}
}
private string getIPfromEndpoint(EndPoint endPoint)
{
return ((IPEndPoint)endPoint).Address.ToString();
//return IPAddress.Parse(((IPEndPoint)endPoint.Address.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ws = new WebServerThread(this, logBox);
}
private void timer1_Tick(object sender, EventArgs e)
{
lblNumReq.Text = "Number of HTTP requests: " + numHttpRequests;
}
public string HttpPostMos(string mosID, MosPort mosPort, string mosData)
{
string result = "";
//string url = "http://192.168.30.70/newsroom/mos/";
string url = Properties.Settings.Default.newsroom_url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
var postData = "mosID=" + Uri.EscapeDataString(mosID);
postData += "&mosPort=" + Uri.EscapeDataString(mosPort.ToString());
postData += "&mosData=" + Uri.EscapeDataString(mosData);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
result = new StreamReader(response.GetResponseStream()).ReadToEnd();
response.Close();
} catch (System.Net.WebException ex)
{
logText(ex.Message);
/*if (ex.Status == WebExceptionStatus.ProtocolError)
{
var response = ex.Response as HttpWebResponse;
if (response != null)
{
Console.WriteLine("HTTP Status Code: " + (int)response.StatusCode);
}
else
{
// no http status code available
}
}
else
{
// no http status code available
}*/
} /*finally
{
}*/
return result;
}
public void NcsToMosMessage(string mosID, MosPort mosPort, string mosData)
{
MosClientThread client = clientsManager.getClientByMosID(mosID);
if (client != null) {
client.sendMosMessage(mosPort, mosData);
}
}
}
}