Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // resolve persistent node path.
            string nodeFilePath = null;

            if (2 == args.Length && args[0] == "-nf")
            {
                nodeFilePath = args[1];
            }
            else
            {
                Console.WriteLine("Usage: TestChefNodeCmdlet -nf <nodefile>");
                Console.WriteLine();
                Console.WriteLine("The nodefile is a JSON text file containing initial values and which receives any modified node values.");
                return;
            }

            // load initial node values, if any.
            string nodeFileText = ReadTextFile(nodeFilePath);

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

            if (nodeFileText.Length > 0)
            {
                nodeHash = (IDictionary)transport.NormalizeDeserializedObject(transport.ConvertStringToObject <object>(nodeFileText));
            }

            // current and new resource hashes.
            IDictionary currentResourceHash = new Hashtable();

            currentResourceHash.Add("name", "test name");

            IDictionary newResourceHash = new Hashtable(currentResourceHash);

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

            Console.WriteLine("Hit Ctrl+C to stop the server.");
            for (; ;)
            {
                try
                {
                    Console.WriteLine("Waiting for client to connect...");
                    pipeServer.WaitForConnection();
                    for (; ;)
                    {
                        // use duck typing to determine type of request.
                        IDictionary requestHash = (IDictionary)transport.NormalizeDeserializedObject(pipeServer.Receive <object>());

                        if (null == requestHash)
                        {
                            break;
                        }

                        bool validRequest = false;

                        if (requestHash.Contains(Constants.JSON_COMMAND_KEY))
                        {
                            if (requestHash.Contains(Constants.JSON_PATH_KEY) && requestHash.Contains(Constants.JSON_NODE_VALUE_KEY))
                            {
                                if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetChefNodeRequest")
                                {
                                    SetChefNodeRequest request = new SetChefNodeRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                        requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(nodeHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetChefNodeResponse response = new SetChefNodeResponse(request.Path);
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);

                                    // save change to node file.
                                    WriteTextFile(nodeFilePath, transport.ConvertObjectToString(nodeHash, true));
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetCurrentResourceRequest")
                                {
                                    SetCurrentResourceRequest request = new SetCurrentResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                                      requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(currentResourceHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetCurrentResourceResponse response = new SetCurrentResourceResponse();
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetNewResourceRequest")
                                {
                                    SetNewResourceRequest request = new SetNewResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                              requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(newResourceHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetNewResourceResponse response = new SetNewResourceResponse();
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                            }
                            else if (requestHash.Contains(Constants.JSON_PATH_KEY))
                            {
                                if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetChefNodeRequest")
                                {
                                    GetChefNodeRequest request = new GetChefNodeRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue             = QueryNodeHash(nodeHash, request.Path);
                                    GetChefNodeResponse response = new GetChefNodeResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetCurrentResourceRequest")
                                {
                                    GetCurrentResourceRequest request = new GetCurrentResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue = QueryNodeHash(currentResourceHash, request.Path);
                                    GetCurrentResourceResponse response = new GetCurrentResourceResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetNewResourceRequest")
                                {
                                    GetNewResourceRequest request = new GetNewResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue = QueryNodeHash(newResourceHash, request.Path);
                                    GetNewResourceResponse response = new GetNewResourceResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                            }
                        }
                        if (false == validRequest)
                        {
                            // unknown request type; hang up and try again.
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    pipeServer.Close();
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // resolve persistent node path.
            string nodeFilePath = null;

            if (2 == args.Length && args[0] == "-nf")
            {
                nodeFilePath = args[1];
            }
            else
            {
                Console.WriteLine("Usage: TestChefNodeCmdlet -nf <nodefile>");
                Console.WriteLine();
                Console.WriteLine("The nodefile is a JSON text file containing initial values and which receives any modified node values.");
                return;
            }

            // load initial node values, if any.
            string nodeFileText = ReadTextFile(nodeFilePath);

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

            if (nodeFileText.Length > 0)
            {
                nodeHash = (IDictionary)transport.NormalizeDeserializedObject(transport.ConvertStringToObject<object>(nodeFileText));
            }

            // current and new resource hashes.
            IDictionary currentResourceHash = new Hashtable();
            currentResourceHash.Add("name", "test name");

            IDictionary newResourceHash = new Hashtable(currentResourceHash);

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

            Console.WriteLine("Hit Ctrl+C to stop the server.");
            for (; ; )
            {
                try
                {
                    Console.WriteLine("Waiting for client to connect...");
                    pipeServer.WaitForConnection();
                    for (; ; )
                    {
                        // use duck typing to determine type of request.
                        IDictionary requestHash = (IDictionary)transport.NormalizeDeserializedObject(pipeServer.Receive<object>());

                        if (null == requestHash)
                        {
                            break;
                        }

                        bool validRequest = false;

                        if (requestHash.Contains(Constants.JSON_COMMAND_KEY))
                        {
                            if (requestHash.Contains(Constants.JSON_PATH_KEY) && requestHash.Contains(Constants.JSON_NODE_VALUE_KEY))
                            {
                                if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetChefNodeRequest")
                                {
                                    SetChefNodeRequest request = new SetChefNodeRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                        requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(nodeHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetChefNodeResponse response = new SetChefNodeResponse(request.Path);
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);

                                    // save change to node file.
                                    WriteTextFile(nodeFilePath, transport.ConvertObjectToString(nodeHash, true));
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetCurrentResourceRequest")
                                {
                                    SetCurrentResourceRequest request = new SetCurrentResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                                      requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(currentResourceHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetCurrentResourceResponse response = new SetCurrentResourceResponse();
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "SetNewResourceRequest")
                                {
                                    SetNewResourceRequest request = new SetNewResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY],
                                                                                              requestHash[Constants.JSON_NODE_VALUE_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    InsertNodeHash(newResourceHash, request.Path, transport.NormalizeDeserializedObject(request.NodeValue));

                                    SetNewResourceResponse response = new SetNewResourceResponse();
                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                            }
                            else if (requestHash.Contains(Constants.JSON_PATH_KEY))
                            {
                                if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetChefNodeRequest")
                                {
                                    GetChefNodeRequest request = new GetChefNodeRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue = QueryNodeHash(nodeHash, request.Path);
                                    GetChefNodeResponse response = new GetChefNodeResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetCurrentResourceRequest")
                                {
                                    GetCurrentResourceRequest request = new GetCurrentResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue = QueryNodeHash(currentResourceHash, request.Path);
                                    GetCurrentResourceResponse response = new GetCurrentResourceResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                                else if (requestHash[Constants.JSON_COMMAND_KEY].ToString() == "GetNewResourceRequest")
                                {
                                    GetNewResourceRequest request = new GetNewResourceRequest((ICollection)requestHash[Constants.JSON_PATH_KEY]);
                                    Console.WriteLine(String.Format("Received: {0}", request.ToString()));

                                    object nodeValue = QueryNodeHash(newResourceHash, request.Path);
                                    GetNewResourceResponse response = new GetNewResourceResponse(request.Path, nodeValue);

                                    Console.WriteLine(String.Format("Responding: {0}", response.ToString()));
                                    pipeServer.Send(response);
                                    validRequest = true;
                                }
                            }
                        }
                        if (false == validRequest)
                        {
                            // unknown request type; hang up and try again.
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    pipeServer.Close();
                }
            }
        }