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."); connector = new HttpServiceConnector(user, password); } else { throw new ArgumentException("Invalid protocol '" + protocol + "'."); } IMessageSerializer serializer; if (format.Equals("xml", StringComparison.InvariantCultureIgnoreCase)) { serializer = new XmlRpcMessageSerializer(); } 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; }
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(); } connector = new HttpServiceConnector(userName, password); } else { return CommandResultCode.SyntaxError; } IMessageSerializer serializer; if (format == "binary") { serializer = new BinaryRpcMessageSerializer(); } else if (format == "xml") { serializer = new XmlRpcMessageSerializer(); } 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; }
private Message DoProcess(Message messageStream, int tryCount) { TcpConnection c = null; try { // Check if there's a connection in the pool already, c = connector.GetConnection(address); lock (c) { // Write the message. char code = '\0'; if (serviceType == ServiceType.Manager) { code = 'm'; } else if (serviceType == ServiceType.Root) { code = 'r'; } else if (serviceType == ServiceType.Block) { code = 'b'; } else if (serviceType == ServiceType.Admin) { code = 'a'; } BinaryWriter writer = new BinaryWriter(c.Stream, Encoding.Unicode); writer.Write(code); IMessageSerializer serializer = new BinaryRpcMessageSerializer(); serializer.Serialize(messageStream, c.Stream); writer.Flush(); Message response = serializer.Deserialize(c.Stream, MessageType.Response); if (response is MessageStream) { return response; } else { return new ResponseMessage((RequestMessage) messageStream, (ResponseMessage) response); } } } catch (Exception e) { // If this is a 'connection reset by peer' error, wipe the connection // from the cache and retry connection, if (tryCount == 0 && (e is SocketException || e is EndOfStreamException)) { connector.InvalidateConnection(address); // And retry, return DoProcess(messageStream, tryCount + 1); } MessageError error; if (e is EndOfStreamException) { error = new MessageError(new Exception("EOF (is net password correct?)", e)); } else { // Report this error as a msg_stream fault, error = new MessageError(new Exception(e.Message, e)); } Message responseMessage; if (messageStream is MessageStream) { responseMessage = new MessageStream(MessageType.Response); ResponseMessage inner = new ResponseMessage(); inner.Arguments.Add(error); ((MessageStream)responseMessage).AddMessage(inner); } else { responseMessage = ((RequestMessage) messageStream).CreateResponse(); responseMessage.Arguments.Add(error); } return responseMessage; } finally { if (c != null) connector.ReleaseConnection(c); } }