-
Notifications
You must be signed in to change notification settings - Fork 112
/
pathosub.cs
64 lines (58 loc) · 1.47 KB
/
pathosub.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ZeroMQ;
namespace Examples
{
static partial class Program
{
public static void PathoSub(string[] args)
{
//
// Pathological subscriber
// Subscribes to one random topic and prints received messages
//
// Author: metadings
//
if (args == null || args.Length < 1)
{
Console.WriteLine();
Console.WriteLine("Usage: ./{0} PathoSub [Endpoint]", AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine();
Console.WriteLine(" Endpoint Where PathoSub should connect to.");
Console.WriteLine(" Default is tcp://127.0.0.1:5556");
Console.WriteLine();
args = new string[] { "tcp://127.0.0.1:5556" };
}
using (var context = new ZContext())
using (var subscriber = new ZSocket(context, ZSocketType.SUB))
{
subscriber.Connect(args[0]);
var rnd = new Random();
var subscription = string.Format("{0:D3}", rnd.Next(1000));
subscriber.Subscribe(subscription);
ZMessage msg;
ZError error;
while (true)
{
if (null == (msg = subscriber.ReceiveMessage(out error)))
{
if (error == ZError.ETERM)
break; // Interrupted
throw new ZException(error);
}
using (msg)
{
if (msg[0].ReadString() != subscription)
{
throw new InvalidOperationException();
}
Console.WriteLine(msg[1].ReadString());
}
}
}
}
}
}