-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
48 lines (39 loc) · 1.45 KB
/
Program.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
using System;
namespace Publisher
{
class Program
{
private const int TotalInstances = 3;
private static readonly PublisherHandler[] Publishers = new PublisherHandler[TotalInstances];
static void Main(string[] args)
{
// Ensure that the necessary
// command line arguments are given.
if (args.Length == 0)
{
Console.WriteLine("Usage:");
Console.WriteLine(" Publisher PORT");
return;
}
// Get port.
ushort port = ushort.Parse(args[0]);
// Initialize the publishers.
for (int i = 0; i < TotalInstances; i++)
{
PublisherHandler publisher = new PublisherHandler($"Publisher {i + 1}");
// Set second publisher to only allow one connection
if (i == 1)
publisher.MaximumAllowedConnections = 1;
publisher.Start((ushort)(port + i));
Publishers[i] = publisher;
}
// Wait until the user presses enter before quitting.
Console.ReadLine();
// Stop publisher instances - this stops publication
for (int i = 0; i < TotalInstances; i++)
Publishers[i].Stop();
// Disconnect the subscriber to stop background threads.
Console.WriteLine("Publishers stopped.");
}
}
}