//----< defines SendThread and its operations >---------------------- /* * - asynchronous function defines Sender sendThread processing * - creates MessageQueue<Message> to use inside Sender.sendMessage() * - creates and starts a thread executing that processing * - uses message.ToURL to find or create a Proxy for url destination */ public virtual MessageQueue <Message> defineSendProcessing() { MessageQueue <Message> SendingQueue = new MessageQueue <Message>(); Action sendAction = () => { ThreadStart sendThreadProc = () => { while (true) { try { Message SendingMessage = SendingQueue.dequeue(); if (SendingMessage.MessageContent == "closeSender") { Console.Write("\n send thread quitting\n\n"); break; } if (ProxyDB.ContainsKey(SendingMessage.ToURL)) { ProxyDB[SendingMessage.ToURL].PROXY.sendMessage(SendingMessage); } else { ProxyAndDeadLetterQ ProxyAndDLQ1 = new ProxyAndDeadLetterQ(); // create new Proxy with Connect, save it, and use it if (this.Connect(SendingMessage.ToURL)) // if Connect succeeds it will set Proxy and send start msg { ProxyAndDLQ1.PROXY = Proxy; ProxyDB[RemoteURL] = ProxyAndDLQ1; ProxyDB[SendingMessage.ToURL] = ProxyAndDLQ1; // save Proxy Proxy.sendMessage(SendingMessage); } else { sendMsgNotify(String.Format("could not connect to {0}\n", SendingMessage.ToURL)); ProxyAndDLQ1.DEADLETTERQUEUE.enqueue(SendingMessage); continue; } } } catch (Exception ex) { sendExceptionNotify(ex); continue; } } }; Thread t = new Thread(sendThreadProc); // start the sendThread t.IsBackground = true; t.Start(); }; this.setAction(sendAction); return(SendingQueue); }
//----< Connect repeatedly tries to send messages to service >------- public bool Connect(string RemoteURL) { if (UtilityMethods.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.TimeSent = DateTime.Now; startMsg.MessageContent = "connection start message"; while (attemptNumber < MaxConnectAttempts) { try { Proxy.sendMessage(startMsg); // will throw if server isn't listening yet ProxyAndDeadLetterQ ProxyAndDLQ = new ProxyAndDeadLetterQ(); ProxyAndDLQ.PROXY = Proxy; ProxyDB[RemoteURL] = ProxyAndDLQ; // remember this Proxy if (UtilityMethods.verbose) { sendMsgNotify("connected"); } return(true); } catch { ++attemptNumber; sendAttemptNotify(attemptNumber); Thread.Sleep(100); } } return(false); }