static void StartWorker(string endpoint) { SynapseEndpoint WorkReceiver = new SynapseEndpoint(endpoint); WorkReceiver.Connect(); WorkReceiver.ReceiveMessages(ToUpperReplyToSender, true, WorkReceiver); }
static void StartHandler(String sendOn, String listenOn) { SynapseEndpoint Sender = new SynapseEndpoint(sendOn); Sender.Connect(); SynapseEndpoint Receiver = new SynapseEndpoint(listenOn); Receiver.Connect(); new Thread(() => Receiver.ReceiveMessages(ProcessHandlerRequest, false, Sender)).Start(); while (true) { String input = Console.ReadLine(); String[] strs = input.Split(','); if (strs[0] == "STATUS") { Sender.SendMessage(null, MessageType.STATUS, strs[1]); } else if (strs[0] == "QUERY") { Sender.SendMessage(null, MessageType.REQUEST, strs[1]); } } }
public MQProxy(String listenOn = "tcp://*:5555", String sendOn = "tcp://*:5556", ZContext context = null) { ZContext ctx = context; if (ctx == null) { ctx = new ZContext(); } Listener = new SynapseEndpoint(listenOn, null, ctx, ZSocketType.ROUTER); Sender = new SynapseEndpoint(sendOn, null, ctx, ZSocketType.DEALER); }
static void StartClient(string endpoint) { SynapseEndpoint WorkRequest = new SynapseEndpoint(endpoint); WorkRequest.Connect(); new Thread(() => WorkRequest.ReceiveReplies(ProcessMyReplies)).Start(); while (true) { String message = Console.ReadLine(); WorkRequest.SendMessage(null, MessageType.REQUEST, message); } }
public void ReceiveMessages(Func <String, MessageType, String, SynapseEndpoint, String, String> callback, Boolean sendAck = true, SynapseEndpoint replyOn = null) { ZError error; ZMessage request; SynapseEndpoint replyUsing = this; if (replyOn != null) { replyUsing = replyOn; } while (true) { if (null == (request = Socket.ReceiveMessage(out error))) { if (error == ZError.ETERM) { return; } throw new ZException(error); } using (request) { string identity = request[1].ReadString(); String messageId = request[2].ReadString(); MessageType messageType = (MessageType)Enum.Parse(typeof(MessageType), request[3].ReadString()); string message = request[4].ReadString(); //TODO : Debug - Remove Me Console.WriteLine(">>> [" + messageId + "][" + messageType + "] " + message); if (sendAck) { replyUsing.SendMessage(messageId, MessageType.ACK, String.Empty, identity); } if (callback != null) { String reply = callback(messageId, messageType, message, replyOn, identity); if (!(String.IsNullOrWhiteSpace(reply))) { replyUsing.SendMessage(messageId, MessageType.REPLY, reply, identity); } } } } }
static void StartApi(string sendOn, string listenOn) { SynapseEndpoint Sender = new SynapseEndpoint(sendOn); Sender.Connect(); SynapseEndpoint Receiver = new SynapseEndpoint(listenOn); Receiver.Connect(); new Thread(() => Receiver.ReceiveMessages(ProcessApiRequest, false, Sender)).Start(); while (true) { String message = Console.ReadLine(); Sender.SendMessage(null, MessageType.REQUEST, message); } }
static String ProcessMyReplies(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { Console.WriteLine(">>> [" + messageId + "][" + messageType + "] " + message); if (messageType == MessageType.REPLY) { Console.WriteLine("*** Reply Processed : Length = " + message.Length); } return(null); }
static String ToUpperReplyToSender(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { Thread.Sleep(3000); if (socket != null) { for (int i = 0; i < 5; i++) { socket.SendMessage(messageId, MessageType.STATUS, "Status Report #" + (i + 1), sendStatusTo); Thread.Sleep(3000); } } return(message.ToUpper()); }
static String ProcessPlanStatusRequest(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { if (messageType == MessageType.REQUEST) { socket.SendMessage(messageId, MessageType.ACK, ""); Console.WriteLine(">>> [" + messageId + "] Plan Status Request Received : " + message); Thread.Sleep(3000); Random rnd = new Random(); int i = rnd.Next(1, 3); String reply = (i == 1?"RUNNING":"CANCELLED"); socket.SendMessage(messageId, MessageType.REPLY, reply); } return(null); }
static String ProcessStatus(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { if (messageType == MessageType.STATUS) { socket.SendMessage(messageId, MessageType.ACK, ""); Console.WriteLine(">>> [" + messageId + "] Status Received : " + message); } return(null); }
static String ProcessApiRequest(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { if (messageType == MessageType.REQUEST) { return(ProcessPlanStatusRequest(messageId, messageType, message, socket, sendStatusTo)); } else if (messageType == MessageType.STATUS) { return(ProcessStatus(messageId, messageType, message, socket, sendStatusTo)); } else { return(null); } }
// Worker Functions static String ProcessHandlerRequest(String messageId, MessageType messageType, String message, SynapseEndpoint socket, String sendStatusTo) { if (socket != null && messageType == MessageType.REQUEST) { socket.SendMessage(messageId, MessageType.ACK, ""); Console.WriteLine(">>> [" + messageId + "] Request Received : " + message); Thread.Sleep(3000); socket.SendMessage(messageId, MessageType.REPLY, message.ToUpper()); } return(null); }