Ejemplo n.º 1
0
        public bool SendCmdRequest(CmdRequest request)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect("127.0.0.1", Port);
                using (NetworkStream stream = new NetworkStream(socket))
                {
                    using (BsonWriter writer = new BsonWriter(stream))
                    {
                        JsonSerializer serializer = new JsonSerializer();
                        serializer.Serialize(writer, request);
                        writer.Flush();
                        writer.Close();
                    }
                   
                    stream.Close();
                }
            }
            catch(Exception ex)
            {
                Logger.AddError(string.Format("Failed to send cmd request to checker with port = {0}", Port), ex);
                return false;
            }

            return true;
        }
Ejemplo n.º 2
0
        public void ProcessCmdRequest(CmdRequest request)
        {
            if (request == null)
            {
                return;
            }

            if (request.cmd == CmdTypeEnum.check.ToString())
            {
                // get all free processes
                var freeProcesses = processes.Where(p => !p.Bussy).ToArray();

                // loop throught tasks
                foreach (CmdServer server in request.servers)
                {
                    int i = 0;
                    for (i = 0; i < ProcessCmdRequestAttemptCount; i++)
                    {
                        if (currentProcess >= freeProcesses.Length)
                        {
                            currentProcess = 0;
                        }

                        IThreadProcess proc = freeProcesses[currentProcess++];
                        if (proc != null)
                        {
                            if (proc.SendCmdRequest(new CmdRequest { cmd = CmdTypeEnum.check.ToString(), servers = new List<CmdServer> { server } }))
                            {
                                OnTaskSent(server.id, proc.Port);
                                proc.Bussy = true;
                                break;
                            }
                        }
                    }

                    if (i >= ProcessCmdRequestAttemptCount)
                    {
                        OnTaskSendFailed(server.id);
                        Logger.AddError(string.Format("Failed to send cmd request to thread process with server id = {0}", server.id), new InvalidOperationException());
                    }
                }
            }
            else if (request.cmd == CmdTypeEnum.restart.ToString())
            {
                Stop();
                Start();
            }
            else if (request.cmd == CmdTypeEnum.kill.ToString())
            {
                Stop();
            }
        }
Ejemplo n.º 3
0
 private void OnNewCmdReceived(CmdRequest request)
 {
     if (request != null)
     {
         var handler = NewCmdReceived;
         if (handler != null)
         {
             handler(this, new NewCmdEventArgs { Cmd = request });
         }
     }
 }
Ejemplo n.º 4
0
        private void ProcessRequests(CheckerEntities db)
        {
            // if we have free processes
            if (availableProcesses > 0)
            {
                // getting not processed tasks
                var newTasks = db.Tasks.Where(t => !t.IsProcessed);
                if (newTasks != null && newTasks.Count() > 0)
                {
                    // take tasks according to free processes
                    newTasks = newTasks.Take(availableProcesses);

                    CmdRequest cmdRequest = new CmdRequest();
                    cmdRequest.cmd = CmdTypeEnum.check.ToString();
                    cmdRequest.servers = new List<CmdServer>();

                    foreach (var t in newTasks)
                    {
                        availableProcesses--;

                        t.IsProcessed = true;

                        CmdServer srv = new CmdServer
                        {
                            id = t.TaskId,
                            ip = t.Server.Trim(),
                            port = t.Port,
                            login = t.Login.Trim(),
                            pass = t.Password,
                            info = t.Info
                        };

                        cmdRequest.servers.Add(srv);
                    }

                    db.SaveChanges();

                    // notifying about new commands
                    OnNewCmd(cmdRequest);
                }
            }
        }