/// <summary> /// Send a request for acknoledgement of job error /// </summary> /// <param name="config">The extraction routine's configuration</param> /// <param name="error">Any error message to be passed</param> /// <returns>True if acknoledged, false otherwise</returns> public bool sendJobErrorRequest(ExtractorConfiguration config, ExtractorReport report) { //LOG.Debug("Sending the orchestrator a job error request"); MessageTO message = new MessageTO() { Message = "Job Error!", //Extractor = new Extractor(extractor.HostName, extractor.ListeningPort, // extractor.SiteCode, extractor.VistaFile, extractor.Timestamp), Configuration = config, ExtractorReport = report, //Error = report.ToString(), //HostName = extractor.HostName, //ListeningPort = extractor.ListeningPort, MessageType = MessageTypeTO.JobErrorRequest }; MessageTO response = submitRequest(message); if (response == null || response.MessageType != MessageTypeTO.JobErrorResponse) { ////LOG.Debug("The orchestrator did not successfully acknowledge our job error request!"); return(false); } else { //LOG.Debug("The orchestrator successfully acknowledged our job error response! Better luck next time..."); return(true); } }
/// <summary> /// Send a request to the server to ask if any more jobs are available on it's work stack /// </summary> /// <returns>Returns true if server has work items, false otherwise</returns> public bool sendServerHasWorkRequest() { MessageTO message = new MessageTO(); message.MessageType = MessageTypeTO.ServerHasWorkRequest; MessageTO response = submitRequest(message); if (response != null) { //LOG.Debug("Received " + Enum.GetName(typeof(MessageTypeTO), response.MessageType) + " message type"); if (response.MessageType == MessageTypeTO.Error) { //LOG.Debug(response.Error); } //LOG.Debug("Server reported " + response.Message + " jobs"); } int trash = 0; // response should be server has work type, message should contain the number of items on the // work stack, the message should be a number and the number should be > 0 if (response != null && response.MessageType == MessageTypeTO.ServerHasWorkResponse && !String.IsNullOrEmpty(response.Message) && Int32.TryParse(response.Message, out trash) && trash > 0) { return(true); } else { return(false); } }
void sendToClient(ThreadSafeStateObject state, MessageTO message) { BinaryFormatter serializer = new BinaryFormatter(); MemoryStream bytesToSend = new MemoryStream(); serializer.Serialize(bytesToSend, message); //bytesToSend.WriteByte(EOT); // add EOT before setting stream position bytesToSend.Position = 0; NetworkStream stream = new NetworkStream(state.Socket); byte[] header = System.Text.ASCIIEncoding.ASCII.GetBytes(String.Concat(bytesToSend.Length.ToString() + "|")); stream.Write(header, 0, header.Length); stream.Write(bytesToSend.GetBuffer(), 0, (Int32)bytesToSend.Length); stream.Flush(); try { state.Socket.Shutdown(SocketShutdown.Both); state.Socket.Close(); } catch (Exception exc) { //LOG.Error("An exception was caught trying to shut down a background server socket", exc); } }
void sendToClient(ThreadSafeStateObject state, string message) { MessageTO messageObject = new MessageTO() { Message = message }; sendToClient(state, messageObject); }
/// <summary> /// Submit a query to the connected socket /// </summary> /// <param name="request">The request string to submit</param> /// <param name="disconnect">Will disconnect socket after query has completed if set to true</param> /// <param name="echoRequest">Submits specially formatted request that Server class will simply send back untouched</param> /// <returns>The response from the query</returns> public string sendMessage(string message, bool disconnect, bool echoRequest) { MessageTO messageTO = query(new MessageTO() { Message = message }, disconnect, echoRequest); return(messageTO.Message); }
public MessageTO sendPrioritizationRequest(IList <ExtractorConfiguration> configs) { MessageTO request = new MessageTO(); request.MessageType = MessageTypeTO.JobPrioritizationRequest; request.Configurations = new Dictionary <string, IList <ExtractorConfiguration> >(); request.Configurations.Add("PRIORITIZE", configs); return(submitRequest(request)); }
public MessageTO sendLogErrorRequest(string errorMessage) { //LOG.Debug("Sending a log error request to the orchestrator"); MessageTO message = new MessageTO() { Error = errorMessage, MessageType = MessageTypeTO.LogErrorRequest }; return(submitRequest(message)); }
public MessageTO submitRequest(MessageTO message) { try { return(query(message, true, false)); } catch (Exception exc) { //LOG.Error("Unable to send message: " + message.ToString(), exc); return(null); } }
/// <summary> /// Send a request for the completion percentage of a client extractor /// </summary> /// <returns>A string representing the percentage complete of a client extractor</returns> public string sendJobStatusRequest() { MessageTO request = new MessageTO(); request.MessageType = MessageTypeTO.JobStatusRequest; MessageTO response = submitRequest(request); if (response != null) { return(response.Message); } return("0"); }
public IList <Extractor> sendGetExtractorsRequest() { MessageTO request = new MessageTO(); request.MessageType = MessageTypeTO.ExtractorsRequest; MessageTO response = submitRequest(request); if (response == null || response.MessageType != MessageTypeTO.ExtractorsResponse) { return(null); } return(response.Extractors); }
public bool sendCompressedFilesUploadRequest(Dictionary <string, byte[]> compressedFiles, ExtractorConfiguration config) { MessageTO message = new MessageTO { CompressedDataTables = compressedFiles, MessageType = MessageTypeTO.TablesUploadRequest, Configuration = config }; MessageTO response = submitRequest(message); if (response == null || response.MessageType != MessageTypeTO.TablesUploadResponse) { return(false); } return(true); }
/// <summary> /// Ask the orchestrator for a new extraction job /// </summary> /// <returns></returns> public MessageTO sendNewJobRequest(string localHostName, Int32 listeningPort) { //LOG.Debug("Sending a new job request to the orchestrator"); MessageTO message = new MessageTO() { HostName = localHostName, ListeningPort = listeningPort, Message = "New Job", MessageType = MessageTypeTO.NewJobRequest }; message.Extractor = new Extractor(message.HostName, message.ListeningPort, "", "", new DateTime()); MessageTO response = submitRequest(message); //LOG.Debug("Successfully received a new job response from the orchestrator!"); return(response); }
/// <summary> /// Send a request to either an extractor's or the orchestrator's server to stop the process /// </summary> /// <param name="reason">The reason for the request</param> /// <returns>True if the request was acknoledged, false otherwise</returns> public bool sendStopServerRequest(string reason) { if (String.IsNullOrEmpty(reason)) { return(false); } MessageTO message = new MessageTO() { MessageType = MessageTypeTO.StopServerRequest, Message = reason }; MessageTO response = submitRequest(message); if (response == null || response.MessageType != MessageTypeTO.StopServerResponse) { return(false); } return(true); }
/// <summary> /// Get a generic list of configurations from the orchestrator /// </summary> /// <returns></returns> public String sendGetWorkListsRequest() { MessageTO request = new MessageTO() { MessageType = MessageTypeTO.WorkStacksRequest, }; MessageTO response = submitRequest(request); //if (response == null || response.Configurations == null) //{ // return null; //} if (response == null || String.IsNullOrEmpty(response.Message)) { return(null); } return(response.Message); // now contains JSON representation of TaggedExtractorConfigurationArrays }
/// <summary> /// Send a request to upload a DataTable of Vista data to the orchestrator service /// </summary> /// <param name="vistaData">The Vista data in a DataTable object</param> /// <param name="config">The extractor's configuration</param> /// <returns>Returns the file name created on the orchestrator server if successful. Returns null if there was an error</returns> public string sendVistaDataUploadRequest(DataTable vistaData, ExtractorConfiguration config) { //LOG.Debug("Sending a request to upload our extraction results to the orchestrator"); MessageTO message = new MessageTO() { VistaData = vistaData, MessageType = MessageTypeTO.TableUploadRequest, Configuration = config }; MessageTO response = submitRequest(message); if (response.MessageType == MessageTypeTO.TableUploadResponse) { //LOG.Debug("Successfully uploaded our extraction results!"); return(response.Message); } else { //LOG.Debug("Uh-oh! Our upload seems to have failed..."); return(null); } }
public bool send7zipFileUploadRequest(string checksum, byte[] fileBytes) { // LOG.Debug("Sending a request to upload our zipped extraction results to the orchestrator"); MessageTO message = new MessageTO { Message = checksum, ZippedFile = fileBytes, MessageType = MessageTypeTO.ZipFileUploadRequest }; MessageTO response = submitRequest(message); if (response.MessageType == MessageTypeTO.ZipFileUploadResponse) { //LOG.Debug("Successfully uploaded our zip file!"); return(true); } else { //LOG.Debug("Uh-oh! Some unknown error occurred while uploading our zip file!"); return(false); } }
/// <summary> /// Send a completed message /// </summary> /// <param name="config"></param> public bool sendJobCompletedRequest(ExtractorConfiguration config, ExtractorReport report, String lastIen) { // LOG.Debug("Sending orchestrator a notice the job has beeen completed..."); MessageTO message = new MessageTO() { ExtractorReport = report, Message = lastIen, Configuration = config, MessageType = MessageTypeTO.JobCompletedRequest }; MessageTO response = submitRequest(message); if (response == null || response.MessageType != MessageTypeTO.JobCompletedResponse) { //LOG.Debug("The orchestrator did not successfully acknowledge our job completed notice!"); return(false); } else { //LOG.Debug("The orchestrator got our job completed notice! We are such a good worker!"); return(true); } }
//void waitForData(StateObject state) //{ // state.Socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, SocketFlags.None, new AsyncCallback(dataReceivedCallback), state); //} void dataReceivedCallback(IAsyncResult iar) { ThreadSafeStateObject state = (ThreadSafeStateObject)iar.AsyncState; Socket worker = state.Socket; try { int bytesRead = worker.EndReceive(iar); if (bytesRead == 0 && state.getMemoryStreamLength() == 0) { worker.Close(); return; } // first get length header int msgLength = 0; for (int i = 0; i < bytesRead; i++) { if (state.Buffer[i] == '\x7c') { msgLength = Convert.ToInt32(System.Text.ASCIIEncoding.ASCII.GetString(state.Buffer.Take(i).ToArray())); bytesRead = bytesRead - i - 1; // -1 for the pipe too break; } } state.Buffer = state.Buffer.Skip(msgLength.ToString().Length + 1).ToArray(); // now reset chunk to skip the header state.addBytesToMemoryStream(state.Buffer, bytesRead); state.Buffer = new byte[ThreadSafeStateObject.BufferSize]; while (state.getMemoryStreamLength() < msgLength) // while not EOT { bytesRead = worker.Receive(state.Buffer); state.addBytesToMemoryStream(state.Buffer, bytesRead); state.Buffer = new byte[ThreadSafeStateObject.BufferSize]; } //state.IsEOT = false; BinaryFormatter bf = new BinaryFormatter(); //state.BytesInMemory.Position = 0; // set position in stream to zero so deserialization succeeds byte[] bytes = state.getMemoryStream(); Stream memStream = new MemoryStream(bytes); memStream.Position = 0; MessageTO messageTO = (MessageTO)(bf.Deserialize(memStream)); sendToClient(state, RequestHandler.getInstance().handleRequest(messageTO, this)); // pass this because some methods use the properties and requesthandler is a singleton and not unique among threads // this is our hook for thread safety mentioned in RequestHandler // before returning check to see if shutdown signal was set if (messageTO.MessageType == MessageTypeTO.StopServerRequest) // this.ServiceState != null && this.ServiceState.Status == ServiceStatus.STOPPED) { if (state.ServiceState != null) { state.ServiceState.Status = ServiceStatus.STOPPED; } stopListener(); } } catch (Exception) { //LOG.Error("An unrecoverable error occurred while sending/receiving data", exc); try { worker.Shutdown(SocketShutdown.Both); worker.Close(); } catch (Exception) { } } }
/// <summary> /// Serialize the MessageTO and send it to the connected socket /// </summary> /// <param name="messageTO"></param> /// <param name="disconnect"></param> /// <param name="echoRequest"></param> /// <returns>The MessageTO received as a response to the MessageTO sent</returns> internal MessageTO query(MessageTO messageTO, bool disconnect, bool echoRequest) { try { if (_connection == null || !_connection.Connected) { connect(_serverIp.ToString(), _serverPort); } if (messageTO == null) { throw new ArgumentNullException("Must supply a message to send"); } if (echoRequest) { messageTO.Message = "<ECHO>" + messageTO.Message + "</ECHO>"; } NetworkStream writer = _connection.GetStream(); BinaryFormatter bf = new BinaryFormatter(); MemoryStream bytesToSend = new MemoryStream(); bf.Serialize(bytesToSend, messageTO); //byte EOT = System.Text.Encoding.ASCII.GetBytes("\x04")[0]; //bytesToSend.WriteByte(EOT); // add EOT String lengthHeader = String.Concat(bytesToSend.Length.ToString(), "|"); writer.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(lengthHeader), 0, lengthHeader.Length); // write length and a pipe as header first writer.Write(bytesToSend.GetBuffer(), 0, (Int32)bytesToSend.Length); // then write the rest writer.Flush(); // sent message - now receive response int responseBufferSize = 8192; // create a buffer the size of the expected read - never expect big messages from server so this should be ok byte[] chunk = new byte[responseBufferSize]; NetworkStream reader = _connection.GetStream(); #if RELEASE reader.ReadTimeout = 300000; // give client 5 minutes to send the data and receive a response #endif MemoryStream responseBytes = new MemoryStream(); int bytesRead = reader.Read(chunk, 0, responseBufferSize); // first get length header int msgLength = 0; for (int i = 0; i < bytesRead; i++) { if (chunk[i] == '\x7c') { msgLength = Convert.ToInt32(System.Text.ASCIIEncoding.ASCII.GetString(chunk.Take(i).ToArray())); bytesRead = bytesRead - i - 1; // -1 for the pipe too break; } } chunk = chunk.Skip(msgLength.ToString().Length + 1).ToArray(); // now reset chunk to skip the header responseBytes.Write(chunk, 0, bytesRead); while (responseBytes.Length < msgLength) { chunk = new byte[responseBufferSize]; bytesRead = reader.Read(chunk, 0, responseBufferSize); responseBytes.Write(chunk, 0, bytesRead); } if (disconnect) { this.disconnect(); } bf = new BinaryFormatter(); responseBytes.Position = 0; // reset memory stream position for deserialization MessageTO responseTO = (MessageTO)bf.Deserialize(responseBytes); return(responseTO); } catch (Exception exc) { return(new MessageTO { MessageType = MessageTypeTO.Error, Error = exc.ToString() }); } }