//----< defines SendThread and its operations >---------------------- /* * - asynchronous function defines Sender sendThread processing * - creates BlockingQueue<Message> to use inside Sender.sendMessage() * - creates and starts a thread executing that processing * - uses msg.toUrl to find or create a proxy for url destination */ public virtual SWTools.BlockingQueue <Message> defineSendProcessing() { SWTools.BlockingQueue <Message> sendQ = new SWTools.BlockingQueue <Message>(); Action sendAction = () => { ThreadStart sendThreadProc = () => { while (true) { try { Message smsg = sendQ.deQ(); if (smsg.content == "closeSender") { break; } if (proxyStore.ContainsKey(smsg.toUrl)) { if (Util.verbose) { Console.Write("\n sender sending message to service {0}", smsg.toUrl); } proxyStore[smsg.toUrl].sendMessage(smsg); } else { if (this.Connect(smsg.toUrl)) // if Connect succeeds it will set proxy and send start msg { if (Util.verbose) { Console.Write("\n sender created proxy and sending message {0}", smsg.toUrl); } proxyStore[smsg.toUrl] = this.proxy; // save proxy proxy.sendMessage(smsg); } else { sendMsgNotify(String.Format("could not connect to {0}\n", smsg.toUrl)); continue; } } } catch (Exception ex) { sendExceptionNotify(ex); continue; } } }; Thread t = new Thread(sendThreadProc); // start the sendThread t.IsBackground = true; t.Start(); }; this.setAction(sendAction); return(sendQ); }
//----< defines SendThread and its operations >---------------------- /* * - asynchronous function defines Sender sendThread processing * - creates BlockingQueue<Message> to use inside Sender.sendMessage() * - creates and starts a thread executing that processing * - uses msg.toUrl to find or create a proxy for url destination */ public ThreadStart performSendThreadProc(SWTools.BlockingQueue <Message> sendQ) { ThreadStart sendThreadProc = () => { while (true) { try { Message smsg = sendQ.deQ(); if (smsg.content == "closeSender") { Console.Write("\n send thread quitting\n\n"); break; } if (proxyStore.ContainsKey(smsg.toUrl)) { // proxy already created so use it if (Util.verbose) { Console.Write("\n sender sending message to service {0}", smsg.toUrl); } proxyStore[smsg.toUrl].sendMessage(smsg); } else { // create new proxy with Connect, save it, and use it if (this.Connect(smsg.toUrl)) // if Connect succeeds it will set proxy and send start msg { if (Util.verbose) { Console.Write("\n sender created proxy and sending message {0}", smsg.toUrl); } proxyStore[smsg.toUrl] = this.proxy; // save proxy proxy.sendMessage(smsg); } else { sendMsgNotify(String.Format("could not connect to {0}\n", smsg.toUrl)); continue; } } } catch (Exception ex) { sendExceptionNotify(ex); continue; } } }; return(sendThreadProc); }
//----< Connect repeatedly tries to send messages to service >------- public bool Connect(string remoteUrl) { if (Util.verbose) { sendMsgNotify("attempting to connect"); } if (isConnected(remoteUrl)) { return(true); } proxy = CreateProxy(remoteUrl); int attemptNumber = 0; Message startMsg = new Message(); startMsg.fromUrl = localUrl; startMsg.toUrl = remoteUrl; startMsg.content = "connection start message"; while (attemptNumber < MaxConnectAttempts) { try { proxy.sendMessage(startMsg); // will throw if server isn't listening yet proxyStore[remoteUrl] = proxy; // remember this proxy if (Util.verbose) { sendMsgNotify("connected"); } return(true); } catch { ++attemptNumber; sendAttemptNotify(attemptNumber); Thread.Sleep(100); } } return(false); }
//----< Connect repeatedly tries to send messages to service >------- public bool Connect(string remoteUrl) { if(Util.verbose) sendMsgNotify("attempting to connect"); if (isConnected(remoteUrl)) return true; proxy = CreateProxy(remoteUrl); int attemptNumber = 0; Message startMsg = new Message(); startMsg.fromUrl = localUrl; startMsg.toUrl = remoteUrl; startMsg.content = "connection start message"; while (attemptNumber < MaxConnectAttempts) { try { proxy.sendMessage(startMsg); // will throw if server isn't listening yet proxyStore[remoteUrl] = proxy; // remember this proxy if(Util.verbose) sendMsgNotify("connected"); return true; } catch { ++attemptNumber; sendAttemptNotify(attemptNumber); Thread.Sleep(100); } } return false; }