Beispiel #1
0
        static void Main(string[] args)
        {
            Dictionary <string, List <string> > offerStorage = new Dictionary <string, List <string> >();

            Console.WriteLine("TypeClientAggregator");
            using (var server = new ResponseSocket())
                using (var emailer = new RequestSocket())
                {
                    server.Bind("tcp://*:5557");
                    emailer.Connect("tcp://127.0.0.1:5558");
                    while (true)
                    {
                        var message = server.ReceiveMultipartMessage(3);

                        Console.WriteLine("responseSocket : Server Received '{0}'-{1}", message[0].ConvertToString(Encoding.UTF8), message[1].ConvertToString(Encoding.UTF8));
                        string hashKey = GetHashString(message[0].ConvertToString(Encoding.UTF8) + message[1].ConvertToString(Encoding.UTF8));
                        if (offerStorage.ContainsKey(hashKey))
                        {
                            offerStorage[hashKey].Add(message[2].ConvertToString(Encoding.UTF8));
                            if (offerStorage[hashKey].Count >= 3)
                            {
                                mailOffer(emailer, message[0].ConvertToString(Encoding.UTF8), message[1].ConvertToString(Encoding.UTF8), offerStorage[hashKey]);
                                offerStorage.Remove(hashKey);
                            }
                        }
                        else
                        {
                            offerStorage.Add(hashKey, new List <string>());
                            offerStorage[hashKey].Add(message[2].ConvertToString(Encoding.UTF8));
                        }
                        server.SendFrameEmpty();
                    }
                }
        }
Beispiel #2
0
        /// <summary>
        /// protocol for NetMq
        /// the <message> should be a string send as the second frame and the message must not contain more that two frames or it will be discarded by the server
        /// "A" as first frame if you want <message> + " hello" as response
        /// "B" as first frame if you want "hello " + <message>
        /// </summary>

        static void Main(string[] args)
        {
            Console.WriteLine("NetMQ server have started");


            var server = new ResponseSocket();

            server.Bind("tcp://*:" + ListeningPort);
            while (true)
            {
                var message = server.ReceiveMultipartMessage();
                if (message.FrameCount == 2)
                {
                    var frameOne = message[0];
                    Console.WriteLine("recived message with protocol code: " + frameOne.ConvertToString());

                    //sending response
                    if (frameOne.ConvertToString() == "A")
                    {
                        server.SendFrame(message[1].ConvertToString() + " hello");
                        continue;
                    }
                    else if (frameOne.ConvertToString() == "B")
                    {
                        server.SendFrame(message[1].ConvertToString() + " hello");
                        continue;
                    }
                }

                //must only occure if the message is not valid according to the protocol
                server.SendFrame("INVALID FRAME COUNT!");
            }
        }
        static void Main(string[] args)
        {
            using (var server = new ResponseSocket())
            {
                server.Bind("tcp://*:5555");
                Console.WriteLine("The application have initialised");

                var message        = server.ReceiveMultipartMessage();
                var messagePartOne = message[0].ConvertToString();

                if ("FILE".Equals(messagePartOne))
                {
                    var fileName = message[1].ConvertToString();
                    var data     = message[2].ToByteArray();


                    Console.WriteLine("Frame 0:" + message[0].ConvertToString());
                    Console.WriteLine("Frame 1:" + fileName);
                    Console.WriteLine("Frame 2:" + data.Length);


                    var stream = File.OpenWrite(path + fileName);
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();


                    Console.WriteLine("finished writing the file");
                    Console.ReadKey();
                }
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Tier 3 here");
            var reciveSocket = new ResponseSocket();

            reciveSocket.Bind("tcp://0.0.0.0:" + listeningPort);

            var multipartMessage = reciveSocket.ReceiveMultipartMessage();

            var frames = new List <NetMQFrame>();

            while (false == multipartMessage.IsEmpty)
            {
                frames.Add(multipartMessage.Pop());
            }

            Console.WriteLine(frames[3]);

            while (frames.Count > 2)
            {
                frames.RemoveAt(2);
            }

            frames.Add(new NetMQFrame("Yes this is from server three"));
            var responseMessage = new NetMQMessage(frames);

            reciveSocket.SendMultipartMessage(responseMessage);

            Console.ReadKey();
        }
Beispiel #5
0
        public static IdKeyData ResponseReceive(this ResponseSocket socket)
        {
            IdKeyData kd = new IdKeyData();

            NetMQMessage msg = socket.ReceiveMultipartMessage();

            kd.Id   = msg[0].ConvertToString();
            kd.Key  = msg[1].ConvertToString();
            kd.Data = msg[2].Buffer;

            return(kd);
        }
Beispiel #6
0
        ///<summary>
        /// actual response handling including the response handler wrapper for sync or async callback
        ///</summary>
        private async Task ResponseHandlerCallback <T, TResult>(ResponseSocket socket, ResponseHandler <T, TResult> handler, CancellationToken token)
            where T : class, new()
            where TResult : class
        {
            if (!respondingIsActive || token.IsCancellationRequested)
            {
                Dispose();
                _configuration.Logger.Log(new InfoLogMsg("Task was cancelled by cancellationRequest"));
                return;
            }

            try
            {
                Message <TResult> response = null;
                try
                {
                    // block on this thread for incoming requests of the type T (Request)
                    NetMQMessage incomingRequest = socket.ReceiveMultipartMessage();
                    _configuration.Logger.Log(new DebugLogMsg($"handling response for [Request:{typeof(T)}] and [Response:{typeof(TResult)}]"));

                    var     actualRequestResult = incomingRequest.ParseRqRepMessage <T>(_configuration);
                    TResult result = await handler.HandleAsync(actualRequestResult);

                    response = new RequestReplyMessage <TResult>(_configuration, result, actualRequestResult.IsSuccess);
                    _configuration.Logger.Log(new DebugLogMsg($"sending response for [Request:{typeof(T)}] and [Response:{typeof(TResult)}]"));
                }
                catch (System.Exception ex)
                {
                    // failure to parse or any other exception leads to a non successful response, which then in turn can be handled on the request side
                    _configuration.Logger.Log(new ErrorLogMsg($"Responding to [Request:{typeof(T)}] with [Response:{typeof(TResult)}] did fail: " + ex.Message));
                    var msgParts = new [] { ex.GetType().Name, ex.Message, ex.StackTrace };
                    var msg      = Environment.NewLine + string.Join(Environment.NewLine, msgParts);
                    response = RequestReplyMessage <TResult> .FromError(_configuration, msg);
                }

                // try send response with timeout
                bool noTimeout = socket.TrySendMultipartMessage(_configuration.TimeOut, response);
                if (!noTimeout)
                {
                    _configuration.Logger.Log(new ErrorLogMsg($"Responding to [Request:{typeof(T)}] with [Response:{typeof(TResult)}] timed-out after {_configuration.TimeOut}"));
                }
            }
            catch (NetMQ.TerminatingException terminating)
            {
                _configuration.Logger.Log(new ErrorLogMsg($"repsonseHandler failed with terminating exception: [{terminating.Message}]"));
                Dispose();
            }
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Emailer!");

            using (var server = new ResponseSocket())
            {
                server.Bind("tcp://*:5558");
                while (true)
                {
                    var message = server.ReceiveMultipartMessage(5);
                    Console.WriteLine("responseSocket : Server Received '{0}'-{1}", message[0].ConvertToString(Encoding.UTF8), message[1].ConvertToString(Encoding.UTF8));
                    EmailOffer(message);
                    server.SendFrameEmpty();
                }
            }
        }
        private static void Main()
        {
            using (var worker = new ResponseSocket())
            {
                worker.Connect(WorkerEndpoint);

                while (true)
                {
                    var msg = worker.ReceiveMultipartMessage();
                    Console.WriteLine("Processing Message {0}", msg.Last.ConvertToString());

                    Thread.Sleep(500);

                    worker.SendFrame(msg.Last.ConvertToString());
                }
            }
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Type content router");
            using (var server = new ResponseSocket())
                using (var publish = new PublisherSocket())
                {
                    server.Bind("tcp://*:5555");
                    publish.Bind("tcp://*:5556");
                    while (true)
                    {
                        var message = server.ReceiveMultipartMessage(2);

                        Console.WriteLine("responseSocket : Server Received '{0}'-{1}", message[0].ConvertToString(Encoding.UTF8), message[1].ConvertToString(Encoding.UTF8));
                        server.SendFrameEmpty();

                        publish.SendMultipartMessage(message);
                    }
                }
        }
Beispiel #10
0
        private void CreateWorker(object worker)
        {
            var workerParam = worker as WorkerParam;

            _workers.Add(workerParam.Name);

            using (var workerClient = new ResponseSocket($"inproc://{workerParam.Name}"))
            {
                var message = workerClient.ReceiveMultipartMessage();
                var handler = _resolver.Resolve(workerParam.Type) as IHandler;

                var response = handler.Initialize(message[2].ConvertToString());

                var messageToRouter = new NetMQMessage();
                messageToRouter.Append(message[0]);
                messageToRouter.AppendEmptyFrame();
                messageToRouter.Append(JsonSerializer.Serialize(response));

                workerClient.SendMultipartMessage(messageToRouter);
            }
        }
Beispiel #11
0
        public NetMQMessage ReceivePatientData()
        {
            NetMQMessage message = socket.ReceiveMultipartMessage();

            return(message);
        }
Beispiel #12
0
        /// <summary>
        /// ZMQ通信、接收到命令及参数、根据命令进行相应处理
        /// </summary>
        public void Run()
        {
            int result = 1;

            while (true)
            {
                // 等待接收Message
                var message = serverSocket.ReceiveMultipartMessage();
                // 获取命令
                var Command = message[0].ConvertToString();

                string typeName   = "";
                string configName = "";
                string dataName   = "";
                byte[] pbData     = null;
                // switch case 进行判断
                switch (Command)
                {
                case "bind_config":
                    typeName = message[1].ConvertToString();
                    byte[] configData = message[2].Buffer;
                    configName = message[3].ConvertToString();
                    result     = bind_config(typeName, configData, configName);
                    break;

                case "bind_data":
                    typeName = message[1].ConvertToString();
                    pbData   = message[2].Buffer;
                    dataName = message[3].ConvertToString();
                    result   = bind_data(typeName, pbData, dataName);
                    break;

                case "start_service":
                    configName = message[1].ConvertToString();
                    result     = start_service(configName);
                    break;

                case "stop_service":
                    configName = message[1].ConvertToString();
                    result     = stop_service(configName);
                    break;

                case "set_data":
                    typeName = message[1].ConvertToString();
                    pbData   = message[2].Buffer;
                    dataName = message[3].ConvertToString();
                    result   = set_data(dataName, typeName, pbData);
                    break;

                case "get_data":
                    dataName = message[1].ConvertToString();
                    pb_data_sensor_list data_sensor_list = null;
                    result = get_data(dataName, ref data_sensor_list);
                    if (result == 1)
                    {
                        var msg = new NetMQMessage();
                        msg.Append("pb_data_sensor_list");
                        MemoryStream sm = new MemoryStream();
                        data_sensor_list.WriteTo(sm);
                        msg.Append(sm.ToArray());
                        serverSocket.SendMultipartMessage(msg);
                        log.Debug(string.Format("server response:{0} pb_data_sensor in data_sensor_list", data_sensor_list.PbDataSensors.Count));
                    }
                    else
                    {
                        serverSocket.SendFrame(result.ToString());
                        log.Debug(string.Format("server response:result = 0x{0:X2}", result));
                    }
                    break;

                case "dispose_data":
                    // 清空绑定的数据 缺少判断状态
                    configName = message[1].ConvertToString();
                    dataName   = message[2].ConvertToString();
                    result     = dispose_data(configName, dataName);
                    break;

                case "auto_get_data":
                    break;
                }
                Thread.Sleep(100);
                if (Command != "get_data")
                {
                    serverSocket.SendFrame(result.ToString());
                    log.Debug(string.Format("server response:result = 0x{0:X2}", result));
                }
            }
        }