/// <summary> /// Dispose managed resources. /// </summary> /// <param name="disposing">Is desposing.</param> protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { CancelPolling(); if (pubSocket != null) { pubSocket.Disconnect(PublisherAddress); pubSocket.Dispose(); pubSocket = null; } if (poller != null) { poller.Dispose(); poller = null; } if (context != null) { context.Terminate(); context.Dispose(); context = null; } if (source != null) { source.Dispose(); source = null; } } // Shared cleanup logic. disposed = true; } }
public void Start() { if (context == null) { context = NetMQContext.Create(); publisher = context.CreatePublisherSocket(); publisher.Bind("tcp://*:5252"); receiver = context.CreatePullSocket(); receiver.Bind("tcp://*:5253"); // Start listening.. Task.Run(() => { try { while (!tokenSource.IsCancellationRequested) { var message = receiver.ReceiveString(); var command = Serialization.DeserializeEnvelope <ContractMarker>(message); commandReceivedSubject.OnNext(command); } } catch (Exception ex) { Tracer.Error(ex); throw; } }); } }
private static void PublisherSocket() { Random rand = new Random(50); using var pubSocket = new PublisherSocket(); Console.WriteLine("Publisher socket binding..."); pubSocket.Options.SendHighWatermark = 1000; pubSocket.Bind("tcp://*:12345"); for (var i = 0; i < 100; i++) { var randomizedTopic = rand.NextDouble(); if (randomizedTopic > 0.5) { var msg = "TopicA msg-" + i; Console.WriteLine("Sending message : {0}", msg); pubSocket.SendMoreFrame("TopicA").SendFrame(msg); } else { var msg = "TopicB msg-" + i; Console.WriteLine("Sending message : {0}", msg); pubSocket.SendMoreFrame("TopicB").SendFrame(msg); } Thread.Sleep(500); } }
public void Execute() { using (var jobServerValidationBackend = new PublisherSocket(PUBLISHER)) using (var jobServerFrontend = new SubscriberSocket(JOB_SERVER_FRONTEND_ENDPOINT)) { jobServerFrontend.Subscribe("init"); Console.WriteLine("Job server ONNNNNNNNNNNNNNNNNN"); Console.WriteLine($"Publisher running on: {PUBLISHER}"); Console.WriteLine($"Listening to: {JOB_SERVER_FRONTEND_ENDPOINT}"); while (true) { try { var topic = jobServerFrontend.ReceiveFrameString(); var event_job_message = jobServerFrontend.ReceiveFrameString(); var envelope = Newtonsoft.Json.JsonConvert.DeserializeObject <Envelope>(event_job_message); Console.WriteLine($"Receive: {envelope.Identity}"); jobServerValidationBackend.SendMoreFrame("validation").SendFrame(event_job_message); } catch (Exception ex) //Drop message { Console.WriteLine("Erro na validação do dado: " + ex.Message); } } } }
public void SetPgmSettings() { const int MegaBit = 1024; const int MegaByte = 1024; using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { pub.Options.MulticastHops = 2; pub.Options.MulticastRate = 40 * MegaBit; // 40 megabit pub.Options.MulticastRecoveryInterval = TimeSpan.FromMinutes(10); pub.Options.SendBuffer = MegaByte * 10; // 10 megabyte pub.Connect("pgm://224.0.0.1:5555"); sub.Options.ReceiveBuffer = MegaByte * 10; sub.Bind("pgm://224.0.0.1:5555"); sub.Subscribe(""); pub.SendFrame("Hi"); bool more; Assert.AreEqual("Hi", sub.ReceiveFrameString(out more)); Assert.IsFalse(more); Assert.AreEqual(2, pub.Options.MulticastHops); Assert.AreEqual(40 * MegaBit, pub.Options.MulticastRate); Assert.AreEqual(TimeSpan.FromMinutes(10), pub.Options.MulticastRecoveryInterval); Assert.AreEqual(MegaByte * 10, pub.Options.SendBuffer); Assert.AreEqual(MegaByte * 10, sub.Options.ReceiveBuffer); } }
public void LargeMessage() { using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { pub.Connect("pgm://224.0.0.1:5555"); sub.Bind("pgm://224.0.0.1:5555"); sub.Subscribe(""); var data = new byte[3200]; // this should be at least 3 packets for (Int16 i = 0; i < 1600; i++) { Array.Copy(BitConverter.GetBytes(i), 0, data, i * 2, 2); } pub.SendFrame(data); byte[] message = sub.ReceiveFrameBytes(); Assert.AreEqual(3200, message.Length); for (Int16 i = 0; i < 1600; i++) { Assert.AreEqual(i, BitConverter.ToInt16(message, i * 2)); } } }
public ActorPublisher() { actor_id = random_string(5); var context = NetMQContext.Create(); pub = context.CreatePublisherSocket(); pub.Connect("tcp://localhost:5012"); }
static void Main(string[] args) { Random rand = new Random(50); using (var pubSocket = new PublisherSocket()) { Console.WriteLine("Publisher socket binding..."); pubSocket.Options.SendHighWatermark = 100; pubSocket.Bind("tcp://localhost:12345"); Thread.Sleep(200); pubSocket.SendMoreFrame("").SendFrame("1231231231313123131231"); //for (var i = 0; i < 300; i++) //{ // var randomizedTopic = rand.NextDouble(); // if (randomizedTopic > 0.5) // { // var msg = "TopicA msg-" + i; // Console.WriteLine("Sending message : {0}", msg); // pubSocket.SendMoreFrame("TopicA").SendFrame(msg); // } // else // { // var msg = "TopicB msg-" + i; // Console.WriteLine("Sending message : {0}", msg); // pubSocket.SendMoreFrame("TopicB").SendFrame(msg); // } // Thread.Sleep(100); //} } }
public void LargerBufferLength() { var largerBuffer = new byte[256]; { largerBuffer[124] = 0xD; largerBuffer[125] = 0xE; largerBuffer[126] = 0xE; largerBuffer[127] = 0xD; } using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub.Connect("tcp://127.0.0.1:" + port); sub.Subscribe(""); Thread.Sleep(100); pub.SendFrame(largerBuffer, 128); byte[] recvMesage = sub.ReceiveFrameBytes(); Assert.AreEqual(128, recvMesage.Length); Assert.AreEqual(0xD, recvMesage[124]); Assert.AreEqual(0xE, recvMesage[125]); Assert.AreEqual(0xE, recvMesage[126]); Assert.AreEqual(0xD, recvMesage[127]); Assert.AreNotEqual(largerBuffer.Length, recvMesage.Length); } }
private void TaskDataPub(CancellationToken cancelToken, BlockingCollection <object> queue, string address) { using (var pubSocket = new PublisherSocket()) { //pubSocket.Options.SendHighWatermark = 1; pubSocket.Connect(address); while (!cancelToken.IsCancellationRequested) { if (queue.TryTake(out object data, 500)) { switch (data.GetType().Name) { case "ClientHeartbeat": pubSocket.Serialise <ClientHeartbeat>(data); break; case "ClientPositionUpdate": pubSocket.Serialise <ClientPositionUpdate>(data); break; case "ClientFrequencyUpdate": pubSocket.Serialise <ClientFrequencyUpdate>(data); break; } } } } taskDataPub = null; }
/// <summary> /// Initialize instance /// </summary> public override void Initialize(MessageInstance messageInstance) { //We always have subscribers HasSubscribers = true; //Check if port is defined var foundport = Config.TryGetEnvVariable("NetMQRunnerPort", Config.GlobalConfig.NetMQRunnerPort); if (string.IsNullOrWhiteSpace(foundport) || !int.TryParse(foundport, out var port)) { throw new Exception($"Could not find port number in configuration for setting up outbound event messages. Please set the port number in Global config or via environment variable."); } //Check if port is available for usage if (!Util.CheckPortAvailability(port)) { throw new Exception($"Port with number {port} is unavailable. Cannot start EventRunner, please check your firewall settings or if the port is already in use by another application"); } //Initialize socket _socket = new PublisherSocket("@tcp://*:" + port); //Set default manipulation logic (so we only send deltas) EventKeeper.SetManipulationLogic(EventMessageType.PerformanceInfo, EventKeeper.DefaultManipulationLogic(EventMessageType.PerformanceInfo)); //Send data asap (1ms) MinWait = 1; }
public void Start() { _messageHost?.Start(); lock (_publishSocketLocker) { _publishSocket = new PublisherSocket(); _publishSocket.Connect(_publishAddress); } _subscribeThreadCancellation = new CancellationTokenSource(); _pingThreadCancellation = new CancellationTokenSource(); _subscribeThread = new Thread(SubscribeThread) { IsBackground = true }; _subscribeThread.Start(); _pingThread = new Thread(PingThread) { IsBackground = true }; _pingThread.Start(); // Wait until the subscribe thread has actually subscribed before continuing. // For some reason a "reset event" doesn't work here. while (!_subscribeThreadRunning) { Thread.Sleep(50); } }
public void BindRandomThenUnbind() { using (var pub = new PublisherSocket()) { var port = pub.BindRandomPort("tcp://localhost"); pub.Unbind("tcp://localhost:" + port); } using (var pub = new PublisherSocket()) { var port = pub.BindRandomPort("tcp://*"); pub.Unbind("tcp://*:" + port); } using (var pub = new PublisherSocket()) { var port1 = pub.BindRandomPort("tcp://*"); var port2 = pub.BindRandomPort("tcp://*"); var port3 = pub.BindRandomPort("tcp://*"); pub.Unbind("tcp://*:" + port1); pub.Unbind("tcp://*:" + port2); pub.Unbind("tcp://*:" + port3); } }
public void HandleMessage(Message message, RouterSocket shellSocket, PublisherSocket ioPubSocket) { var executeRequest = JsonSerializer.Deserialize <ExecuteRequest>(message.Content); Log.Info($"Execute Request received with code {executeRequest.Code}"); // Kernel sends a "status: busy" message on IOPub SendMessageToIoPub(message, ioPubSocket, StatusValues.Busy); // Kernel SendInputMessageToIoPub(message, ioPubSocket, executeRequest.Code); // 3: Evaluate the C# code var result = InteractiveShell.ExecuteCode(executeRequest.Code); // 4: Send execute reply to shell socket SendExecuteReplyMessage(message, shellSocket); // 5: Send execute result message to IOPub SendOutputMessageToIoPub(message, ioPubSocket, result); // 6: Send IDLE status message to IOPub SendMessageToIoPub(message, ioPubSocket, StatusValues.Idle); _executionCount += 1; }
private void Run() { using var publisher = new PublisherSocket(); publisher.Bind(@"tcp://127.0.0.1:17232"); using var subscriber = new SubscriberSocket(); subscriber.Bind(@"tcp://127.0.0.1:17233"); subscriber.SubscribeToAnyTopic(); running.Set(); using var server = new TelnetServer(IPAddress.Any, port, publisher, subscriber) { OptionReuseAddress = true }; Log.Information("Server starting ..."); server.Start(); while (running.WaitOne(TimeSpan.Zero)) { if (subscriber.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(100), out var frame)) { server.Multicast(frame); } } server.Stop(); Log.Information("Server stopped."); }
public async Task Scenario1() { using var server = new PublisherSocket(); using var client = new SubscriberSocket(); var url = "ipc://hello/world"; server.Listen(url); client.Dial(url); client.SubscribeToAllMessages(); var data = Enumerable.Range(0, 10); var send = Task.Run(async() => { foreach (var x in data) { var message = new Message(); message.SetStruct(x); _testOutputHelper.WriteLine($"Send: {x}"); await server.SendMessageAsync(message); } }); var receive = Task.Run(async() => { var count = 0; while (count++ < 10) { var message = await client.ReceiveMessageAsync(); var value = message.GetStruct <int>(); _testOutputHelper.WriteLine($"Received: {value}"); } }); await Task.WhenAll(send, receive); }
/// <summary> /// Initializes a new instance of the <see cref="NetMQWriter"/> class. /// </summary> /// <param name="pipeline">Pipeline to which this component belongs</param> /// <param name="address">Connection string</param> /// <param name="serializer">Format serializer with which messages are serialized</param> public NetMQWriter(Pipeline pipeline, string address, IFormatSerializer serializer) { this.pipeline = pipeline; this.serializer = serializer; this.socket = new PublisherSocket(); pipeline.PipelineRun += (s, e) => this.socket.Bind(address); }
public async Task ShouldSubscribeToSpecificSubject() { using (var publisherSocket = new PublisherSocket()) { publisherSocket.Bind(ToSubscribersEndpoint); var createEvent = new Func <string, string, Task>(async(streamId, market) => { var @event = new ChangeCcyPairPrice(streamId, market, 0.0, 0.0, 0.0, 0.0); var message = _eventSerializer.ToProducerMessage(@event); var eventId = new EventId(streamId, 0, string.IsNullOrEmpty(market) ? streamId : $"{streamId}.{market}", DateTime.Now.Ticks); publisherSocket.SendMoreFrame(message.Subject) .SendMoreFrame(_serializer.Serialize(eventId)) .SendFrame(_serializer.Serialize(message)); await Task.Delay(500); }); var subscribedToStreamId = "EUR/USD"; var subscribedToMarket = "Harmony"; var NOTsubscribedToStreamId = "EUR/GBP"; var NOTsubscribedToMarket = "FxConnect"; var cacheConfiguration = new DynamicCacheConfiguration(ToSubscribersEndpoint, StateOfTheWorldEndpoint, HeartbeatEndpoint) { Subject = $"{subscribedToStreamId}.{subscribedToMarket}", HeartbeatDelay = TimeSpan.FromSeconds(1), HeartbeatTimeout = TimeSpan.FromSeconds(1), }; var cache = new DynamicCache <string, CurrencyPair>(cacheConfiguration, LoggerForTests <DynamicCache <string, CurrencyPair> > .Default(), _eventSerializer); await cache.Run(); await Task.Delay(1000); await createEvent(NOTsubscribedToStreamId, NOTsubscribedToMarket); Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count()); await createEvent(NOTsubscribedToStreamId, subscribedToMarket); Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count()); await createEvent(subscribedToStreamId, NOTsubscribedToMarket); Assert.AreEqual(0, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count()); await createEvent(subscribedToStreamId, subscribedToMarket); Assert.AreEqual(1, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count()); await createEvent(subscribedToStreamId, string.Empty); Assert.AreEqual(1, cache.Items.SelectMany(ccy => ccy.AppliedEvents).Count()); } }
private static void Main() { Console.Title = "NetMQ Weather Update Server"; bool stopRequested = false; // Wire up the CTRL+C handler Console.CancelKeyPress += (sender, e) => stopRequested = true; Console.WriteLine("Publishing weather updates..."); using (var publisher = new PublisherSocket()) { publisher.Bind("tcp://127.0.0.1:5556"); var rng = new Random(); while (!stopRequested) { int zipcode = rng.Next(0, 99999); int temperature = rng.Next(-80, 135); int relhumidity = rng.Next(0, 90); publisher.SendFrame($"{zipcode} {temperature} {relhumidity}"); } } }
public Shell( ICommandScheduler <JupyterRequestContext> scheduler, ConnectionInformation connectionInformation) { if (connectionInformation == null) { throw new ArgumentNullException(nameof(connectionInformation)); } _scheduler = scheduler ?? throw new ArgumentNullException(nameof(scheduler)); _shellAddress = $"{connectionInformation.Transport}://{connectionInformation.IP}:{connectionInformation.ShellPort}"; _ioPubAddress = $"{connectionInformation.Transport}://{connectionInformation.IP}:{connectionInformation.IOPubPort}"; _stdInAddress = $"{connectionInformation.Transport}://{connectionInformation.IP}:{connectionInformation.StdinPort}"; _controlAddress = $"{connectionInformation.Transport}://{connectionInformation.IP}:{connectionInformation.ControlPort}"; var signatureAlgorithm = connectionInformation.SignatureScheme.Replace("-", string.Empty).ToUpperInvariant(); _signatureValidator = new SignatureValidator(connectionInformation.Key, signatureAlgorithm); _shell = new RouterSocket(); _ioPubSocket = new PublisherSocket(); _stdIn = new RouterSocket(); _control = new RouterSocket(); _shellSender = new MessageSender(_shell, _signatureValidator); _ioPubSender = new MessageSender(_ioPubSocket, _signatureValidator); _disposables = new CompositeDisposable { _shell, _ioPubSocket, _stdIn, _control }; }
public static void Publish() { using (var pub = new PublisherSocket()) { bool usePlain = true; if (usePlain) { pub.Options.PlainUsername = "******"; pub.Options.PlainPassword = "******"; pub.Options.PlainServer = true; } pub.Bind("tcp://192.168.0.193:6021"); int i = 0; while (true) { Thread.Sleep(500); pub.SendMoreFrame("A").SendFrame("Hello - " + i++); Console.WriteLine("SENDING MESSAGE on port 6021"); } Console.WriteLine(); Console.Write("Press any key to exit..."); Console.ReadKey(); } }
public void HandleMessage(Message message, RouterSocket serverSocket, PublisherSocket ioPub) { Message replyMessage = new Message(MessageType.KernelInfoReply, CreateKernelInfoReply(), message.Header); _logger.LogInformation("Sending kernel_info_reply"); serverSocket.SendMessage(replyMessage); }
public void MultipleLargeMessages() { var largeMessage = new byte[12000]; for (int i = 0; i < 12000; i++) { largeMessage[i] = (byte)(i % 256); } using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub.Connect("tcp://127.0.0.1:" + port); sub.Subscribe(""); Thread.Sleep(1000); pub.SendFrame(""); sub.SkipFrame(); for (int i = 0; i < 100; i++) { pub.SendFrame(largeMessage); byte[] recvMesage = sub.ReceiveFrameBytes(); for (int j = 0; j < 12000; j++) { Assert.AreEqual(largeMessage[j], recvMesage[j]); } } } }
private static void XPublisherSocket() { using var pubSocket = new PublisherSocket(">tcp://127.0.0.1:5678"); Console.WriteLine("Publisher socket connecting..."); pubSocket.Options.SendHighWatermark = 1000; var rand = new Random(50); while (true) { var randomizedTopic = rand.NextDouble(); if (randomizedTopic > 0.5) { var msg = randomizedTopic.ToString(); Console.WriteLine("Sending message : {0}", msg); pubSocket.SendMoreFrame($"{TopicEvnet.YcYxSetChangeEvent}/1").SendFrame(msg); } else { var msg = "TopicB msg-" + randomizedTopic; Console.WriteLine("Sending message : {0}", msg); pubSocket.SendMoreFrame("TopicB").SendFrame(msg); } Thread.Sleep(500); } }
public EventsBus(String bind) { Server = new PublisherSocket(); Server.Options.SendHighWatermark = 5000; BindAddress = bind; }
public void TwoPublishers() { using (var pub = new PublisherSocket()) using (var pub2 = new PublisherSocket()) using (var sub = new SubscriberSocket()) { pub.Connect("pgm://224.0.0.1:5555"); pub2.Connect("pgm://224.0.0.1:5555"); sub.Bind("pgm://224.0.0.1:5555"); sub.Subscribe(""); pub.SendFrame("Hi"); bool more; Assert.AreEqual("Hi", sub.ReceiveFrameString(out more)); Assert.IsFalse(more); pub2.SendFrame("Hi2"); Assert.AreEqual("Hi2", sub.ReceiveFrameString(out more)); Assert.IsFalse(more); } }
public void PubSub() { ThreadPool.QueueUserWorkItem(state => { var server = new PublisherSocket(); server.Bind("tcp://*:5557"); while (!_stopLoops) { try { var dataFacadeEvent = new DataFacadeEvent { State = new List <User> { new User { Id = 666 } } }; using (var responseStream = new MemoryStream()) { Serializer.Serialize(responseStream, dataFacadeEvent); server.SendFrame(responseStream.ToArray()); } Thread.Sleep(5000); } catch (Exception e) { Console.WriteLine(e); } } }); }
private void mq_test() { mq_end = true; time_cnt = 0; try { using (var publisher = new PublisherSocket()) { publisher.Connect("tcp://127.0.0.1:5550"); while (mq_end) { int x = (int)(100 * Math.Sin(time_cnt)); int y = 0;//(int)obj1; this.chkPx.Location = new System.Drawing.Point(x + 150, 257 + y); byte[][] bytes = { Encoding.ASCII.GetBytes("foo"), Encoding.ASCII.GetBytes($"{x}") }; publisher.SendMultipartBytes(bytes); time_cnt += 0.05; Thread.Sleep(10); } } } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void PreparePublishing() { AsyncIO.ForceDotNet.Force(); pubSocket = new PublisherSocket(); pubSocket.Options.SendHighWatermark = 100; pubSocket.Bind("tcp://localhost:12344"); }
private static void Main() { Console.Title = "NetMQ Weather Update Server"; bool stopRequested = false; // Wire up the CTRL+C handler Console.CancelKeyPress += (sender, e) => stopRequested = true; Console.WriteLine("Publishing weather updates..."); using (var publisher = new PublisherSocket()) { publisher.Bind("tcp://127.0.0.1:5556"); var rng = new Random(); while (!stopRequested) { int zipcode = rng.Next(0, 99999); int temperature = rng.Next(-80, 135); int relhumidity = rng.Next(0, 90); publisher.SendFrame(string.Format("{0} {1} {2}", zipcode, temperature, relhumidity)); } } }
public void RequestResponseMultipartMessageWithRetryFails() { const string address = "tcp://127.0.0.1:50002"; const string pubAddress = "tcp://127.0.0.1:60002"; const int numTries = 5; var requestTimeout = TimeSpan.FromMilliseconds(100); var requestMessage = new NetMQMessage(1); requestMessage.Append("Hi"); using (var progressPublisher = new PublisherSocket(pubAddress)) using (var progressSubscriber = new SubscriberSocket(pubAddress)) using (var server = new RouterSocket(address)) { progressSubscriber.SubscribeToAnyTopic(); var progressProactor = new NetMQProactor(progressSubscriber, (socket, message) => Console.WriteLine("C: {0} {1:ss.fff}", message[0].ConvertToString(), DateTime.Now)); var serverProactor = new NetMQProactor(server, (socket, message) => { Console.WriteLine("ResponseEcho recieved message {0} at {1:ss.fff}", message[2].ConvertToString(), DateTime.Now); }); using (serverProactor) using (progressProactor) { var responseMessage = RequestSocket.RequestResponseMultipartMessageWithRetry(address, requestMessage, numTries, requestTimeout, progressPublisher); Assert.IsNull(responseMessage); } } }
public Kernel(ConnectionInformation connectionInformation) { this.connectionInformation = connectionInformation; // heartbeat hbSocket = new RouterSocket(); hbSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.hb_port}"); // control controlSocket = new RouterSocket(); controlSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.control_port}"); // stdin stdinSocket = new RouterSocket(); stdinSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.stdin_port}"); // iopub ioSocket = new PublisherSocket(); ioSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.iopub_port}"); // shell shellSocket = new RouterSocket(); shellSocket.Bind($"{connectionInformation.transport}://{connectionInformation.ip}:{connectionInformation.shell_port}"); payload = new List <Payload>(); nugetManager = new NuGetManager(new FileInfo(".").FullName); executionCount = 0; lastMessage = null; headerCode = getHeaderCode(); hmac = new HMACSHA256(Encoding.UTF8.GetBytes(connectionInformation.key)); }
public void SendErrorMessageToIOPub(Message message, PublisherSocket ioPub, JObject errorContent) { Message executeReplyMessage = MessageBuilder.CreateMessage(MessageTypeValues.Error, errorContent, message.Header); this.logger.Info(string.Format("Sending message to IOPub {0}", JsonSerializer.Serialize(executeReplyMessage))); this.messageSender.Send(executeReplyMessage, ioPub); }
public void Start(int publishPort) { ctx = NetMQContext.Create(); publishSocket = ctx.CreatePublisherSocket(); publishSocket.Bind("tcp://*:" + publishPort); logger.Info("Message publisher started on port " + publishPort); }
public void InsertNetMQ(string didiStr) { using (var pubSocket = new PublisherSocket()) { pubSocket.Options.SendHighWatermark = 10000; pubSocket.Bind("tcp://localhost:12345"); Thread.Sleep(500); pubSocket.SendMoreFrame("").SendFrame(didiStr); } }
public Shell(ILog logger,string addressShell, string addressIOPub, NetMQContext context, Dictionary<string, IShellMessageHandler> messageHandlers) { this.logger = logger; this.addressShell = addressShell; this.addressIOPub = addressIOPub; this.context = context; this.messageHandlers = messageHandlers; this.server = this.context.CreateRouterSocket(); this.ioPubSocket = this.context.CreatePublisherSocket(); this.stopEvent = new ManualResetEventSlim(); }
public ReactiveServer() { var context = NetMQContext.Create(); repSocket = context.CreateResponseSocket(); repSocket.Bind("tcp://127.0.0.1:3334"); repSocket.ReceiveReady += RepSocketOnReceiveReady; var repThread = new Thread(RepThread); repThread.Start(); pubSocket = context.CreatePublisherSocket(); pubSocket.Bind("tcp://127.0.0.1:3333"); }
static void Main(string[] args) { Console.Title = "NetMQ LazyPirate Client 2"; const string serverAddress = "tcp://127.0.0.1:5555"; const string requestString = "Hi"; var requestTimeout = TimeSpan.FromMilliseconds(2500); var requestRetries = 10; var requestMessage = new NetMQMessage(1); requestMessage.Append("Hi"); using (var progressPublisher = new PublisherSocket()) { const string pubSubAddress = "tcp://127.0.0.1:5556"; progressPublisher.Bind(pubSubAddress); SubscriberContinuousLoop(pubSubAddress, requestString); while (true) { var responseString = RequestSocket.RequestResponseMultipartMessageWithRetry(serverAddress, requestMessage, requestRetries, requestTimeout, progressPublisher); } } }
private void SenderWorker() { _mqContext = NetMQContext.Create(); _publisherSocket = _mqContext.CreatePublisherSocket(); if (Protocol == "tcp") _publisherSocket.Connect(_address); if (Protocol == "pgm") //Multicast { _publisherSocket.Options.MulticastHops = 4; _publisherSocket.Options.MulticastRate = 40 * 1024; // 40 megabit _publisherSocket.Options.MulticastRecoveryInterval = TimeSpan.FromMinutes(10); _publisherSocket.Options.SendBuffer = 1024 * 10; // 10 megabyte _publisherSocket.Bind(_address); } _publisherSocket.SendReady += (s, a) => { }; }
public void Terminate() { _disposed = true; if (_publisherSocket != null) { _publisherSocket.Dispose(); _publisherSocket = null; } if (_mqContext != null) { _mqContext.Terminate(); _mqContext = null; } if (_workerReceiverThread != null) { _workerReceiverThread.Abort(); _workerReceiverThread = null; } if (_workerSenderThread != null) { _workerSenderThread.Abort(); _workerSenderThread = null; } }
private void RunActor(PairSocket shim) { // save the shim to the class to use later m_shim = shim; // create all subscriber, publisher and beacon using (m_subscriber = new SubscriberSocket()) using (m_publisher = new PublisherSocket()) using (m_beacon = new NetMQBeacon()) { // listen to actor commands m_shim.ReceiveReady += OnShimReady; // subscribe to all messages m_subscriber.Subscribe(""); // we bind to a random port, we will later publish this port // using the beacon m_randomPort = m_subscriber.BindRandomPort("tcp://*"); Console.WriteLine("Bus subscriber is bound to {0}", m_subscriber.Options.LastEndpoint); // listen to incoming messages from other publishers, forward them to the shim m_subscriber.ReceiveReady += OnSubscriberReady; // configure the beacon to listen on the broadcast port Console.WriteLine("Beacon is being configured to UDP port {0}", m_broadcastPort); m_beacon.Configure(m_broadcastPort); // publishing the random port to all other nodes Console.WriteLine("Beacon is publishing the Bus subscriber port {0}", m_randomPort); m_beacon.Publish(m_randomPort.ToString(), TimeSpan.FromSeconds(1)); // Subscribe to all beacon on the port Console.WriteLine("Beacon is subscribing to all beacons on UDP port {0}", m_broadcastPort); m_beacon.Subscribe(""); // listen to incoming beacons m_beacon.ReceiveReady += OnBeaconReady; // Create a timer to clear dead nodes NetMQTimer timer = new NetMQTimer(TimeSpan.FromSeconds(1)); timer.Elapsed += ClearDeadNodes; // Create and configure the poller with all sockets and the timer m_poller = new NetMQPoller { m_shim, m_subscriber, m_beacon, timer }; // signal the actor that we finished with configuration and // ready to work m_shim.SignalOK(); // polling until cancelled m_poller.Run(); } }
/// <summary> /// the broker setting up the cluster /// /// /// State 2 ---+ +--- State n /// | | /// +----+----+ /// client 1 ---| | |--- worker 1 /// client 2 ---+---- BROKER 1 ----+--- worker 2 /// : | | | : /// client n ---+ +----+----+ +--- worker n /// | | /// BROKER 2 BROKER n /// /// BROKER 2 and n are not included and must be setup separately /// /// A minimum of two address must be supplied /// </summary> /// <param name="args">[0] = this broker's address /// [1] = 1st peer's address /// : /// [n] = nth peer address</param> /// <remarks> /// since "inproc://" is not working in NetMQ we use "tcp://" /// for each broker we need 5 ports which for this example are /// assigned as follows (in true life it should be configurable whether /// they are ports or tcp/ip addresses) /// /// this brokers address => local frontend binds to tcp://127.0.0.1:5555 /// cloud frontend binds to :5556 /// local backend binds to :5557 /// state backend binds to :5558 /// monitor PULL binds to :5559 /// /// the sockets are connected as follows /// /// this broker's monitor PUSH connects to tcp://127.0.0.1:5559 /// /// (if peer's address and port is tcp://127.0.0.1:5575) /// /// this broker's cloud backend connects to :5576 /// this broker's state frontend connects to :5578 /// /// this scheme is fix in this example /// </remarks> public static void Main(string[] args) { Console.Title = "NetMQ Inter-Broker Router"; const string baseAddress = "tcp://127.0.0.1:"; if (args.Length < 2) { Console.WriteLine("usage: program me peer1 [peer]*"); Console.WriteLine("each broker needs 5 port for his sockets!"); Console.WriteLine("place enough distance between multiple broker addresses!"); Environment.Exit(-1); } // trapping Ctrl+C as exit signal! Console.CancelKeyPress += (s, e) => { e.Cancel = true; s_keepRunning = false; }; // get random generator for later use var rnd = new Random(); // get list for registering the clients var clients = new List<byte[]>(NbrClients); // get a list of peer addresses var peers = new List<byte[]>(); // get all peer addresses - first is this broker! for (var i = 1; i < args.Length; i++) peers.Add(Encoding.UTF8.GetBytes(args[i])); // build this broker's address var me = baseAddress + args[0]; // get the port as integer for later use var myPort = int.Parse(args[0]); Console.WriteLine("[BROKER] The broker can be stopped by using CTRL+C!"); Console.WriteLine("[BROKER] setting up sockets ..."); // set up all the addresses needed in the due course var localFrontendAddress = me; var cloudFrontendAddress = baseAddress + (myPort + 1); var localBackendAddress = baseAddress + (myPort + 2); var stateBackendAddress = baseAddress + (myPort + 3); var monitorAddress = baseAddress + (myPort + 4); // create the context and all the sockets using (var localFrontend = new RouterSocket()) using (var localBackend = new RouterSocket()) using (var cloudFrontend = new RouterSocket()) using (var cloudBackend = new RouterSocket()) using (var stateBackend = new PublisherSocket()) using (var stateFrontend = new SubscriberSocket()) using (var monitor = new PullSocket()) { // give every socket an unique identity, e.g. LocalFrontend[Port] SetIdentities(myPort, localFrontend, cloudFrontend, localBackend, stateBackend, monitor, cloudBackend, stateFrontend); // subscribe to any message on the stateFrontend socket! stateFrontend.Subscribe(""); // bind the serving sockets localFrontend.Bind(localFrontendAddress); cloudFrontend.Bind(cloudFrontendAddress); localBackend.Bind(localBackendAddress); stateBackend.Bind(stateBackendAddress); monitor.Bind(monitorAddress); // connect sockets to peers for (var i = 1; i < args.Length; i++) { // build the cloud back end address var peerPort = int.Parse(args[i]); var address = baseAddress + (peerPort + 1); Console.WriteLine("[BROKER] connect to cloud peer {0}", address); // this cloudBackend connects to all peer cloudFrontends cloudBackend.Connect(address); // build the state front end address address = baseAddress + (peerPort + 3); Console.WriteLine("[BROKER] subscribe to state peer {0}", address); // this stateFrontend to all peer stateBackends stateFrontend.Connect(address); } // setup the local worker queue for LRU and monitor cloud capacity var workerQueue = new Queue<byte[]>(); int previousLocalCapacity = 0; // receive the capacity available from other peer(s) stateFrontend.ReceiveReady += (s, e) => { // the message should contain the available cloud capacity var capacity = e.Socket.ReceiveFrameString(); Debug.Assert(string.IsNullOrWhiteSpace(capacity), "StateFrontend: message was empty!"); int couldCapacity; Debug.Assert(int.TryParse(capacity, out couldCapacity), "StateFrontend: message did not contain a number!"); }; // get the status message and print it monitor.ReceiveReady += (s, e) => { var msg = e.Socket.ReceiveFrameString(); Console.WriteLine("[MONITOR] {0}", msg); }; // all local clients are connecting to this socket // they send a REQ and get a REPLY localFrontend.ReceiveReady += (s, e) => { // [client adr][empty][message id] var request = e.Socket.ReceiveMultipartMessage(); // register the local client for later identification if not known if (!clients.Any(n => AreSame(n, request[0]))) clients.Add(request[0].Buffer); // if we have local capacity send worker else send to cloud if (workerQueue.Count > 0) { // get the LRU worker adr var worker = workerQueue.Dequeue(); // wrap message with workers address var msg = Wrap(worker, request); // send message to the worker // [worker adr][empty][client adr][empty][data] localBackend.SendMultipartMessage(msg); } else { // get an random index for peers var peerIdx = rnd.Next(peers.Count - 2) + 2; // get peers address var peer = peers[peerIdx]; // wrap message with peer's address var msg = Wrap(peer, request); // [peer adr][empty][client adr][empty][data] cloudBackend.SendMultipartMessage(msg); } }; // the workers are connected to this socket // we get a REPLY either for a cloud client [worker adr][empty][peer adr][empty][peer client adr][empty][data] // or local client [worker adr][empty][client adr][empty][data] // or a READY message [worker adr][empty][WORKER_READY] localBackend.ReceiveReady += (s, e) => { // a worker can send "READY" or a request // or an REPLAY var msg = e.Socket.ReceiveMultipartMessage(); // just to make sure we received a proper message Debug.Assert(msg != null && msg.FrameCount > 0, "[LocalBackend] message was empty or frame count == 0!"); // get the workers identity var id = Unwrap(msg); // this worker done in either way so add it to available workers workerQueue.Enqueue(id); // if it is NOT a ready message we need to route the message // it could be a reply to a peer or a local client // [WORKER_READY] or [client adr][empty][data] or [peer adr][empty][peer client adr][empty][data] if (msg[0].Buffer[0] != WorkerReady) { Debug.Assert(msg.FrameCount > 2, "[LocalBackend] None READY message malformed"); // if the adr (first frame) is any of the clients send the REPLY there // and send it to the peer otherwise if (clients.Any(n => AreSame(n, msg.First))) localFrontend.SendMultipartMessage(msg); else cloudFrontend.SendMultipartMessage(msg); } }; // this socket is connected to all peers // we receive either a REQ or a REPLY form a peer // REQ [peer adr][empty][peer client adr][empty][message id] -> send to peer for processing // REP [peer adr][empty][client adr][empty][message id] -> send to local client cloudBackend.ReceiveReady += (s, e) => { var msg = e.Socket.ReceiveMultipartMessage(); // just to make sure we received a message Debug.Assert(msg != null && msg.FrameCount > 0, "[CloudBackend] message was empty or frame count == 0!"); // we need the peers address for proper addressing var peerAdr = Unwrap(msg); // the remaining message must be at least 3 frames! Debug.Assert(msg.FrameCount > 2, "[CloudBackend] message malformed"); // if the id is any of the local clients it is a REPLY // and a REQ otherwise if (clients.Any(n => AreSame(n, msg.First))) { // [client adr][empty][message id] localFrontend.SendMultipartMessage(msg); } else { // add the peers address to the request var request = Wrap(peerAdr, msg); // [peer adr][empty][peer client adr][empty][message id] cloudFrontend.SendMultipartMessage(request); } }; // all peers are binding to this socket // we receive REPLY or REQ from peers // REQ [peer adr][empty][peer client adr][empty][data] -> send to local worker for processing // REP [peer adr][empty][client adr][empty][data] -> send to local client cloudFrontend.ReceiveReady += (s, e) => { var msg = e.Socket.ReceiveMultipartMessage(); // just to make sure we received a message Debug.Assert(msg != null && msg.FrameCount > 0, "[CloudFrontend] message was empty or frame count == 0!"); // we may need the peers address for proper addressing var peerAdr = Unwrap(msg); // the remaining message must be at least 3 frames! Debug.Assert(msg.FrameCount > 2, "[CloudFrontend] message malformed"); // if the address is any of the local clients it is a REPLY // and a REQ otherwise if (clients.Any(n => AreSame(n, msg.First))) localFrontend.SendMultipartMessage(msg); else { // in order to know which per to send back the peers adr must be added again var original = Wrap(peerAdr, msg); // reduce the capacity to reflect the use of a worker by a cloud request previousLocalCapacity = workerQueue.Count; // get the LRU worker var workerAdr = workerQueue.Dequeue(); // wrap the message with the worker address and send var request = Wrap(workerAdr, original); localBackend.SendMultipartMessage(request); } }; // in order to reduce chatter we only check to see if we have local capacity to provide to cloud // periodically every 2 seconds with a timer var timer = new NetMQTimer((int)TimeSpan.FromSeconds(2).TotalMilliseconds); timer.Elapsed += (t, e) => { // send message only if the previous send information changed if (previousLocalCapacity != workerQueue.Count) { // set the information previousLocalCapacity = workerQueue.Count; // generate the message var msg = new NetMQMessage(); var data = new NetMQFrame(previousLocalCapacity.ToString()); msg.Append(data); var stateMessage = Wrap(Encoding.UTF8.GetBytes(me), msg); // publish info stateBackend.SendMultipartMessage(stateMessage); } // restart the timer e.Timer.Enable = true; }; // start all clients and workers as threads var clientTasks = new Thread[NbrClients]; var workerTasks = new Thread[NbrWorker]; for (var i = 0; i < NbrClients; i++) { var client = new Client(localFrontendAddress, monitorAddress, (byte)i); clientTasks[i] = new Thread(client.Run) { Name = string.Format("Client_{0}", i) }; clientTasks[i].Start(); } for (var i = 0; i < NbrWorker; i++) { var worker = new Worker(localBackendAddress, (byte)i); workerTasks[i] = new Thread(worker.Run) { Name = string.Format("Worker_{0}", i) }; workerTasks[i].Start(); } // create poller and add sockets & timer var poller = new NetMQPoller { localFrontend, localBackend, cloudFrontend, cloudBackend, stateFrontend, stateBackend, monitor, timer }; // start monitoring the sockets poller.RunAsync(); // we wait for a CTRL+C to exit while (s_keepRunning) Thread.Sleep(100); Console.WriteLine("Ctrl-C encountered! Exiting the program!"); if (poller.IsRunning) poller.Stop(); poller.Dispose(); } }
/// <summary> /// sets unique identities for all sockets /// </summary> private static void SetIdentities( int myPort, RouterSocket localFrontend, RouterSocket cloudFrontend, RouterSocket localBackend, PublisherSocket stateBackend, PullSocket monitor, RouterSocket cloudBackend, SubscriberSocket stateFrontend) { localFrontend.Options.Identity = Encoding.UTF8.GetBytes("LocalFrontend[" + myPort + "]"); cloudFrontend.Options.Identity = Encoding.UTF8.GetBytes("CloudFrontend[" + (myPort + 1) + "]"); localBackend.Options.Identity = Encoding.UTF8.GetBytes("LocalBackend[" + (myPort + 2) + "]"); stateBackend.Options.Identity = Encoding.UTF8.GetBytes("StateBackend[" + (myPort + 3) + "]"); monitor.Options.Identity = Encoding.UTF8.GetBytes("Monitor[" + (myPort + 4) + "]"); cloudBackend.Options.Identity = Encoding.UTF8.GetBytes("CloudBackend"); stateFrontend.Options.Identity = Encoding.UTF8.GetBytes("StateFrontend"); }