/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ private void threadProc() { while (true) { CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { break; } if (msg.to == lastUrl) { channel.postMessage(msg); } else { close(); if (!connect(msg.to)) { continue; } lastUrl = msg.to; channel.postMessage(msg); } } }
/*Process messages received grom gui*/ private static void processGUI() { if (rcvMsg.command == "DeleteSenders") { while (readyQ.size() != 0) { readyQ.deQ(); } CloseServers(); } /*Spawn pool processes depending on the input received*/ else if (rcvMsg.command == "spawnchilds") { childCount = Int32.Parse(rcvMsg.arguments[0]); Console.WriteLine("\nRequirement: 5"); Console.WriteLine("\nCreating " + childCount + " child builder processes"); bs = new Sender[childCount]; for (int p = 1; p <= childCount; p++) { createProcess(8081 + p, "ChildBuilder\\bin\\debug\\ChildBuilder.exe"); // Spawn all the child builders bs[p - 1] = new Sender("http://localhost", 8081 + p); //bs[p - 1] = new Sender("http://localhost", 8081 + p); } } else { requestQ.enQ(rcvMsg.arguments[0]); Console.WriteLine("\nEnqueing message to TestRequest Queue"); } }
/*----< closes Sender's proxy >--------------------------------*/ public void close() { while (sndQ.size() > 0) { CommMessage msg = sndQ.deQ(); try { channel.postMessage(msg); } catch (Exception ex) { Console.Write(ex.Message); } } try { if (factory != null) { factory.Close(); } } catch (Exception ex) { Console.Write("\n already closed"); } }
/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { TestUtilities.putLine(string.Format("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId)); CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { TestUtilities.putLine("Sender send thread quitting"); return; } if (msg.to == lastUrl) { channel.postMessage(msg); } else { close(); if (!connect(msg.to)) { return; } lastUrl = msg.to; channel.postMessage(msg); } } }
public void processMessages() { AppDomain main = AppDomain.CurrentDomain; Console.Write("\n Starting in AppDomain " + main.FriendlyName + "\n"); ThreadStart doTests = () => { Message testRequest = inQ_.deQ(); if (testRequest.body == "quit") { inQ_.enQ(testRequest); return; } hrt2.Start(); ITestResults testResults = runTests(testRequest); hrt2.Stop(); lock (sync_) { comm.sndr.PostMessage(makeTestResultsMessage(testResults, testRequest)); } Console.Write("\n REQ 12 - Time taken for test Execution {0} is {1}", testRequest.clientName, hrt2.ElapsedMicroseconds); }; Console.Write("\n Creating AppDomain thread"); int numThreads = 8; for (int i = 0; i < numThreads; ++i) { Thread t = new Thread(doTests); threads_.Add(t); t.Start(); } }
/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { TestUtilities.putLine(string.Format("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId)); CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { TestUtilities.putLine("Sender send thread quitting"); break; } if (msg.to == lastUrl) { channel.postMessage(msg); } else { if (channelWorking) { close(); } if (!connect(msg.to)) { //sndQ.enQ(msg); // message that failed to send will be immediately put back to the queue // sndQ.end(msg) <-- removed, message will be discarded and there will be no attempt to resend continue; } lastUrl = msg.to; channel.postMessage(msg); } } }
/*----< processing for send thread >--------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { break; } /*if (msg.to == lastUrl) * { * channel.postMessage(msg); * } */ else { close(); if (!connect(msg.to)) { return; } lastUrl = msg.to; channel.postMessage(msg); } } }
/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { Console.WriteLine("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId); CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { Console.WriteLine("Sender send thread quitting"); break; } if (msg.to == lastUrl) { channel.postMessage(msg); } else { close(); if (!connect(msg.to)) { return; } lastUrl = msg.to; channel.postMessage(msg); } } }
//----< child thread processing for writing to streams >--------- public void threadProc() { while (true) { string msg = Q_.deQ(); if (showTimeStamp_) { msg += " - " + DateTime.Now.ToString(); } if (msg == "quit") { threadRunning_ = false; break; } foreach (StreamWriter stream in streams_) { stream.Write(msg); } if (paused_) { lock (sync_) { Monitor.Wait(sync_); foreach (StreamWriter stream in streams_) { stream.Write(msg); } } } } }
/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { TestUtilities.putLine(string.Format("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId)); CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { TestUtilities.putLine("Sender send thread quitting"); break; } close(); if (!connect(msg.to)) { continue; } try { channel.postMessage(msg); } catch { Console.Write("\n Error: post message failed"); } close(); } }
static void Main(string[] args) { Console.Write("\n Testing Monitor-Based Blocking Queue"); Console.Write("\n ======================================"); SWTools.BlockingQueue <string> q = new SWTools.BlockingQueue <string>(); Thread t = new Thread(() => { string msg; while (true) { msg = q.deQ(); Console.Write("\n child thread received {0}", msg); if (msg == "quit") { break; } } }); t.Start(); string sendMsg = "msg #"; for (int i = 0; i < 20; ++i) { string temp = sendMsg + i.ToString(); Console.Write("\n main thread sending {0}", temp); q.enQ(temp); } q.enQ("quit"); t.Join(); Console.Write("\n\n"); }
/*----< processing for send thread >---------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { TestUtilities.putLine(string.Format("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId)); CommMessage msg = sndQ.deQ(); try { if (msg.type == CommMessage.MessageType.closeSender) { TestUtilities.putLine("Sender send thread quitting"); close(); break; } /* MV: Commented out so we reconnect every time in case server or client * disconnect. If not, channel goes into an unusable "faulted" state. * Better solution is to check for faulted state exception and reconnect. * * Commented out, side-effect is that prior objects get deleted which causes * postFile to fail. */ if (msg.to == lastUrl) { channel.postMessage(msg); lastError = "connected to " + msg.to; TestUtilities.putLine(string.Format("\n sending msg to {0}", msg.to)); if (ClientEnvironment.verbose) { msg.show(msg.arguments.Count < 7); } } else { close(); if (!connect(msg.to)) { CommMessage errMsg = new CommMessage(CommMessage.MessageType.commError); errMsg.to = msg.from; errMsg.from = msg.from; errMsg.errorMsg = "can't connect to " + msg.to; comm.postMessage(errMsg); TestUtilities.putLine(string.Format("\n can't connect to {0}", msg.to)); lastError = "can't connect to " + msg.to; lastUrl = ""; continue; } lastError = "connected to " + msg.to; TestUtilities.putLine(string.Format("\n {0}", lastError)); lastUrl = msg.to; channel.postMessage(msg); } } catch (Exception ex) { Console.Write("\n--{0}", ex.Message); } } }
void build_req_thread() { string build_req = ""; string avail_proc = ""; while (true) { if (br_q.size() != 0) { if (rd_q.size() != 0) { build_req = build_req + br_q.deQ(); avail_proc = avail_proc + rd_q.deQ(); CommMessage send_to_child = new CommMessage(CommMessage.MessageType.request); send_to_child.command = "To Child With Port:" + avail_proc; send_to_child.author = "Salim Zhulkhrni"; send_to_child.to = "http://localhost:" + avail_proc + "/IPluggableComm"; send_to_child.from = "http://localhost:8091/IMessagePassingComm"; send_to_child.msg_body = "to_child" + build_req; send_to_child.port_number = 8091; comm.postMessage(send_to_child); build_req = ""; avail_proc = ""; } } } }
/*----< processing for receive thread >------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { break; } if (msg.to == lastUrl) { try { channel.postMessage(msg); } catch (Exception ex) { Console.WriteLine(ex); } } else { close(); if (!connect(msg.to)) { return; } lastUrl = msg.to; channel.postMessage(msg); } } }
/*----< dequeue a test request, wait for a ready child to process >----*/ void threadProc2() { while (true) { TestRequest request = requestQ_.deQ(); try { int i = readyQ_.deQ(); Console.Write("\n Polled from request queue and send to child {0}", i); Msg childMsg = new Msg(Msg.MessageType.request); childMsg.to = childs_[i].address; childMsg.from = "http://localhost:8080/IPluggableComm"; childMsg.argument = request.ToXml(); childMsg.command = Msg.Command.testRequest; try { createCommIfNeeded(); comm_.postMessage(childMsg); } catch { Console.Write("\n Error: can't connect"); } childs_[i].setBusy(); } catch { Console.Write("\n Error: child builder doesn't exist"); } } }
void processMessages(SWTools.BlockingQueue <TestData> p, SWTools.BlockingQueue <System.IO.FileStream> q) //getMessage and decode message from Queue, return metadata of the test //p includes test, q include xmlstream. //we need to transfer p to TestLoader { System.IO.FileStream current = q.deQ(); XmlDecoder decoder = new XmlDecoder(); decoder.parse(current, p); }
//----< 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); }
/*----< processing for send thread >---------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { TestUtilities.putLine(string.Format("sender enqueuing message on thread {0}", Thread.CurrentThread.ManagedThreadId)); CommMessage msg = sndQ.deQ(); try { if (msg.type == CommMessage.MessageType.closeSender) { TestUtilities.putLine("Sender send thread quitting"); close(); break; } if (msg.to == lastUrl) { channel.postMessage(msg); lastError = "connected to " + msg.to; TestUtilities.putLine(string.Format("\n sending msg to {0}", msg.to)); if (ClientEnvironment.verbose) { msg.show(msg.arguments.Count < 7); } } else { close(); if (!connect(msg.to)) { CommMessage errMsg = new CommMessage(CommMessage.MessageType.commError); errMsg.to = msg.from; errMsg.from = msg.from; errMsg.errorMsg = "can't connect to " + msg.to; comm.postMessage(errMsg); TestUtilities.putLine(string.Format("\n can't connect to {0}", msg.to)); lastError = "can't connect to " + msg.to; lastUrl = ""; continue; } lastError = "connected to " + msg.to; TestUtilities.putLine(string.Format("\n {0}", lastError)); lastUrl = msg.to; channel.postMessage(msg); } } catch (Exception ex) { Console.Write("\n--{0}", ex.Message); } } }
// Takes care of sending build requests to the child builders based on availablity of the child builders (from "ready" status) private void buildRequestDispatcher() { while (true) { CommMessage msg = buildRequestQ.deQ(); string childBuilderAddr = readyStatusQ.deQ(); // Sends message when both the messages are dequeued msg.to = childBuilderAddr; Console.WriteLine("\n Sending the following Build Request to: {0} child builder", msg.to); msg.show(); commObj.postMessage(msg); Thread.Sleep(1000); } }
//----< 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); }
//function to manage ready queue public static void qManager() { while (true) { int pId; if (readyQ.size() != 0) { pId = readyQ.deQ(); lock (ConsoleWriterLock) { Console.Write("\npId retrieved from readyQ is {0}", pId); } processPid(pId); } } }
public bool startlocal() //each Test request will have its own child appdomain { //the local start part bool finished = false; //here finished may be need to lock while (q.size() != 0) { Thread th = new Thread(new ThreadStart(createChildAppDomain)); finished = false; th.Start(); Console.WriteLine("--------------this is for requirement 4-----------------\nthread start,id={0}", th.ManagedThreadId.ToString()); th.Join(); finished = true; Console.WriteLine("--------------this is for requirement 4-----------------\nthread terminate"); q.deQ(); //return finished; } return(finished); }
private void startThread() { if (execute == 1) { execute++; Thread newThread = new Thread(x => { while (true) { PostError newLog = queue.deQ(); HitOnRest(newLog); Thread.Sleep(10); Console.WriteLine("Sent to rest : " + newLog.ErrorMessage); } }); newThread.Start(); } }
void ThreadProc() { string msg = ""; while (true) { try { msg = sndQueue.deQ(); channel.PostMessage(msg); if (msg == "quit") { break; } } catch (Exception e) { //Console.WriteLine("{0}\n{1}",msg,e.ToString()); too detail Console.WriteLine("\nsending string fall " + e.Message); } } }
/*----< processing for send thread >--------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { Message msg = sndQ.deQ(); if (msg.type == Message.MessageType.closeSender) { break; } else { close(); if (!connect(msg.toAddress)) { return; } lastUrl = msg.toAddress; channel.postMessage(msg); } } }
/*----< processing for send thread >--------------------------*/ /* * - send thread dequeues send message and posts to channel proxy * - thread inspects message and routes to appropriate specified endpoint */ void threadProc() { while (true) { CommMessage msg = sndQ.deQ(); if (msg.type == CommMessage.MessageType.closeSender) { Console.WriteLine("Sender send thread quitting"); break; } if (msg.to == lastUrl) { try { channel.postMessage(msg); } catch (Exception ex) { Console.WriteLine("exceprion caught{0}", ex.Message); } } else { close(); if (!connect(msg.to)) { continue; } lastUrl = msg.to; try { channel.postMessage(msg); } catch (Exception e) { Console.WriteLine("new exception{0}", e.Message); } } } }
public void Test1() { Console.Write("\n Testing Monitor-Based Blocking Queue"); Console.Write("\n ======================================"); SWTools.BlockingQueue <string> q = new SWTools.BlockingQueue <string>(); int count = 0; Task t = new Task(() => { string msg; while (true) { msg = q.deQ(); Console.Write("\n child thread received {0}", msg); // Assert.True(msg == "msg #" + count.ToString()); count++; if (msg == "quit") { break; } } //Assert.True(count == 21); }); t.Start(); string sendMsg = "msg #"; for (int i = 0; i < 20; ++i) { string temp = sendMsg + i.ToString(); Console.Write("\n main thread sending {0}", temp); q.enQ(temp); } q.enQ("quit"); t.Wait(); Console.Write("\n\n"); }
//Costructor MotherBuilder(int count) { connectWithRepo(); for (int i = 1; i <= count; ++i) { int portNum = portNumber + i; connectWithChild(portNum); } if (ready == null) { ready = new SWTools.BlockingQueue <string>(); } if (testQueue == null) { testQueue = new SWTools.BlockingQueue <string>(); } Thread t = new Thread(() => { while (true) { if (ready.size() != 0 && testQueue.size() != 0) { string port = ready.deQ(); CommMessage csndMsg2 = new CommMessage(CommMessage.MessageType.testRequest); csndMsg2.command = "show"; csndMsg2.author = "Jim Fawcett"; csndMsg2.to = "http://localhost:" + port + "/IPluggableComm"; csndMsg2.from = fromAddr; csndMsg2.body = testQueue.deQ(); c.postMessage(csndMsg2); } } }); t.Start(); recieveMessages(count); }
private void dequeRequest( ) { while (open) { if (requestedQ.size() > 0 && readQ.size() > 0) { try { RequestInfo request = requestedQ.deQ( ); int childBuilderIndex = readQ.deQ( ); allProcessinPool[childBuilderIndex].isReady = false; Console.Write("\n Deque a ready Child Builder({0}) for your request \n ", childBuilderIndex); CommMessage childMsg = new CommMessage(CommMessage.MessageType.request); childMsg.to = allProcessinPool[childBuilderIndex].adress; childMsg.from = "http://localhost:8080/IMessagePassingComm"; //childMsg.argument = request.requestStr; childMsg.argument = request.msg.argument; //childMsg.command = "BuildRequest"; childMsg.command = request.msg.command; comm.postMessage(childMsg); //Thread.Sleep ( 100 ); //readQ.enQ ( childBuilderIndex ); //allProcessinPool[ childBuilderIndex ].isReady = true; } catch { } } } }
/*To dequeue a ready port and a test request and send a message to child builder*/ private static void CreateBuildRequests() { while (true) { string portName = readyQ.deQ(); // Dequeue port number string request = requestQ.deQ(); // Dequeue test request CommMessage sndMsg = new CommMessage(CommMessage.MessageType.reply); sndMsg.command = "Parse"; sndMsg.author = "Vikrant"; sndMsg.to = "http://localhost:" + portName + "/IPluggableComm"; sndMsg.from = "Mother Builder"; sndMsg.arguments.Add(request); try { bs[Int32.Parse(portName) - 8082].postMessage(sndMsg); // Send message Console.WriteLine("\nSending Message to Child Builder " + (Int32.Parse(portName) - 8081)); Console.WriteLine("\nTestRequestName: " + request); } catch { Console.WriteLine("\nChild Builder is Closed"); } } }
//----< 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") { 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; } } }; Thread t = new Thread(sendThreadProc); // start the sendThread t.IsBackground = true; t.Start(); }; this.setAction(sendAction); return sendQ; }