Beispiel #1
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            if (Application.ActiveContext != null && Application.ActiveContext.IsIsolated)
            {
                Error.WriteLine("a context is already opened: try to disconnect first");
                Error.WriteLine();
                return(CommandResultCode.ExecutionFailed);
            }

            if (!args.MoveNext())
            {
                return(CommandResultCode.SyntaxError);
            }

            if (args.Current != "to")
            {
                return(CommandResultCode.SyntaxError);
            }

            if (!args.MoveNext())
            {
                return(CommandResultCode.SyntaxError);
            }

            string          address = args.Current;
            IServiceAddress serviceAddress;

            try {
                serviceAddress = ServiceAddresses.ParseString(address);
            } catch (Exception) {
                Error.WriteLine("Invalid service address specified: {0}", address);
                return(CommandResultCode.ExecutionFailed);
            }

            NetworkConfigSource configSource = new NetworkConfigSource();

            try {
                configSource.AddNetworkNode(serviceAddress);
            } catch (Exception e) {
                Error.WriteLine("The address '" + address + "' is invalid: " + e.Message);
                return(CommandResultCode.ExecutionFailed);
            }

            string protocol    = "tcp";
            string credentials = String.Empty;
            string format      = "binary";

            if (args.MoveNext())
            {
                if (args.Current == "identified")
                {
                    if (!args.MoveNext())
                    {
                        return(CommandResultCode.SyntaxError);
                    }
                    if (args.Current != "by")
                    {
                        return(CommandResultCode.SyntaxError);
                    }
                    if (!args.MoveNext())
                    {
                        return(CommandResultCode.SyntaxError);
                    }

                    credentials = args.Current;

                    if (args.MoveNext())
                    {
                        if (args.Current != "on")
                        {
                            return(CommandResultCode.SyntaxError);
                        }

                        protocol = args.Current;

                        if (args.MoveNext())
                        {
                            if (args.Current != "with")
                            {
                                return(CommandResultCode.SyntaxError);
                            }

                            format = args.Current;
                        }
                    }
                }
                else if (args.Current == "on")
                {
                    if (!args.MoveNext())
                    {
                        return(CommandResultCode.SyntaxError);
                    }

                    protocol = args.Current;

                    if (args.MoveNext())
                    {
                        if (args.Current != "with")
                        {
                            return(CommandResultCode.SyntaxError);
                        }

                        format = args.Current;
                    }
                }
                else if (args.Current == "with")
                {
                    if (!args.MoveNext())
                    {
                        return(CommandResultCode.SyntaxError);
                    }

                    format = args.Current;
                }
                else
                {
                    return(CommandResultCode.SyntaxError);
                }
            }

            IServiceConnector connector;

            if (protocol == "tcp")
            {
                if (String.IsNullOrEmpty(credentials))
                {
                    while (String.IsNullOrEmpty(credentials = Readline.ReadPassword("password: "******"please provide a valid password...");
                    }
                    Out.WriteLine();
                }
                connector = new TcpServiceConnector(credentials);
            }
            else if (protocol == "http")
            {
                string userName = credentials;
                string password = null;
                int    index    = credentials.IndexOf(':');
                if (index != -1)
                {
                    password = credentials.Substring(index + 1);
                    userName = credentials.Substring(0, index);
                }

                if (String.IsNullOrEmpty(password))
                {
                    while (String.IsNullOrEmpty(password = Readline.ReadPassword("password: "******"please provide a valid password...");
                    }

                    Out.WriteLine();
                }

                // TODO: connector = new HttpServiceConnector(userName, password);
                Out.WriteLine("Not supported yet.");
                return(CommandResultCode.ExecutionFailed);
            }
            else
            {
                return(CommandResultCode.SyntaxError);
            }

            IMessageSerializer serializer;

            if (format == "binary")
            {
                serializer = new BinaryRpcMessageSerializer();
            }
            else if (format == "xml")
            {
                //TODO: serializer = new XmlRpcMessageSerializer();
                return(CommandResultCode.ExecutionFailed);
            }
            else if (format == "json")
            {
                if (JsonRpcMessageSerializer == null)
                {
                    Error.WriteLine("JSON serializer was not installed.");
                    Error.WriteLine();
                    return(CommandResultCode.ExecutionFailed);
                }
                serializer = JsonRpcMessageSerializer;
            }
            else
            {
                return(CommandResultCode.SyntaxError);
            }

            connector.MessageSerializer = serializer;

            NetworkProfile networkProfile = new NetworkProfile(connector);

            networkProfile.Configuration = configSource;

            //TODO: test the connection is correct ...

            ((CloudAdmin)Application).SetNetworkContext(new NetworkContext(networkProfile));

            Out.WriteLine("connected successfully to {0}", address);
            Out.WriteLine();

            return(CommandResultCode.Success);
        }
Beispiel #2
0
        public override bool HandleCommandLine(CommandLine commandLine)
        {
            string protocol = commandLine.GetOptionValue("protocol", "tcp");
            string address  = commandLine.GetOptionValue("address", null);
            string format   = commandLine.GetOptionValue("format", "binary");

            if (String.IsNullOrEmpty(address))
            {
                return(false);
            }

            IServiceConnector connector;

            if (protocol.Equals("tcp", StringComparison.InvariantCultureIgnoreCase))
            {
                string netPassword = commandLine.GetOptionValue("password");
                if (String.IsNullOrEmpty(netPassword))
                {
                    throw new ArgumentException("Netwrok password required for TCP/IP protocol.");
                }

                connector = new TcpServiceConnector(netPassword);
            }
            else if (protocol.Equals("http", StringComparison.InvariantCultureIgnoreCase))
            {
                string user     = commandLine.GetOptionValue("user");
                string password = commandLine.GetOptionValue("password");
                if (String.IsNullOrEmpty(user))
                {
                    throw new ArgumentException("User name not specified. for HTTP connection.");
                }
                if (String.IsNullOrEmpty(password))
                {
                    throw new ArgumentException("Password not specofoed for HTTP connection.");
                }
                //TODO: connector = new HttpServiceConnector(user, password);
                throw new NotSupportedException("HTTP not supported yet.");
            }
            else
            {
                throw new ArgumentException("Invalid protocol '" + protocol + "'.");
            }

            IMessageSerializer serializer;

            if (format.Equals("xml", StringComparison.InvariantCultureIgnoreCase))
            {
                //TODO: serializer = new XmlRpcMessageSerializer();
                throw new NotSupportedException("XML format not supported yet.");
            }
            else if (format.Equals("binary", StringComparison.InvariantCultureIgnoreCase))
            {
                serializer = new BinaryRpcMessageSerializer();
            }
            else if (format.Equals("json", StringComparison.InvariantCultureIgnoreCase))
            {
                if (JsonRpcMessageSerializer == null)
                {
                    throw new ApplicationException("The JSON serializer was not installed.");
                }
                serializer = JsonRpcMessageSerializer;
            }
            else
            {
                throw new ArgumentException("Invalid message format.");
            }

            connector.MessageSerializer = serializer;
            NetworkProfile networkProfile = new NetworkProfile(connector);

            NetworkConfigSource configSource = new NetworkConfigSource();

            configSource.AddNetworkNode(address);
            networkProfile.Configuration = configSource;

            ((CloudAdmin)Application).SetNetworkContext(new NetworkContext(networkProfile));
            return(true);
        }