示例#1
0
        public static void parse(Process p, SocketComm comm)
        {
            string line = comm.readLine();          //reader.ReadLine();

            while (line.Equals(""))
            {
                line = comm.readLine();
            }
            if (line.Equals("define process"))
            {
                defineProcess(p, comm);
                line = "";
                return;
            }
            if (line.Equals("start process"))
            {
                startProcess(p, comm);
                line = "";
                return;
            }
            if (line.Equals("stop process"))
            {
                stopProcess(p, comm);
                line = "";
                return;
            }
            if (line.Equals("undefine process"))
            {
                undefineProcess(p, comm);
                line = "";
                return;
            }
        }
示例#2
0
        private static void defineProcess(Process p, SocketComm comm)
        {
            string line = comm.readLine();          //reader.ReadLine();

            while (!line.Equals(""))
            {
                if (line.StartsWith("status:"))
                {
                    line = line.Remove(0, 7);
                    line = line.Trim();
                    if (line.Equals("1"))
                    {
                        p.setDefined(true);
                        p.setStatus(Process.Status.Stopped);
                    }
                    else
                    {
                        p.setDefined(false);
                    }
                }
                if (line.StartsWith("id:"))
                {
                    line = line.Remove(0, 3);
                    line = line.Trim();
                    p.setId(line);
                }
                line = comm.readLine();
            }
        }
示例#3
0
        private void OnConnect(IAsyncResult asyncResult)
        {
            Socket serverSock = (Socket)asyncResult.AsyncState;

            serverSock.EndConnect(asyncResult);
            Logger.Info("Connected.");
            _connectDone.Set();
            SocketComm serverComm = new SocketComm {
                ParentComm = this,
                WorkSocket = serverSock
            };

            //AppLog.WriteLine(5, "STATUS", "Waiting for data...");
            Logger.Debug("Starting Recieve Buffer.");
            serverComm.WorkSocket.BeginReceive(serverComm.Buffer, 0, SocketComm.BufferSize, 0, OnDataReceived, serverComm);
            Logger.Debug("Negotiating IRC Logon.");
            if (_parentServer.Pass != "")
            {
                _parentServer.QueueSend("PASS " + _parentServer.Pass, true);
                _parentServer.QueueSend("USER " + _parentServer.Nick + " 8 * : " + _parentServer.RealName, true);
                _parentServer.QueueSend("NICK " + _parentServer.Nick, true);
            }
            else
            {
                _parentServer.QueueSend("USER " + _parentServer.Nick + " 8 * : " + _parentServer.RealName, true);
                _parentServer.QueueSend("NICK " + _parentServer.Nick, true);
            }
            _parentServer.StartSendQueueConsumer();
        }
示例#4
0
 public Computer(string name, int port)
 {
     m_name      = name;
     m_status    = Status.Disconnected;
     m_processes = new ArrayList();
     m_cpcdPort  = port;
     m_socket    = new SocketComm(m_name, m_cpcdPort);
 }
示例#5
0
        private void OnDataReceived(IAsyncResult asyncResult)
        {
            SocketComm serverComm  = (SocketComm)asyncResult.AsyncState;
            Socket     sockHandler = serverComm.WorkSocket;

            try {
                Int32 bytesRead = sockHandler.EndReceive(asyncResult);
                if (bytesRead > 0)
                {
                    Char[] tempByteArr     = new Char[bytesRead];
                    Int32  receivedLen     = Encoding.UTF8.GetChars(serverComm.Buffer, 0, bytesRead, tempByteArr, 0);
                    Char[] receivedCharArr = new Char[receivedLen];
                    Array.Copy(tempByteArr, receivedCharArr, receivedLen);
                    String receivedData = new String(receivedCharArr);
                    serverComm.StringBuffer += receivedData;
                    // Per RFC1459:
                    //    The protocol messages must be extracted from the contiguous stream of octets. The current solution
                    //    is to designate two characters, CR and LF, as message separators. Empty messages are silently ignored,
                    //    which permits use of the sequence CR-LF between messages without extra problems.
                    Int32 indexOfEndLine = serverComm.StringBuffer.IndexOfAny(new[] { '\r', '\n' });
                    while (indexOfEndLine > -1)
                    {
                        if (indexOfEndLine == 0)
                        {
                            // If CR or LF is the first character of the line, remove it.
                            serverComm.StringBuffer = serverComm.StringBuffer.Remove(0, 1);
                        }
                        else
                        {
                            // If a CR or LF is not the first character
                            // Send it off to the parser. Passing current time as the "master time" the message was received.
                            // Could this be a bottle neck? Implement some sort of queue to free the socket?
                            _parentServer.ParseRawLine(serverComm.StringBuffer.Substring(0, indexOfEndLine), DateTime.UtcNow);
                            // Remove the line from the beginning of the buffer
                            serverComm.StringBuffer = serverComm.StringBuffer.Remove(0, indexOfEndLine);
                        }
                        // Seed the next value
                        indexOfEndLine = serverComm.StringBuffer.IndexOfAny(new[] { '\r', '\n' });
                    }
                    // Begin receiving data on the socket again.
                    sockHandler.BeginReceive(serverComm.Buffer, 0, SocketComm.BufferSize, 0, OnDataReceived, serverComm);
                }
                else
                {
                    OnDisconnect(sockHandler);
                }
            }
            catch (SocketException se) {
                if (se.ErrorCode == 10054)
                {
                    OnDisconnect(sockHandler);
                }
            }
            catch (ObjectDisposedException) {
                // The socket was closed previously and then came back, so just exit gracefully.
                //return;
            }
        }
示例#6
0
 public Computer(string name, string ip)
 {
     m_ip        = ip;
     m_name      = name;
     m_status    = Status.Disconnected;
     m_processes = new ArrayList();
     m_cpcdPort  = 1234;          //default port
     m_socket    = new SocketComm(m_ip, m_cpcdPort);
 }
示例#7
0
        public static void parse(ArrayList processes, Computer c, SocketComm comm)
        {
            string line = comm.readLine();          //reader.ReadLine();

            while (line.Equals(""))
            {
                line = comm.readLine();
            }

            if (line.Equals("start processes"))
            {
                listProcesses(processes, c, comm);
                line = "";
                return;
            }
        }
示例#8
0
        private static void stopProcess(Process p, SocketComm comm)
        {
            string line = comm.readLine();          //reader.ReadLine();

            while (!line.Equals(""))
            {
                if (line.StartsWith("status:"))
                {
                    line = line.Remove(0, 7);
                    line = line.Trim();
                    if (line.Equals("1"))
                    {
                        p.setStatus(NDB_CPC.Process.Status.Stopped);
                    }
                    else
                    {
                        p.setStatus(NDB_CPC.Process.Status.Unknown);
                    }
                }
                if (line.StartsWith("id:"))
                {
                    line = line.Remove(0, 3);
                    line = line.Trim();
                    if (p.getId().Equals(line))
                    {
                        ;
                    }
                    else
                    {
                        //damn something is wrong
                        p.setStatus(NDB_CPC.Process.Status.Unknown);
                    }
                }
                line = comm.readLine();
            }
        }
示例#9
0
        private static void listProcesses(ArrayList processes, Computer c, SocketComm comm)
        {
            bool processExist = false;

            string line = comm.readLine();          //reader.ReadLine();

            while (!line.Equals("end processes"))
            {
                if (line.Equals("process"))
                {
                    line = comm.readLine();
                    Process p = new Process();

                    while (!line.Equals(""))
                    {
                        if (line.StartsWith("id:"))
                        {
                            string pid;
                            line = line.Remove(0, 3);
                            pid  = line.Trim();
                            /*check if process already exist*/
                            processExist = findProcess(processes, pid);
                            if (!processExist)
                            {
                                p.setId(pid);
                            }
                        }

                        if (line.StartsWith("name:"))
                        {
                            line = line.Remove(0, 5);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setName(line);
                            }
                        }

                        if (line.StartsWith("path:"))
                        {
                            line = line.Remove(0, 5);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setPath(line);
                            }
                        }

                        if (line.StartsWith("args:"))
                        {
                            line = line.Remove(0, 5);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setArgs(line);
                            }
                        }

                        if (line.StartsWith("type:"))
                        {
                            line = line.Remove(0, 5);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                            }
                        }

                        if (line.StartsWith("cwd:"))
                        {
                            line = line.Remove(0, 4);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setCwd(line);
                            }
                        }

                        if (line.StartsWith("env:"))
                        {
                            line = line.Remove(0, 4);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setEnv(line);
                            }
                        }

                        if (line.StartsWith("owner:"))
                        {
                            line = line.Remove(0, 6);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setOwner(line);
                            }
                        }
                        if (line.StartsWith("group:"))
                        {
                            line = line.Remove(0, 6);
                            line = line.Trim();
                            /*check if process already exist*/
                            if (!processExist)
                            {
                                p.setDatabase(line);
                            }
                        }

                        if (line.StartsWith("status:"))
                        {
                            line = line.Remove(0, 7);
                            line = line.Trim();
                            /*check if process already exist*/
                            //if(!processExist)
                            //{
                            if (line.Equals("0"))
                            {
                                p.setStatus(Process.Status.Stopped);
                            }
                            if (line.Equals("1"))
                            {
                                p.setStatus(Process.Status.Running);
                            }
                            if (line.Equals("2"))
                            {
                                p.setStatus(Process.Status.Unknown);
                            }
                            //}
                        }


                        line = comm.readLine();
                    }
                    if (!processExist)
                    {
                        p.setComputer(c);
                        p.setDefined(true);
                        processes.Add(p);
                    }
                    processExist = false;
                }
                line = comm.readLine();
            }
        }