示例#1
0
        private void AcceptConnectionCallBack(IAsyncResult ar)
        {
            byte[] message      = null; // buffer
            byte   numberOfJobs = 0;
            byte   flags;
            uint   payloadSize;
            bool   isLittleEndianArchitecture;

            // Accept Connection
            TcpClient client;

            // After invoking TcpListener.Stop(), this method is invoked and we must return from this method
            // if IsListening is false.
            if (IsListening)
            {
                client = _listener.EndAcceptTcpClient(ar);
            }
            else
            {
                return;
            }

            #region Start reciving data from network and store it in buffer

            const int     HEADER_JOB_ID_FIELD_SIZE = 8;                 // 8 Byte
            const int     HEADER_JOB_EXECUTABLE_ADDRESS_FIELD_SIZE = 4; // 4 Byte
            const int     HEADER_JOB_INPUT_ADDRESS_FIELD_SIZE      = 4; // 4 Byte
            const int     JOB_HEADER_SIZE         = HEADER_JOB_ID_FIELD_SIZE + HEADER_JOB_EXECUTABLE_ADDRESS_FIELD_SIZE + HEADER_JOB_INPUT_ADDRESS_FIELD_SIZE;
            NetworkStream netStream               = null;
            bool          dataRecivedSuccessfully = true;

            try {
                netStream = client.GetStream();

                // Reads field "Flags"
                flags = (byte)netStream.ReadByte();
                isLittleEndianArchitecture = (flags == 0) ? true : false;

                // Reads field "Number Of Jobs"
                numberOfJobs = (byte)netStream.ReadByte();

                // Reads field "Payload Size"
                byte[] payloadSizeBytes = new byte[4];
                netStream.Read(payloadSizeBytes, 0, 4);
                if (!isLittleEndianArchitecture)
                {
                    payloadSizeBytes.Reverse();
                }
                payloadSize = BitConverter.ToUInt32(payloadSizeBytes, 0);
                //

                // Allocate memory for the rest of message (without fileds "Flags", "Number of jobs" and "Payload Size")
                // And read rest of the message from the NetworkStream
                int messageSize = (numberOfJobs * JOB_HEADER_SIZE) + (int)payloadSize;
                message = new byte[messageSize];
                netStream.Read(message, 0, messageSize);
            }
            catch {
                dataRecivedSuccessfully = false;
            }
            finally {
                if (netStream != null)
                {
                    netStream.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }

            // Start listening for new connections
            try {
                //_listener.Start(1);
                _listener.BeginAcceptTcpClient(AcceptConnectionCallBack, _listener);
            }
            catch {
                // Generate WorkerCouldNotStartLog log
                WorkerStoppedLog log = new WorkerStoppedLog();
                log.Message = "Worker could not continue to listen for new jobs, so it has stopped.";
                EventExtensions.Raise(NewLogItem, new[] { log });
            }

            #endregion

            #region Create job objects and save theam to the "JobsDirectoryName" directory

            // numberOfJobs = 1
            List <Job> recivedJobs = new List <Job>(10);
            if (dataRecivedSuccessfully)
            {
                for (int i = 0; i < numberOfJobs; i++)
                {
                    #region Create job object
                    Job    newJob = new Job();
                    UInt32 jobExecutableStartLocation;
                    UInt32 jobInputStartLocation;

                    // ID
                    newJob.ID = BitConverter.ToUInt64(message, 0);

                    // InputFileName
                    StringBuilder filename = new StringBuilder(50);
                    DateTime      now      = DateTime.Now;
                    Random        rnd      = new Random();

                    filename.Append(JobsDirectoryName);
                    filename.Append("\\job");
                    filename.Append(newJob.ID.ToString());
                    filename.Append("_");
                    filename.Append(now.Hour.ToString("D2"));
                    filename.Append(now.Minute.ToString("D2"));
                    filename.Append(now.Second.ToString("D2"));
                    filename.Append("_");
                    filename.Append(rnd.Next(1000000).ToString("D6"));

                    newJob.InputFileName = filename.ToString() + ".in";

                    // OutputFileName
                    newJob.OutputFileName = filename.ToString() + ".out";

                    // ExecuteableFileName
                    newJob.ExecuteableFileName = filename.ToString() + ".exe";

                    #endregion

                    #region Write job's Executable and input files to disk
                    jobExecutableStartLocation  = BitConverter.ToUInt32(message, 8);  // in payload address space
                    jobInputStartLocation       = BitConverter.ToUInt32(message, 12); // in payload address space
                    jobExecutableStartLocation += 16;
                    jobInputStartLocation      += 16;
                    newJob.ExecutableFileLength = jobInputStartLocation - jobExecutableStartLocation;
                    newJob.InputFileLength      = (UInt32)message.Length - newJob.ExecutableFileLength - JOB_HEADER_SIZE;

                    System.IO.FileStream fsExe   = null;
                    System.IO.FileStream fsInput = null;
                    try {
                        // Create "jobs directory" if it is not exists
                        if (!System.IO.Directory.Exists(JobsDirectoryName))
                        {
                            System.IO.Directory.CreateDirectory(JobsDirectoryName);
                        }

                        // Create job's executable file
                        fsExe = System.IO.File.Create(newJob.ExecuteableFileName);
                        fsExe.Write(message, (int)jobExecutableStartLocation, (int)newJob.ExecutableFileLength);

                        // Create job's input file
                        fsInput = System.IO.File.Create(newJob.InputFileName);
                        fsInput.Write(message, (int)jobInputStartLocation, (int)newJob.InputFileLength);

                        // Add jobs to the jobs queue
                        lock (_jobsQueue) {
                            _jobsQueue.Enqueue(newJob);
                        }

                        // Add jobs to the recieved jobs list
                        recivedJobs.Add(newJob);
                    }
                    catch (Exception e) {
                        // Error in writing jobs to the disk
                        // Generate log and Raise "NewLogItem" event
                        WorkerAgent.ErrorInWritingJobToDiskLog log = new WorkerAgent.ErrorInWritingJobToDiskLog();
                        log.Job          = newJob;
                        log.Message      = "Could not write the job to the jobs directory, please verify jobs directory.";
                        log.ErrorMessage = e.Message;
                        EventExtensions.Raise(NewLogItem, new[] { log });
                    }
                    finally {
                        if (fsExe != null)
                        {
                            fsExe.Close();
                        }
                        if (fsInput != null)
                        {
                            fsInput.Close();
                        }
                    }
                    #endregion
                }
            }

            // Generate Log
            WorkerAgent.JobsRecivedLog recivelog = new WorkerAgent.JobsRecivedLog();
            ((WorkerAgent.JobsRecivedLog)recivelog).Jobs = recivedJobs.ToArray();
            recivelog.Message = "A batch of jobs recieved.";

            // Raise "NewLogItem" event
            EventExtensions.Raise(NewLogItem, new[] { recivelog });

            #endregion
        }
示例#2
0
文件: FormMain.cs 项目: mehrta/Quax
        private void AddLogToTextBox(WorkerAgent.Log log)
        {
            Color     timeColor         = Color.Gray;
            Color     messageColor      = Color.White;
            Color     errorMessageColor = Color.Red;
            const int INDENT_L1         = 30; // Level 1 indent size = 30;

            AppendText(DateTime.Now.ToLongTimeString() + "  ", timeColor);

            if (log is WorkerAgent.JobsRecivedLog)
            {
                #region JobsRecivedLog
                WorkerAgent.JobsRecivedLog l = (WorkerAgent.JobsRecivedLog)log;
                AppendText(l.Jobs.Length.ToString() + " job(s) recived:", messageColor);
                rtbLogs.AppendText(Environment.NewLine);
                for (int i = 0; i < l.Jobs.Length; i++)
                {
                    AppendText("[" + (i + 1).ToString() + "]    Job ID: ", Color.Aqua, INDENT_L1);
                    AppendText(l.Jobs[i].ID.ToString(), Color.Aqua, 30);
                    AppendText("  Executable length: " + l.Jobs[i].ExecutableFileLength.ToString() + " Bytes", Color.Aqua, INDENT_L1);
                    rtbLogs.AppendText(Environment.NewLine);
                }
                #endregion
            }
            else if (log is WorkerAgent.JobExecutionStartedLog)
            {
                #region JobExecutionStartedLog
                WorkerAgent.JobExecutionStartedLog l = (WorkerAgent.JobExecutionStartedLog)log;
                AppendText(l.Message + "  (Job ID: " + l.Job.ID.ToString() + ")", messageColor);

                #endregion
            }
            else if (log is WorkerAgent.JobResultSentLog)
            {
                #region JobResultSentLog
                WorkerAgent.JobResultSentLog l = (WorkerAgent.JobResultSentLog)log;
                AppendText(l.Message + "  (Job ID: " + l.Job.ID.ToString() + ")", messageColor);
                #endregion
            }
            else if (log is WorkerAgent.JobExecutedSuccessfullyLog)
            {
                #region JobExecutedSuccessfullyLog
                WorkerAgent.JobExecutedSuccessfullyLog l = (WorkerAgent.JobExecutedSuccessfullyLog)log;
                AppendText(l.Message + "  (Job ID: " + l.Job.ID.ToString() + ")", messageColor);
                #endregion
            }
            else if (log is WorkerAgent.WorkerStartedLog)
            {
                #region WorkerStartedLog
                WorkerAgent.WorkerStartedLog l = (WorkerAgent.WorkerStartedLog)log;
                AppendText(l.Message, Color.GreenYellow);
                #endregion
            }
            else if (log is WorkerAgent.WorkerStoppedLog)
            {
                #region WorkerStoppedLog
                WorkerAgent.WorkerStoppedLog l = (WorkerAgent.WorkerStoppedLog)log;
                AppendText(l.Message, messageColor);
                #endregion
            }
            else if (log is WorkerAgent.WorkerCouldNotStartLog)
            {
                #region WorkerCouldNotStartLog
                WorkerAgent.WorkerCouldNotStartLog l = (WorkerAgent.WorkerCouldNotStartLog)log;
                AppendText(l.Message, errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Error Message: " + l.ErrorMessage, errorMessageColor);
                #endregion
            }
            else if (log is WorkerAgent.WorkerCouldNotStopLog)
            {
                #region WorkerCouldNotStopLog
                WorkerAgent.WorkerCouldNotStopLog l = (WorkerAgent.WorkerCouldNotStopLog)log;
                AppendText(l.Message, errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Error Message: " + l.ErrorMessage, errorMessageColor);
                #endregion
            }
            else if (log is WorkerAgent.ErrorInJobExecutionLog)
            {
                #region ErrorInJobExecutionLog
                WorkerAgent.ErrorInJobExecutionLog l = (WorkerAgent.ErrorInJobExecutionLog)log;
                AppendText(l.Message, errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Job ID: " + l.Job.ID.ToString() + Environment.NewLine, errorMessageColor, INDENT_L1);
                AppendText("Job Executable: " + l.Job.ExecuteableFileName + Environment.NewLine, errorMessageColor, INDENT_L1);
                AppendText("Error: " + l.ErrorMessage, errorMessageColor, 30);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Is Win32 Error: " + (l.IsWin32Error ? "Yes" : "No"), errorMessageColor, INDENT_L1);
                if (l.IsWin32Error)
                {
                    rtbLogs.AppendText(Environment.NewLine);
                    AppendText("Win32 Error Code: " + l.Win32ErrorCode.ToString(), errorMessageColor, INDENT_L1);
                }
                #endregion
            }
            else if (log is WorkerAgent.ErrorInWritingJobToDiskLog)
            {
                #region ErrorInWritingJobToDiskLog
                WorkerAgent.ErrorInWritingJobToDiskLog l = (WorkerAgent.ErrorInWritingJobToDiskLog)log;
                AppendText(l.Message + "  (Job ID: " + l.Job.ID.ToString() + ")", errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Error Message: " + l.ErrorMessage, errorMessageColor);
                #endregion
            }
            else if (log is WorkerAgent.ErrorInReadingJobOutputFile)
            {
                #region ErrorInReadingJobOutputFile
                WorkerAgent.ErrorInReadingJobOutputFile l = (WorkerAgent.ErrorInReadingJobOutputFile)log;
                AppendText(l.Message + "  (Job ID: " + l.Job.ID.ToString() + ")", errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Error Message: " + l.ErrorMessage, errorMessageColor, INDENT_L1);
                #endregion
            }
            else if (log is WorkerAgent.ErrorInConnectingToJobServerLog)
            {
                #region ErrorInConnectingToJobServerLog
                WorkerAgent.ErrorInConnectingToJobServerLog l = (WorkerAgent.ErrorInConnectingToJobServerLog)log;
                AppendText(l.Message, errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                AppendText("Error Message: " + l.ErrorMessage, errorMessageColor, INDENT_L1);
                #endregion
            }
            else if (log is WorkerAgent.WorkerRegisterationFailedLog)
            {
                #region WorkerRegisterationFailedLog
                WorkerAgent.WorkerRegisterationFailedLog l = (WorkerAgent.WorkerRegisterationFailedLog)log;
                AppendText(l.Message, errorMessageColor);
                rtbLogs.AppendText(Environment.NewLine);
                #endregion
            }
            else if (log is WorkerAgent.WorkerRegisterationStartLog)
            {
                #region WorkerRegisterationStartLog
                WorkerAgent.WorkerRegisterationStartLog l = (WorkerAgent.WorkerRegisterationStartLog)log;
                AppendText(l.Message, Color.GreenYellow);
                #endregion
            }
            else if (log is WorkerAgent.WorkerRegisterdLog)
            {
                #region WorkerRegisterdLog
                WorkerAgent.WorkerRegisterdLog l = (WorkerAgent.WorkerRegisterdLog)log;
                AppendText(l.Message, Color.GreenYellow);
                rtbLogs.AppendText(Environment.NewLine);
                #endregion
            }

            else   // log is Log
            {
                AppendText(log.Message, messageColor);
            }

            // Move cursor to next line
            rtbLogs.AppendText(Environment.NewLine);
            rtbLogs.ScrollToCaret();
        }