public void InitialiseSocket() { m_PublishSocket = m_Context.CreateSocket(SocketType.PUB); m_PublishSocket.Bind("epgm://239.1.1.1:9500"); m_PublishSocket.Bind("tcp://*:9500"); m_PublishSocket.Bind("inproc://Local"); }
public static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket frontend = context.CreateSocket(SocketType.SUB), backend = context.CreateSocket(SocketType.PUB)) { // This is where the weather server sits frontend.Connect("tcp://127.0.0.1:5556"); frontend.Subscribe(new byte[0]); // This is our public endpoint for subscribers backend.Bind("tcp://*:8100"); // i use local to be able to run the example, this could be the public ip instead eg. tcp://10.1.1.0:8100 // Shunt messages out to our own subscribers while (true) { bool hasMore = true; while (hasMore) { string message = frontend.Receive(Encoding.Unicode); hasMore = frontend.ReceiveMore; backend.Send(Encoding.Unicode.GetBytes(message), message.Length, hasMore ? SocketFlags.SendMore : SocketFlags.None); } } } } }
public ZeroMqMessageStream(IScheduler scheduler, string subsciptionAddress, params string[] publishAddresses) { _scheduler = scheduler; _correlationId = Guid.NewGuid(); _context = ZmqContext.Create(); _subSocket = _context.CreateSocket(SocketType.SUB); _pubSocket = _context.CreateSocket(SocketType.PUB); if (!String.IsNullOrEmpty(subsciptionAddress)) { _subSocket.Bind(subsciptionAddress); _subSocket.Connect(subsciptionAddress); _subSocket.SubscribeAll(); } foreach (var publishAddress in publishAddresses) { _pubSocket.Connect(publishAddress); } Scheduler.NewThread.Schedule(() => { while (true) { var zmqMessage = _subSocket.ReceiveMessage(TimeSpan.FromMilliseconds(1000)); if (zmqMessage.FrameCount >= 0 && zmqMessage.TotalSize > 1 && zmqMessage.IsComplete) { var message = Encoding.UTF8.GetString(zmqMessage.First()); _messageStream.OnNext(message); Debug.WriteLine("Correlation ID: {0} - message received with status {1}", _correlationId, _subSocket.ReceiveStatus); } } }); }
public ZmqEndPoint Bind() { _socket = CreateSocket(); _endPoint = new ZmqEndPoint(_originalEndpoint.Value); if (_endPoint.HasRandomPort) { _endPoint.SelectRandomPort(_peerId, _environment); } _socket.Bind(_endPoint.Value); try { _endPoint.SavePort(_peerId, _environment); } catch (Exception ex) { _logger.Error("Impossible to write inbound port file.", ex); } var endPointWithIp = new ZmqEndPoint(_socket.LastEndpoint); _logger.InfoFormat("Socket bound, Inbound EndPoint: {0}", endPointWithIp.Value); return(endPointWithIp); }
public void Start() { try { using (var context = ZmqContext.Create()) { using (ZmqSocket pullSocket = context.CreateSocket(SocketType.PULL), repSocket = context.CreateSocket(SocketType.REP)) { pullSocket.Bind(_clientEndPoint); repSocket.Bind(_workerEndPoint); pullSocket.ReceiveReady += pullSocket_ReceiveReady; repSocket.ReceiveReady += repSocket_ReceiveReady; var poller = new Poller(new List <ZmqSocket> { pullSocket, repSocket }); while (true) { poller.Poll(); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public ZmqPublisher(string endpoint) { _endpoint = endpoint; _context = ZmqContext.Create(); _socket = _context.CreateSocket(SocketType.PUB); _socket.Bind(_endpoint); }
public static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket publisher = context.CreateSocket(SocketType.PUB)) { publisher.Bind("tcp://*:5556"); var randomizer = new Random(DateTime.Now.Millisecond); while (true) { // Get values that will fool the boss int zipcode = randomizer.Next(0, 100000); int temperature = randomizer.Next(-80, 135); int relativeHumidity = randomizer.Next(10, 60); string update = zipcode.ToString() + " " + temperature.ToString() + " " + relativeHumidity.ToString(); // Send message to 0..N subscribers via a pub socket publisher.Send(update, Encoding.Unicode); } } } }
/// <summary> /// Initializes a new instance of the <see cref="ZmqResponseQueue"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="port">The port.</param> internal ZmqResponseQueue(ZmqContext context, int port) { socket = context.CreateSocket(SocketType.REP); var address = string.Format("tcp://127.0.0.1:{0}", port); socket.Bind(address); }
protected virtual ZmqSocket CreateSocket(string endPoint) { ZmqSocket receiver = ZeroMessageQueue.ZmqContext.CreateSocket(SocketType.PULL); receiver.Bind(endPoint); return(receiver); }
private static void Open(Object cancelationToken) { context = ZmqContext.Create(); frontend = context.CreateSocket(SocketType.ROUTER); backend = context.CreateSocket(SocketType.DEALER); backend.Identity = Encoding.UTF8.GetBytes("inproc://workers"); frontend.Bind("tcp://127.0.0.1:5000"); backend.Bind("inproc://workers"); workerThreads = new Thread[2]; for (int threadId = 0; threadId < workerThreads.Length; threadId++) { workerThreads[threadId] = new Thread(WorkerRoutine); workerThreads[threadId].Start(context); } frontend.ReceiveReady += new EventHandler<SocketEventArgs>(frontend_ReceiveReady); backend.ReceiveReady += new EventHandler<SocketEventArgs>(backend_ReceiveReady); Poller poller = new Poller(new[] { frontend, backend }); var token = (CancellationToken)cancelationToken; while (!token.IsCancellationRequested) { poller.Poll(TimeSpan.FromMilliseconds(100)); } }
public void Initialize() { ZmqContext = ZmqContext.Create(); Sender = ZmqContext.CreateSocket(_senderType); Receiver = ZmqContext.CreateSocket(_receiverType); _senderThread = new Thread(() => { SenderInit(Sender); Sender.SendHighWatermark = 1; _receiverReady.WaitOne(); Sender.Connect("inproc://spec_context"); SenderAction(Sender); }); _receiverThread = new Thread(() => { ReceiverInit(Receiver); Receiver.SendHighWatermark = 1; Receiver.Bind("inproc://spec_context"); _receiverReady.Set(); ReceiverAction(Receiver); }); StartThreads(); }
public static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket receiver = context.CreateSocket(SocketType.PULL), controller = context.CreateSocket(SocketType.PUB)) { receiver.Bind("tcp://*:5558"); controller.Bind("tcp://*:5559"); // Wait for start of batch receiver.Receive(Encoding.Unicode); var stopwatch = new Stopwatch(); stopwatch.Start(); const int tasksToConfirm = 100; for (int taskNumber = 0; taskNumber < tasksToConfirm; taskNumber++) { string message = receiver.Receive(Encoding.Unicode); Console.WriteLine(taskNumber % 10 == 0 ? ":" : "."); } stopwatch.Stop(); Console.WriteLine("Total elapsed time: {0}", stopwatch.ElapsedMilliseconds); controller.Send("KILL", Encoding.Unicode); } } }
internal void Configure() { if (_isConfigured) { return; } if (_bindings.Count == 0 && _connections.Count == 0) { throw new InvalidOperationException("Device sockets must bind or connect to at least one endpoint."); } foreach (Action <ZmqSocket> initializer in _socketInitializers) { initializer.Invoke(_socket); } foreach (string endpoint in _bindings) { _socket.Bind(endpoint); } foreach (string endpoint in _connections) { _socket.Connect(endpoint); } _isConfigured = true; }
public static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket publisher = context.CreateSocket(SocketType.PUB)) { // your first potential issue lies here, if you're not // populating your addresses properly then you're not going to // bind appropriately // Test by hard coding the address publisher.Bind("tcp://127.0.0.1:5000"); int msgIndex = 0; while (true) { // your second potential issue lies here, if your logic // short circuits your send, that'll throw a wrench in the // works // Test by removing the logic and just sending a bunch of // messages var msg = "Message: " + msgIndex; // simplify Console.WriteLine("Publishing: " + msg); socket.Send(msg, Encoding.UTF8); Thread.Sleep(500); // hard code msgIndex++; if (msgIndex > 1500) { break; // hard code the break } } } } }
public void Socket_Send_Receive_Multipart_Test(string endpoint) { using (var receiver = new ZmqSocket(ZmqContext.Current, ZmqSocketType.Rep)) using (var senderContext = new ZmqContext()) using (var sender = (endpoint.StartsWith("inproc") ? ZmqContext.Current : senderContext).Socket(ZmqSocketType.Req)) { receiver.Bind(endpoint); sender.Connect(endpoint); var message1 = MessageTests.GetTestData(); var message2 = MessageTests.GetTestData(); sender.Send(message1, ZmqSendReceiveFlags.SendMore); sender.Send(message2); using (var receivedMessage1 = new ZmqMessage()) { receiver.Receive(receivedMessage1); CollectionAssert.AreEqual(message1, receivedMessage1.ToArray()); Assert.IsTrue(receivedMessage1.HasMore); } using (var receivedMessage2 = new ZmqMessage()) { receiver.Receive(receivedMessage2); CollectionAssert.AreEqual(message2, receivedMessage2.ToArray()); Assert.IsFalse(receivedMessage2.HasMore); } } }
public static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket publisher = context.CreateSocket(SocketType.PUB), syncService = context.CreateSocket(SocketType.REP)) { publisher.Bind("tcp://*:5561"); syncService.Bind("tcp://*:5562"); // Get synchronization from subscribers const int subscribersToWaitFor = 10; for (int count = 0; count < subscribersToWaitFor; count++) { syncService.Receive(Encoding.Unicode); syncService.Send("", Encoding.Unicode); } // Now broadcast exactly 1M updates followed by END const int updatesToSend = 1000000; for (int updateId = 0; updateId < updatesToSend; updateId++) { publisher.Send("Rhubard", Encoding.Unicode); } publisher.Send("END", Encoding.Unicode); } } }
static void Main(string[] args) { using (var context = ZmqContext.Create()) { using (ZmqSocket frontend = context.CreateSocket(SocketType.ROUTER), backend = context.CreateSocket(SocketType.ROUTER)) { frontend.Bind("tcp://*:5555"); // For Clients backend.Bind("tcp://*:5556"); // For Workers // Logic of LRU loop // - Poll backend always, frontend only if 1+ worker ready // - If worker replies, queue worker as ready and forward reply // to client if necessary // - If client requests, pop next worker and send request to it // Queue of available workers var workerQueue = new Queue <byte[]>(); // Handle worker activity on backend backend.ReceiveReady += (s, e) => { var zmsg = new ZMessage(e.Socket); // Use worker address for LRU routing workerQueue.Enqueue(zmsg.Unwrap()); // Forward message to client if it's not a READY if (!Encoding.Unicode.GetString(zmsg.Address).Equals(LRU_READY)) { zmsg.Send(frontend); } }; frontend.ReceiveReady += (s, e) => { // Now get next client request, route to next worker // Dequeue and drop the next worker address var zmsg = new ZMessage(e.Socket); zmsg.Wrap(workerQueue.Dequeue(), new byte[0]); zmsg.Send(backend); }; var poller = new Poller(new ZmqSocket[] { frontend, backend }); while (true) { //int rc = ZmqContext.Poller(workerQueue.Count > 0 // ? new List<ZmqSocket>(new ZmqSocket[] {frontend, backend}) // : new List<ZmqSocket>(new ZmqSocket[] {backend})); int rc = poller.Poll(); if (rc == -1) { break; } } } } }
public static ZmqSocket GetBoundSubscribeSocket(ZmqContext context, string address) { ZmqSocket subscriber = context.CreateSocket(SocketType.SUB); subscriber.Bind(address); subscriber.SubscribeAll(); return(subscriber); }
public void CreateCommandReceiverSocket(string endpoint) { _receptionSocket = _context.CreateSocket(SocketType.PULL); _receptionSocket.Linger = TimeSpan.FromSeconds(1); _receptionSocket.ReceiveHighWatermark = 30000; _receptionSocket.Bind(endpoint); _logger.DebugFormat("Command processor socket bound to {0}", endpoint); }
public ZeroMqMessagePublisher(ZmqContext context) { Console.WriteLine("Hey there"); _pub = context.CreateSocket(SocketType.PUB); _pub.Bind("tcp://*:5555"); Console.WriteLine("Hello"); }
public void CreateReceiveSocket() { m_SubscribeSocket = m_Context.CreateSocket(SocketType.SUB); m_SubscribeSocket.SubscribeAll(); m_SubscribeSocket.Connect("epgm://239.1.1.1:9500"); m_SubscribeSocket.Bind("tcp://*:9501"); m_SubscribeSocket.Connect("inproc://Local"); }
public void Start(ZmqContext context) { this.SetUpMonitorChannel(context); this.SetUpAddSubscriberCountChannel(context); ////this should work but the forwarder device appears to be broken - it does not use XSUb and XPUB sockets ////ForwarderDevice = new ForwarderDevice(context, PublishAddressServer, SubscribeAddressServer, DeviceMode.Threaded); ////ForwarderDevice.Start(); ////while (!ForwarderDevice.IsRunning) ////{ } QueueDevce = new QueueDevice(context, PubSubControlBackAddressServer, PubSubControlFrontAddress, DeviceMode.Threaded); QueueDevce.Start(); while (!QueueDevce.IsRunning) { } this.Writeline("Control channel started"); //this.Context = context; this.cancellationTokenSource = new CancellationTokenSource(); var token = this.cancellationTokenSource.Token; Task.Run(() => { using (frontend = context.CreateSocket(SocketType.XSUB)) { using (backend = context.CreateSocket(SocketType.XPUB)) { frontend.Bind(Pipe.PublishAddressServer); ////"tcp://*:5550"); backend.Bind(Pipe.SubscribeAddressServer); ////"tcp://*:5553"); frontend.ReceiveReady += new EventHandler <SocketEventArgs>(FrontendReceiveReady); backend.ReceiveReady += new EventHandler <SocketEventArgs>(BackendReceiveReady); this.AddSubscriberCountChannel.ReceiveReady += new EventHandler <SocketEventArgs>(AddSubscriberCountChannelReceiveReady); using (poller = new Poller(new ZmqSocket[] { frontend, backend, this.AddSubscriberCountChannel })) { Writeline("About to start polling"); while (true) { poller.Poll(new TimeSpan(0, 0, 0, 0, 5)); Writeline("polling"); if (token.IsCancellationRequested) { Writeline("break"); break; } } } Writeline("stopped polling and exiting"); } } }, token); }
internal static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("usage: local_thr <address> <message-size> <message-count>\n"); return(1); } string address = args[0]; int messageSize = Convert.ToInt32(args[1]); int messageCount = Convert.ToInt32(args[2]); if (messageSize <= 0 || messageCount <= 0) { Console.Error.WriteLine("message-size and message-count must be positive values."); return(1); } long elapsedTime; // Initialize 0MQ infrastructure using (ZmqContext ctx = ZmqContext.Create()) using (ZmqSocket skt = ctx.CreateSocket(SocketType.PULL)) { skt.Bind(address); // Wait for the first message. var msg = new byte[messageSize]; int receivedBytes = skt.Receive(msg); Debug.Assert(receivedBytes == messageSize, "Received message did not have the expected length."); Stopwatch watch = Stopwatch.StartNew(); // Receive all the remaining messages. for (int i = 1; i < messageCount; i++) { receivedBytes = skt.Receive(msg); Debug.Assert(receivedBytes == messageSize, "Received message did not have the expected length."); } watch.Stop(); elapsedTime = watch.ElapsedTicks; } long messageThroughput = messageCount * Stopwatch.Frequency / elapsedTime; long megabitThroughput = messageThroughput * messageSize * 8 / 1000000; Console.WriteLine("Average throughput: {0} [msg/s]", messageThroughput); Console.WriteLine("Average throughput: {0} [Mb/s]", megabitThroughput); Console.WriteLine("Msgs: {0,7}, MsgSz: {1,6}, Msgs/s: {2,6} , Throughput: {3,6} Mb/s", messageCount, messageSize, latency.ToString("f2"), trans); return(0); }
public static void Main(string[] args) { var randomizer = new Random(DateTime.Now.Millisecond); if (args.Length < 2) { Console.WriteLine("Usage: peering1 <myself> <peer_1> ... <peer_N>"); return; } var myself = args[0]; Console.WriteLine("Hello, I am " + myself); using (var context = ZmqContext.Create()) { using (ZmqSocket statebe = context.CreateSocket(SocketType.PUB), statefe = context.CreateSocket(SocketType.SUB)) { var bindAddress = "tcp://127.0.0.1:" + myself; statebe.Bind(bindAddress); Thread.Sleep(1000); for (int arg = 1; arg < args.Length; arg++) { var endpoint = "tcp://127.0.0.1:" + args[arg]; statefe.Connect(endpoint); statefe.Subscribe(Encoding.Unicode.GetBytes(string.Empty)); Thread.Sleep(1000); } statefe.ReceiveReady += (s, e) => { string peerName = e.Socket.Receive(Encoding.Unicode); string available = e.Socket.Receive(Encoding.Unicode); Console.WriteLine("{0} - {1} workers free\n", peerName, available); }; var poller = new Poller(new List <ZmqSocket> { statefe }); while (true) { int count = poller.Poll(TimeSpan.FromMilliseconds(1000 * 1000)); if (count == 0) { statebe.Send(myself, Encoding.Unicode); statebe.Send(randomizer.Next(10).ToString(), Encoding.Unicode); } } } } }
public override void Bind(ZmqSocket socket, ZeroRoute config) { ZeroLog.LogInfo("pub bind...."); foreach (var endPoint in config.ConnectEndPoints()) { socket.Bind(endPoint); ZeroLog.LogInfo("pub bind...." + endPoint); } }
public void Start() { _context = ZmqContext.Create(); _commandReceiver = _context.CreateSocket(SocketType.REP); _commandReceiver.Bind("tcp://*:7777"); _dequeueReceiver = _context.CreateSocket(SocketType.PUB); _dequeueReceiver.Bind("tcp://*:7778"); StartListener(); }
public override void Bind(ZmqSocket socket, ZeroRoute config) { ZeroLog.LogInfo("pull bind...."); foreach (var endPoint in config.ConnectEndPoints()) { socket.Bind(endPoint); ZeroLog.LogInfo("pull bind...."); } }
/// <summary> /// Binds the back end server side socket to the specified endpoint. /// </summary> /// <param name="backend">The backend socket.</param> /// <param name="endpoint">The endpoint.</param> /// <exception cref="System.Exception"></exception> private static void BindBackEnd(ZmqSocket backend, string endpoint) { try { Console.WriteLine("Binding to {0}", endpoint); backend.Bind(endpoint); } catch (Exception ex) { throw new Exception(string.Format("Problem binding to '{0}'", endpoint), ex); } }
public void StartIncoming() { // ZMQ Context, server socket context = ZmqContext.Create(); LocalHost = LocalIPAddress() + ":" + listenport; Task.Factory.StartNew(() => { string message; try { ZmqSocket server = context.CreateSocket(ZeroMQ.SocketType.REP); server.Bind("tcp://" + LocalHost); Console.Out.WriteLine("I'm listening on " + LocalHost); while (true) { message = server.Receive(Encoding.Unicode); if (message.Equals("OVER")) { continue; } Console.WriteLine("COM: Got message {0}", message); var dataParts = message.Split('|'); if (dataParts.Count() < 2) { continue; } var type = (RaftMessageType)Int32.Parse(dataParts[0]); var sender = dataParts[1]; var data = (dataParts.Count() > 2 ? dataParts[2] : ""); var msg = new RaftMessage { Type = type, Sender = sender, Data = data }; incoming.Add(msg); server.Send("OVER", Encoding.UTF8); } } catch (Exception ex) { Console.Out.Write(ex); } }, TaskCreationOptions.LongRunning); }
/// <summary> /// Binds the back end server side socket to the specified endpoint. /// </summary> /// <param name="backend">The backend socket.</param> /// <param name="endpoint">The endpoint.</param> /// <exception cref="System.Exception"></exception> private static void BindSocket(ZmqSocket socket, string endpoint) { try { Console.WriteLine("Binding to {0}", endpoint); socket.Bind(endpoint); } catch (Exception ex) { throw new Exception(string.Format("Problem binding to endpoint '{0}'", endpoint), ex); } }
/// <summary> /// The main entry point for the application. /// </summary> static void Main() { ServiceBase[] servicesToRun = new ServiceBase[1]; using (ZmqContext context = ZmqContext.Create()) { using (ZmqSocket socket = context.CreateSocket(SocketType.PUB)) { socket.Bind("tcp://*:8585"); servicesToRun[0] = new Service1(socket); ServiceBase.Run(servicesToRun); } } }
static void Main(string[] args) { // ZMQ Context and client socket using (ZmqContext context = ZmqContext.Create()) using (ZmqSocket client = context.CreateSocket(SocketType.PUB)) { client.Bind("tcp://*:9000"); Console.WriteLine("Starting Zmq chat..."); while (true) { var message = Console.ReadLine(); client.Send("jorge: " + message, Encoding.ASCII); } } }
public ZmqEndPoint Bind() { _socket = CreateSocket(); _endPoint = new ZmqEndPoint(_originalEndpoint.Value); if (_endPoint.HasRandomPort) _endPoint.SelectRandomPort(_peerId, _environment); _socket.Bind(_endPoint.Value); var endPointWithIp = new ZmqEndPoint(_socket.LastEndpoint); _logger.InfoFormat("Socket bound, Inbound EndPoint: {0}", endPointWithIp.Value); return endPointWithIp; }
public Publisher(string _hostName, int _port) { hostName = _hostName; port = _port; PublishBag = new ConcurrentBag <byte[]>(); connectionString = String.Format("tcp://{0}:{1}", hostName, port); ctx = ZmqContext.Create(); socket = ctx.CreateSocket(SocketType.PUB); var str = String.Format("tcp://*:{0}", port); socket.Bind(str); socket.SendHighWatermark = 100000; socket.Backlog = 100000; socket.SendBufferSize = 128 * 1024; }
public void Start() { if (!string.IsNullOrWhiteSpace(_PubEndPoint)) { ZmqEventPublisher = ZeroMessageQueue.ZmqContext.CreateSocket(SocketType.PUB); ZmqEventPublisher.Bind(_PubEndPoint); using (var messageStore = IoCFactory.Resolve<IMessageStore>()) { messageStore.GetAllUnPublishedEvents() .ForEach(eventContext => MessageQueue.Add(eventContext)); } _WorkTask = Task.Factory.StartNew(PublishEvent, TaskCreationOptions.LongRunning); } }
public void OkNowIKnowThatMyMessagesAreNotLostAfterDisconnect() { var message = new byte[100]; var receiveBuffer = new byte[100]; Console.WriteLine("ZMQ v{0}", ZmqUtil.GetVersion().ToString(3)); Console.WriteLine(Environment.Is64BitProcess ? "x64" : "x86"); using (var context = new ZmqContext()) using (var receiver = new ZmqSocket(context, ZmqSocketType.PULL)) using (var sender = new ZmqSocket(context, ZmqSocketType.PUSH)) { var port = TcpUtil.GetRandomUnusedPort(); var receiveEndpoint = $"tcp://*:{port}"; var sendEndpoint = $"tcp://localhost:{port}"; receiver.SetOption(ZmqSocketOption.RCVHWM, 2_000); receiver.SetOption(ZmqSocketOption.RCVTIMEO, 200); receiver.SetOption(ZmqSocketOption.RCVBUF, 100_000); receiver.Bind(receiveEndpoint); sender.SetOption(ZmqSocketOption.SNDHWM, 2_000); //sender.SetOption(ZmqSocketOption.RCVHWM, 1); sender.SetOption(ZmqSocketOption.SNDTIMEO, 200); receiver.SetOption(ZmqSocketOption.SNDBUF, 100_000); sender.Connect(sendEndpoint); var sendCount = 0; while (sender.TrySend(message, 0, message.Length, out _)) { sendCount++; } Console.WriteLine("{0} sent messages", sendCount); sender.TryDisconnect(sendEndpoint); sender.Connect(sendEndpoint); var receivedMessageCount = 0; while (receiver.TryReadMessage(ref receiveBuffer, out _, out _)) { receivedMessageCount++; } Console.WriteLine("{0} received messages", receivedMessageCount); } }
public static void Main(string[] args) { const int workersCount = 10; var workers = new List <Thread>(workersCount); using (var context = ZmqContext.Create()) { using (ZmqSocket client = context.CreateSocket(SocketType.ROUTER)) { client.Bind("tcp://*:5555"); for (int workerNumber = 0; workerNumber < workersCount; workerNumber++) { workers.Add(new Thread(WorkerTask)); workers[workerNumber].Start(); } for (int taskNumber = 0; taskNumber < workersCount * 10; taskNumber++) { // LRU worker is next waiting in queue string address = client.Receive(Encoding.Unicode); string empty = client.Receive(Encoding.Unicode); string ready = client.Receive(Encoding.Unicode); client.SendMore(address, Encoding.Unicode); client.SendMore(string.Empty, Encoding.Unicode); client.Send("This is the workload", Encoding.Unicode); } // Now ask mamas to shut down and report their results for (int taskNbr = 0; taskNbr < workersCount; taskNbr++) { string address = client.Receive(Encoding.Unicode); string empty = client.Receive(Encoding.Unicode); string ready = client.Receive(Encoding.Unicode); client.SendMore(address, Encoding.Unicode); client.SendMore(string.Empty, Encoding.Unicode); client.Send("END", Encoding.Unicode); } } } Console.ReadLine(); }
static void Main(string[] args) { context_ = ZmqContext.Create(); socket_ = context_.CreateSocket(SocketType.PUB); socket_.Bind("tcp://127.0.0.1:5000"); bool publish = true; Task.Run(() => { while (publish) { string timestring = DateTime.Now.ToString("u"); Console.WriteLine("Sending '{0}' to subscribers", timestring); socket_.Send(timestring, Encoding.Unicode); Thread.Sleep(1000); } }); Console.ReadLine(); publish = false; }
public Publisher(string _guid, string _host, int _port, ZmqContext _ctx) { guid = _guid; host = _host; port = _port; if (_ctx == null) ctx = ZmqContext.Create(); else ctx = _ctx; socket = ctx.CreateSocket(SocketType.PUB); var bindString = String.Empty; if(port > 0) bindString = String.Format("tcp://*:{0}", port); else bindString = String.Format("inproc://{0}", guid.ToLower()); socket.Bind(bindString); log.InfoFormat("Publisher successfuly binded on {0}", bindString); socket.SendHighWatermark = 1000000; socket.SendBufferSize = 128 * 1024; }
public Publisher(string _hostName, int _port) { hostName = _hostName; port = _port; PublishBag = new ConcurrentBag<byte[]>(); connectionString = String.Format("tcp://{0}:{1}", hostName, port); ctx = ZmqContext.Create(); socket = ctx.CreateSocket(SocketType.PUB); var str = String.Format("tcp://*:{0}", port); socket.Bind(str); socket.SendHighWatermark = 100000; socket.Backlog = 100000; socket.SendBufferSize = 128 * 1024; }
/// <summary> /// Start the server. /// </summary> public void StartServer() { //check that it's not already running if (ServerRunning) return; _context = ZmqContext.Create(); _routerSocket = _context.CreateSocket(SocketType.ROUTER); _routerSocket.Bind("tcp://*:" + _listenPort); _runServer = true; _serverThread.Start(); ServerRunning = true; }
public void Start(ZmqContext context) { this.SetUpMonitorChannel(context); this.SetUpAddSubscriberCountChannel(context); ////this should work but the forwarder device appears to be broken - it does not use XSUb and XPUB sockets ////ForwarderDevice = new ForwarderDevice(context, PublishAddressServer, SubscribeAddressServer, DeviceMode.Threaded); ////ForwarderDevice.Start(); ////while (!ForwarderDevice.IsRunning) ////{ } QueueDevce = new QueueDevice(context, PubSubControlBackAddressServer, PubSubControlFrontAddress, DeviceMode.Threaded); QueueDevce.Start(); while (!QueueDevce.IsRunning) { } this.Writeline("Control channel started"); //this.Context = context; this.cancellationTokenSource = new CancellationTokenSource(); var token = this.cancellationTokenSource.Token; Task.Run(() => { using (frontend = context.CreateSocket(SocketType.XSUB)) { using (backend = context.CreateSocket(SocketType.XPUB)) { frontend.Bind(Pipe.PublishAddressServer); ////"tcp://*:5550"); backend.Bind(Pipe.SubscribeAddressServer); ////"tcp://*:5553"); frontend.ReceiveReady += new EventHandler<SocketEventArgs>(FrontendReceiveReady); backend.ReceiveReady += new EventHandler<SocketEventArgs>(BackendReceiveReady); this.AddSubscriberCountChannel.ReceiveReady += new EventHandler<SocketEventArgs>(AddSubscriberCountChannelReceiveReady); using (poller = new Poller(new ZmqSocket[] { frontend, backend, this.AddSubscriberCountChannel })) { Writeline("About to start polling"); while (true) { poller.Poll(new TimeSpan(0,0,0,0,5)); Writeline("polling"); if (token.IsCancellationRequested) { Writeline("break"); break; } } } Writeline("stopped polling and exiting"); } } }, token); }
/// <summary> /// Starts this instance. /// </summary> public bool Start() { if (Started) return true; // Prepare peer asynchronous operation _cancellationTokenSource = new CancellationTokenSource(); _peers = new ConcurrentDictionary<Guid, Node>(); _mailboxMessageQueue = new ConcurrentQueue<ZmqMessage>(); _dataAvailable = new AutoResetEvent(false); // Create the context _context = ZmqContext.Create(); // Create the mailbox router and connect it _mailbox = _context.CreateSocket(SocketType.ROUTER); _mailbox.Bind("tcp://*:*"); // Port ermitteln var port = _mailbox.LastEndpoint.Split(':').Last(); if (!UInt16.TryParse(port, NumberStyles.Integer, CultureInfo.InvariantCulture, out _mailboxPort)) { Trace.TraceError("Could not parse ZRE router port '{0}'", port); StopInternal(); return false; } // Start mailbox processing task _mailboxPollTask = Task.Factory.StartNew(MailboxPollTask, new ConsumerTaskState(_mailbox, _cancellationTokenSource.Token, _dataAvailable, _mailboxMessageQueue), TaskCreationOptions.LongRunning); _mailboxProcessTask = Task.Factory.StartNew(MailboxProcessTask, new ConsumerTaskState(null, _cancellationTokenSource.Token, _dataAvailable, _mailboxMessageQueue), TaskCreationOptions.LongRunning); // Broadcast erzeugen Trace.Assert(_broadcastFactory != null); _broadcast = _broadcastFactory.Create(_uuid, _mailboxPort); _broadcast.PeerDiscovered += OnBroadcastPeerDiscovered; _broadcast.Start(); // Oink Started = true; return true; }
/// <summary> /// The main loop. Runs on its own thread. Accepts requests on the REP socket, gets results from the InstrumentManager, /// and sends them back right away. /// </summary> private void ContractServer() { var timeout = new TimeSpan(100000); _socket = _context.CreateSocket(SocketType.REP); _socket.Bind("tcp://*:" + _socketPort); var ms = new MemoryStream(); List<Instrument> instruments; while (_runServer) { string request = _socket.Receive(Encoding.UTF8, timeout); if (request == null) continue; //if the request is for a search, receive the instrument w/ the search parameters and pass it to the searcher if (request == "SEARCH" && _socket.ReceiveMore) { int size; byte[] buffer = _socket.Receive(null, timeout, out size); var searchInstrument = MyUtils.ProtoBufDeserialize<Instrument>(buffer, ms); Log(LogLevel.Info, string.Format("Instruments Server: Received search request: {0}", searchInstrument)); try { instruments = _instrumentManager.FindInstruments(null, searchInstrument); } catch (Exception ex) { Log(LogLevel.Error, string.Format("Instruments Server: Instrument search error: {0}", ex.Message)); instruments = new List<Instrument>(); } } else if (request == "ALL") //if the request is for all the instruments, we don't need to receive anything else { Log(LogLevel.Info, "Instruments Server: received request for list of all instruments."); instruments = _instrumentManager.FindInstruments(); } else if (request == "ADD") //request to add instrument { int size; byte[] buffer = _socket.Receive(null, timeout, out size); var instrument = MyUtils.ProtoBufDeserialize<Instrument>(buffer, ms); bool addResult; try { addResult = InstrumentManager.AddInstrument(instrument); } catch (Exception ex) { addResult = false; Log(LogLevel.Error, string.Format("Instruments Server: Instrument addition error: {0}", ex.Message)); } _socket.Send(addResult ? "SUCCESS" : "FAILURE", Encoding.UTF8); continue; } else //no request = loop again { continue; } byte[] uncompressed = MyUtils.ProtoBufSerialize(instruments, ms);//serialize the list of instruments ms.Read(uncompressed, 0, (int)ms.Length); //get the uncompressed data byte[] result = LZ4Codec.Encode(uncompressed, 0, (int)ms.Length); //compress it //before we send the result we must send the length of the uncompressed array, because it's needed for decompression _socket.SendMore(BitConverter.GetBytes(uncompressed.Length)); //then finally send the results _socket.Send(result); } _socket.Dispose(); }
void ServerThread() { using (context ?? (context = ZmqContext.Create())) using (subscribeSocket = context.CreateSocket(SocketType.XSUB)) using (publishSocket = context.CreateSocket(SocketType.XPUB)) { publishSocket.Bind(PublishAddress); subscribeSocket.Bind(SubscribeAddress); publishSocket.ReceiveReady += publishSocket_ReceiveReady; subscribeSocket.ReceiveReady += subscribeSocket_ReceiveReady; var poller = new Poller(new List<ZmqSocket> { subscribeSocket, publishSocket }); InitializationDone.Set(); while (true) { poller.Poll(); } } }
/// <summary> /// Starts the publishing and request servers. /// </summary> public void StartServer() { if (!ServerRunning) //only start if it isn't running already { _runServer = true; _context = ZmqContext.Create(); //the publisher socket _pubSocket = _context.CreateSocket(SocketType.PUB); _pubSocket.Bind("tcp://*:" + PublisherPort); //the request socket _reqSocket = _context.CreateSocket(SocketType.REP); _reqSocket.Bind("tcp://*:" + RequestPort); _requestThread = new Thread(RequestServer) {Name = "RTDB Request Thread"}; //clear queue before starting? _requestThread.Start(); } ServerRunning = true; }
public void Start() { _ventilator = _context.CreateSocket(SocketType.PUSH); _ventilator.Bind("inproc://ventilator"); }
/// <summary> /// +------------+ _____ /// W <--> | ROUTER | / /// W <--> o|B F|o <----- /// W <--> | DEALER | \_____ /// +------------+ /// </summary> private void PrepareSockets() { // Create frontend/router socket _frontendSocket = ServiceContext.CreateSocket(SocketType.ROUTER); _frontendSocket.ReceiveReady += new EventHandler<SocketEventArgs>(_frontendSocket_ReceiveReady); _frontendSocket.Linger = TimeSpan.FromMilliseconds(0); _frontendSocket.Bind(String.Format("tcp://{0}:{1}", _frontendAddress, _frontendPort)); _frontendSocket.SendReady += new EventHandler<SocketEventArgs>(_frontendSocket_SendReady); // Create backend/dealer socket _backendSocket = ServiceContext.CreateSocket(SocketType.DEALER); _backendSocket.ReceiveReady += new EventHandler<SocketEventArgs>(_backendSocket_ReceiveReady); _backendSocket.Linger = TimeSpan.FromMilliseconds(0); _backendSocket.Bind(String.Format("tcp://{0}:{1}", _backendAddress, _backendPort)); _backendSocket.SendReady += new EventHandler<SocketEventArgs>(_backendSocket_SendReady); }
private static void Start(Object cancelationToken) { router = context.CreateSocket(SocketType.ROUTER); dealer = context.CreateSocket(SocketType.DEALER); routerEndpoint = String.Format("inproc://router"); workersEndpoint = String.Format("inproc://workers"); router.Bind(routerEndpoint); router.ReceiveReady += new EventHandler<SocketEventArgs>(router_ReceiveReady); dealer.Bind(workersEndpoint); dealer.ReceiveReady += new EventHandler<SocketEventArgs>(dealer_ReceiveReady); Poller poller = new Poller(new[] { router, dealer }); var token = (CancellationToken)cancelationToken; workerThreads = new Thread[1]; for (int threadId = 0; threadId < workerThreads.Length; threadId++) { workerThreads[threadId] = new Thread(WorkerRoutine); workerThreads[threadId].Start(context); } senderThreads = new Thread[1]; for (int threadId = 0; threadId < senderThreads.Length; threadId++) { senderThreads[threadId] = new Thread(SenderRoutine); senderThreads[threadId].Start(token); } while (!token.IsCancellationRequested) { poller.Poll(TimeSpan.FromMilliseconds(100)); } // to close asockets we have to wait finishing of current operation first }