/* * Writes a message to a file */ private void writeMessageToFile(String content) { if (!fileQueue.Contains(fileNum.ToString())) { // create a writer and open the file TextWriter tw = new StreamWriter(fileStorageAddress + "\\" + fileNum); // write a line of text to the file tw.Write(content); // close the stream tw.Close(); fileQueue.Enqueue(fileNum.ToString()); //reset counter if (fileNum >= maxFileStorageNum) { fileNum = 1; } else { fileNum++; } } }
/* * Sends messages to consumers * */ public void ZeromqSendThreadRun() { logger.Debug("messaging type: " + MESSAGING_TYPE); double messageNum = 0; bool wait = false; switch (MESSAGING_TYPE) { case PIPELINE: while (!messagingFinished) { try { //When a wait command is received, waits until the continue message byte[] command = lbReceiver.Recv(SendRecvOpt.NOBLOCK); if (command != null) { string commandString = System.Text.Encoding.UTF8.GetString(command); if (commandString.StartsWith(WAIT_COMMAND)) { int clientNum = Convert.ToInt32(commandString.Split(' ')[1]); logger.Debug(ID + "wait message is received from : " + clientNum); lbSet.Add(clientNum); } else if (commandString.StartsWith(CONTINUE_COMMAND)) { int clientNum = Convert.ToInt32(commandString.Split(' ')[1]); lbSet.Remove(clientNum); logger.Debug(ID + "continue message is received from : " + clientNum); } if (lbSet.Count * ratio >= ID) { wait = true; } else { wait = false; } } //Gets message from the queue added by the WP3 if (outgoingMessageQueue.Count > 0 && wait == false) { String value = (String)outgoingMessageQueue.Dequeue(); //Sends the message sender.Send(value, Encoding.UTF8, SendRecvOpt.NONE); logger.Debug("message is sent over network: " + messageNum++); //if enabled, send a copy to DB_logging component if (DB_LOGGING == true) { loggerQueue.Enqueue(value); } } else { logger.Debug(ID + "wait mode, size: " + lbSet.Count); Thread.Sleep(10); } //Terminates the thread if finish message is retrived from WP3 //if (messagingFinished) //{ // sender.Send(FINISH_COMMAND, Encoding.UTF8, SendRecvOpt.NONE); // logger.Debug("Finish command is received"); // return; //} } catch (ThreadStateException e) { logger.Error(e); } catch (System.Exception e) { logger.Error(e); } } while (outgoingMessageQueue.Count > 0) { writeOutgoingMessageToFile(outgoingMessageQueue.Dequeue()); } break; case REQ_REP: while (!messagingFinished) { try { //Waits request message from WP4 String message = sender.Recv(Encoding.UTF8); while (!message.Equals(MESSAGE_REQUEST)) { message = sender.Recv(Encoding.UTF8); } logger.Debug("Received request: {0}" + message); // Sends WP3 message to WP4 if (outgoingMessageQueue.Count > 0) { String value = (String)outgoingMessageQueue.Dequeue(); sender.Send(value, Encoding.UTF8); logger.Debug("message is sent over network: " + messageNum++); //if enabled, send a copy to DB_logging component if (DB_LOGGING == true) { loggerQueue.Enqueue(value); } } } catch (ThreadStateException e) { logger.Error(e); } catch (System.Exception e) { logger.Error(e); if (outgoingMessageQueue.Count > 0) { String value = (String)outgoingMessageQueue.Dequeue(); sender.Send(value, Encoding.UTF8); logger.Debug("message is sent over network: " + messageNum++); //if enabled, send a copy to DB_logging component if (DB_LOGGING == true) { loggerQueue.Enqueue(value); } } } } while (outgoingMessageQueue.Count > 0) { writeOutgoingMessageToFile(outgoingMessageQueue.Dequeue()); } break; } }
/* * Messaging thread function handles communication between WP4 * */ public void ZeromqThreadRun() { logger.Info("messaging type: " + MESSAGING_TYPE); double messageNum = 0; switch (MESSAGING_TYPE) { case PIPELINE: while (true) { try { //When a wait command is received, waits until the continue message byte[] command = zeromqWP4Subscriber.Recv(SendRecvOpt.NOBLOCK); if (command != null) { string commandString = System.Text.Encoding.UTF8.GetString(command); if (commandString.Equals(WAIT_COMMAND)) { logger.Info("wait message is received: "); Thread.Sleep(1); String continueMessage = ""; while (!continueMessage.Equals(CONTINUE_COMMAND)) { continueMessage = zeromqWP4Subscriber.Recv(Encoding.UTF8); logger.Info("continue message is received: " + continueMessage); } } } //Gets message from the queue added by the WP3 if (zeromqQueue.Count > 0) { String value = (String)zeromqQueue.Dequeue(); //Sends the message zeromqSender.Send(value, Encoding.UTF8, SendRecvOpt.NONE); logger.Debug("message is sent over network: " + messageNum++); //if enabled, send a copy to DB_logging component if (DB_LOGGING == true) { loggerQueue.Enqueue(value); } } //Terminates the thread if finish message is retrived from WP3 if (messagingFinished && zeromqQueue.Count == 0 && fileQueue.Count == 0 && brokerQueue.Count == 0 && loggerQueue.Count == 0) { zeromqSender.Send(FINISH_COMMAND, Encoding.UTF8, SendRecvOpt.NONE); logger.Info("Finish command is received"); return; } } catch (ThreadStateException e) { logger.Error(e); } catch (System.Exception e) { logger.Error(e); } } case REQ_REP: while (true) { try { //Waits request message from WP4 String message = zeromqSender.Recv(Encoding.UTF8); while (!message.Equals(MESSAGE_REQUEST)) { message = zeromqSender.Recv(Encoding.UTF8); } logger.Debug("Received request: {0}" + message); // Sends WP3 message to WP4 if (zeromqQueue.Count > 0) { String value = (String)zeromqQueue.Dequeue(); zeromqSender.Send(value, Encoding.UTF8); logger.Debug("message is sent over network: " + messageNum++); //if enabled, send a copy to DB_logging component if (DB_LOGGING == true) { loggerQueue.Enqueue(value); } } //Terminates the thread if finish message is retrived from WP3 if (messagingFinished && zeromqQueue.Count == 0 && fileQueue.Count == 0 && brokerQueue.Count == 0 && loggerQueue.Count == 0) { zeromqSender.Send(FINISH_COMMAND, Encoding.UTF8, SendRecvOpt.NONE); logger.Info("Finish command is received"); return; } } catch (ThreadStateException e) { logger.Error(e); } catch (System.Exception e) { logger.Error(e); } } } }