示例#1
0
        static void Main(string[] args)
        {
            // resolve persistent node path.
            string pipeName = null;
            string nextActionPath = null;

            if (4 == args.Length && args[0] == "-pn" && args[2] == "-na")
            {
                pipeName = args[1];
                nextActionPath = args[3];
            }
            else
            {
                Console.WriteLine("Usage: -pn <pipe name> -na <next action file path>");
                Console.WriteLine();
                Console.WriteLine("The <pipe name> is any legal file name which uniquely distinguishes the pipe server.");
                Console.WriteLine("The <next action file path> is a text file containing a list of actions to execute in PowerShell.");
                return;
            }

            FileStream fileStream = null;
            StreamReader streamReader = null;

            try
            {
                // read next action file linewise.
                fileStream = new FileStream(nextActionPath, FileMode.OpenOrCreate, FileAccess.Read);
                streamReader = new StreamReader(fileStream);

                // use JSON transport to unmarshal initial nodes.
                ITransport transport = new JsonTransport();

                // create pipe server using JSON as transport.
                PipeServer pipeServer = new PipeServer(pipeName, transport);

                Console.WriteLine("Hit Ctrl+C to stop the server.");

                bool moreCommands = true;

                while (moreCommands)
                {
                    try
                    {
                        Console.WriteLine("Waiting for client to connect...");
                        pipeServer.WaitForConnection();

                        GetNextActionRequest request = pipeServer.Receive<GetNextActionRequest>();

                        if (null == request)
                        {
                            break;
                        }
                        Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                        for (;;)
                        {
                            string nextLine = streamReader.ReadLine();

                            if (null == nextLine)
                            {
                                moreCommands = false;
                                nextLine = "exit";
                            }
                            if (nextLine.Trim().Length > 0)
                            {
                                GetNextActionResponse response = new GetNextActionResponse(nextLine);

                                Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                pipeServer.Send(response);
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        pipeServer.Close();
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (null != streamReader)
                {
                    streamReader.Close();
                    streamReader = null;
                }
                if (null != fileStream)
                {
                    fileStream.Close();
                    fileStream = null;
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            // resolve persistent node path.
            string pipeName       = null;
            string nextActionPath = null;

            if (4 == args.Length && args[0] == "-pn" && args[2] == "-na")
            {
                pipeName       = args[1];
                nextActionPath = args[3];
            }
            else
            {
                Console.WriteLine("Usage: -pn <pipe name> -na <next action file path>");
                Console.WriteLine();
                Console.WriteLine("The <pipe name> is any legal file name which uniquely distinguishes the pipe server.");
                Console.WriteLine("The <next action file path> is a text file containing a list of actions to execute in PowerShell.");
                return;
            }

            FileStream   fileStream   = null;
            StreamReader streamReader = null;

            try
            {
                // read next action file linewise.
                fileStream   = new FileStream(nextActionPath, FileMode.OpenOrCreate, FileAccess.Read);
                streamReader = new StreamReader(fileStream);

                // use JSON transport to unmarshal initial nodes.
                ITransport transport = new JsonTransport();

                // create pipe server using JSON as transport.
                PipeServer pipeServer = new PipeServer(pipeName, transport);

                Console.WriteLine("Hit Ctrl+C to stop the server.");

                bool moreCommands = true;

                while (moreCommands)
                {
                    try
                    {
                        Console.WriteLine("Waiting for client to connect...");
                        pipeServer.WaitForConnection();

                        GetNextActionRequest request = pipeServer.Receive <GetNextActionRequest>();

                        if (null == request)
                        {
                            break;
                        }
                        Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                        for (;;)
                        {
                            string nextLine = streamReader.ReadLine();

                            if (null == nextLine)
                            {
                                moreCommands = false;
                                nextLine     = "exit";
                            }
                            if (nextLine.Trim().Length > 0)
                            {
                                GetNextActionResponse response = new GetNextActionResponse(nextLine);

                                Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                pipeServer.Send(response);
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    finally
                    {
                        pipeServer.Close();
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (null != streamReader)
                {
                    streamReader.Close();
                    streamReader = null;
                }
                if (null != fileStream)
                {
                    fileStream.Close();
                    fileStream = null;
                }
            }
        }