Exemple #1
0
		public static PathInfo Parse(string name, string s) {
			String[] parts = s.Split('|');

			try {
				string type = parts[0];
				int versionNumber = Int32.Parse(parts[1]);
				int sz = parts.Length - 2;
				IServiceAddress rootLeader = null;
				IServiceAddress[] servers = new IServiceAddress[sz];

				for (int i = 0; i < sz; ++i) {
					bool isLeader = false;
					String item = parts[i + 2];
					if (item.StartsWith("*")) {
						item = item.Substring(1);
						isLeader = true;
					}

					IServiceAddress addr = ServiceAddresses.ParseString(item);
					servers[i] = addr;
					if (isLeader) {
						rootLeader = addr;
					}
				}

				// Return the PathInfo object,
				return new PathInfo(name, type, versionNumber, rootLeader, servers);
			} catch (IOException e) {
				throw new ApplicationException(e.Message, e);
			}
		}
Exemple #2
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            NetworkContext networkContext = context as NetworkContext;

            if (networkContext == null)
            {
                return(CommandResultCode.ExecutionFailed);
            }

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

            string role = args.Current;

            IServiceAddress address = null;

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

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

                try {
                    address = ServiceAddresses.ParseString(args.Current);
                } catch (Exception) {
                    Error.WriteLine("Invalid service address");
                    return(CommandResultCode.ExecutionFailed);
                }
            }
            else
            {
                IServiceAddress[] addresses = networkContext.Network.Configuration.NetworkNodes;
                if (addresses != null && addresses.Length == 1)
                {
                    address = addresses[0];
                }
            }

            if (address == null)
            {
                Error.WriteLine("cannot determine the address of the service to stop.");
                return(CommandResultCode.ExecutionFailed);
            }

            return(StopRole(networkContext, role, address));
        }
Exemple #3
0
        public IServiceAddress Get(string path)
        {
            string rootServerStr = dictionary.GetValue(path);

            if (rootServerStr == null)
            {
                return(null);
            }

            try {
                return(ServiceAddresses.ParseString(rootServerStr));
            } catch (Exception e) {
                throw new FormatException("Unable to parse service address: " + e.Message);
            }
        }
Exemple #4
0
        private static IServiceAddress[] ParseAddress(string value)
        {
            List <IServiceAddress> addresses = new List <IServiceAddress>();

            string[] sp = value.Split(',');
            for (int i = 0; i < sp.Length; i++)
            {
                string s = sp[i].Trim();
                if (s.Length > 0)
                {
                    addresses.Add(ServiceAddresses.ParseString(s));
                }
            }

            return(addresses.ToArray());
        }
Exemple #5
0
        public void AddNetworkNode(string address)
        {
            if (String.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException("address");
            }

            IServiceAddress serviceAddress = ServiceAddresses.ParseString(address);

            if (serviceAddress == null)
            {
                throw new ArgumentException("The address '" + address + "' is not supported.");
            }

            AddNetworkNode(serviceAddress);
        }
Exemple #6
0
        private IServiceAddress[] ParseMachineAddressList(string rootAddress)
        {
            String[] machines = rootAddress.Split(',');

            try {
                IServiceAddress[] services = new IServiceAddress[machines.Length];
                for (int i = 0; i < machines.Length; ++i)
                {
                    services[i] = ServiceAddresses.ParseString(machines[i].Trim());
                }
                return(services);
            } catch (FormatException e) {
                Out.WriteLine("Error parsing machine address: " + e.Message);
                throw;
            }
        }
        protected override void OnStart()
        {
            try {
                // Read the manager server address from the properties file,
                Properties p = new Properties();

                // Contains the root properties,
                string propFile = Path.Combine(path, "00.properties");
                if (File.Exists(propFile))
                {
                    FileStream fin = new FileStream(propFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                    p.Load(fin);
                    fin.Close();
                }

                // Fetch the manager server property,
                String v = p.GetProperty("manager_server_address");
                if (v != null)
                {
                    String[] addresses = v.Split(',');
                    int      sz        = addresses.Length;
                    ManagerServices = new IServiceAddress[sz];
                    for (int i = 0; i < sz; ++i)
                    {
                        ManagerServices[i] = ServiceAddresses.ParseString(addresses[i]);
                    }
                }
            } catch (IOException e) {
                throw new ApplicationException("IO Error: " + e.Message);
            }

            // Adds all the files to the path info queue,
            string[] rootFiles = Directory.GetFiles(path);
            foreach (string f in rootFiles)
            {
                String fname = Path.GetFileName(f);
                if (!fname.Contains("."))
                {
                    pathInitializationQueue.Add(f);
                }
            }

            base.OnStart();
        }
Exemple #8
0
        private CommandResultCode RemovePath(NetworkContext context, string pathName, string rootAddress)
        {
            IServiceAddress address;

            // If machine is null, we need to find the machine the path is on,
            if (rootAddress == null)
            {
                PathInfo pathInfo = context.Network.GetPathInfo(pathName);
                if (pathInfo == null)
                {
                    Out.WriteLine("The path '" + pathName + "' was not found.");
                    return(CommandResultCode.ExecutionFailed);
                }
                address = pathInfo.RootLeader;
            }
            else
            {
                address = ServiceAddresses.ParseString(rootAddress);
            }

            Out.WriteLine("Removing path " + pathName + " from root " + address);
            Out.Flush();

            MachineProfile p = context.Network.GetMachineProfile(address);

            if (p == null)
            {
                Out.WriteLine("Error: Machine was not found in the network schema.");
                return(CommandResultCode.ExecutionFailed);
            }
            if (!p.IsRoot)
            {
                Out.WriteLine("Error: Given machine is not a root.");
                return(CommandResultCode.ExecutionFailed);
            }

            // Remove the path,
            context.Network.RemovePathFromNetwork(pathName, address);
            Out.WriteLine("done.");
            return(CommandResultCode.Success);
        }
Exemple #9
0
        public void AddNetworkNode(IServiceAddress address)
        {
            lock (stateLock) {
                IServiceAddress[] nodes;
                string            value = GetString(NetworkNodeList, null);
                if (value != null)
                {
                    nodes = ParseAddress(value);
                    int index = Array.BinarySearch(nodes, address);
                    if (index >= 0)
                    {
                        throw new ArgumentException("The address '" + address + "' is already present.");
                    }
                    IServiceAddress[] oldNodes = nodes;
                    nodes = new IServiceAddress[oldNodes.Length + 1];
                    Array.Copy(oldNodes, 0, nodes, 0, oldNodes.Length);
                    nodes[nodes.Length - 1] = address;
                }
                else
                {
                    nodes = new IServiceAddress[] { address };
                }

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < nodes.Length; i++)
                {
                    IServiceAddressHandler handler = ServiceAddresses.GetHandler(nodes[i]);
                    sb.Append(handler.ToString(nodes[i]));

                    if (i < nodes.Length - 1)
                    {
                        sb.Append(", ");
                    }
                }

                SetValue(NetworkNodeList, sb.ToString());
            }
        }
Exemple #10
0
        protected override void OnStart()
        {
            localDb = new FileSystemDatabase(dbpath);
            localDb.Start();

            SetBlockDatabase(localDb);

            // Read the unique id value,
            string f = Path.Combine(basePath, ManagerProperties);

            if (File.Exists(f))
            {
                StreamReader reader = new StreamReader(f);
                string       line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("id="))
                    {
                        int uniqueId = Int32.Parse(line.Substring(3));
                        UniqueManagerId = uniqueId;
                    }
                }
                reader.Close();
            }

            // Read all the registered block servers that were last persisted and
            // populate the manager with them,
            f = Path.Combine(basePath, RegisteredBlockServers);
            if (File.Exists(f))
            {
                StreamReader reader = new StreamReader(f);
                string       line;
                while ((line = reader.ReadLine()) != null)
                {
                    int             p    = line.IndexOf(",");
                    long            guid = Int64.Parse(line.Substring(0, p));
                    IServiceAddress addr = ServiceAddresses.ParseString(line.Substring(p + 1));
                    AddRegisteredBlockService(guid, addr);
                }
                reader.Close();
            }

            // Read all the registered root servers that were last persisted and
            // populate the manager with them,
            f = Path.Combine(basePath, RegisteredRootServers);
            if (File.Exists(f))
            {
                StreamReader reader = new StreamReader(f);
                string       line;
                while ((line = reader.ReadLine()) != null)
                {
                    IServiceAddress addr = ServiceAddresses.ParseString(line);
                    AddRegisteredRootService(addr);
                }
                reader.Close();
            }

            // Read all the registered manager servers that were last persisted and
            // populate the manager with them,
            f = Path.Combine(basePath, RegisteredManagerServers);
            if (File.Exists(f))
            {
                StreamReader reader = new StreamReader(f);
                string       line;
                while ((line = reader.ReadLine()) != null)
                {
                    IServiceAddress addr = ServiceAddresses.ParseString(line);
                    AddRegisteredManagerService(addr);
                }
                reader.Close();
            }

            // Perform the initialization procedure (contacts the other managers and
            // syncs data).
            base.OnStart();
        }
            public void Process(object state)
            {
                Socket socket = (Socket)state;

                Stream socket_in  = new NetworkStream(socket, FileAccess.Read);
                Stream socket_out = new NetworkStream(socket, FileAccess.Write);

                try {
                    // 30 minute timeout on proxy connections,
                    socket.SendTimeout = 30 * 60 * 1000;

                    // Wrap the input stream in a data and buffered input stream,
                    BufferedStream bin = new BufferedStream(socket_in, 1024);
                    BinaryReader   din = new BinaryReader(bin);

                    // Wrap the output stream in a data and buffered output stream,
                    BufferedStream bout = new BufferedStream(socket_out, 1024);
                    BinaryWriter   dout = new BinaryWriter(bout);

                    // Perform the handshake,
                    DateTime systemtime = DateTime.Now;
                    dout.Write(systemtime.ToUniversalTime().ToBinary());
                    dout.Flush();
                    long back = din.ReadInt64();
                    if (systemtime.ToUniversalTime().ToBinary() != back)
                    {
                        throw new IOException("Bad protocol request");
                    }
                    dout.Write("CloudB Proxy Service");
                    dout.Flush();
                    string net_password = din.ReadString();

                    // The connector to proxy commands via,
                    TcpServiceConnector connector = new TcpServiceConnector(net_password);

                    // The rest of the communication will be command requests;
                    while (true)
                    {
                        // Read the command,
                        char command = din.ReadChar();
                        if (command == '0')
                        {
                            // Close connection if we receive a '0' command char
                            dout.Close();
                            din.Close();
                            return;
                        }

                        int  addressCode = din.ReadInt32();
                        Type addressType = ServiceAddresses.GetAddressType(addressCode);
                        if (addressType == null || addressType != typeof(TcpServiceAddress))
                        {
                            throw new ApplicationException("Invalid address type.");
                        }

                        int    addressLength = din.ReadInt32();
                        byte[] addressBytes  = new byte[addressLength];
                        din.Read(addressBytes, 0, addressLength);

                        IServiceAddressHandler handler = ServiceAddresses.GetHandler(addressType);
                        TcpServiceAddress      address = (TcpServiceAddress)handler.FromBytes(addressBytes);
                        IEnumerable <Message>  request = service.MessageSerializer.Deserialize(din.BaseStream);

                        Message response;

                        // Proxy the command over the network,
                        if (command == 'a')
                        {
                            response = connector.Connect(address, ServiceType.Admin).Process(request);
                        }
                        else if (command == 'b')
                        {
                            response = connector.Connect(address, ServiceType.Block).Process(request);
                        }
                        else if (command == 'm')
                        {
                            response = connector.Connect(address, ServiceType.Manager).Process(request);
                        }
                        else if (command == 'r')
                        {
                            response = connector.Connect(address, ServiceType.Root).Process(request);
                        }
                        else
                        {
                            throw new IOException("Unknown command to proxy: " + command);
                        }

                        // Return the result,
                        service.MessageSerializer.Serialize(response, dout.BaseStream);
                        dout.Flush();
                    }
                } catch (SocketException e) {
                    if (e.ErrorCode == (int)SocketError.ConnectionReset)
                    {
                        // Ignore connection reset messages,
                    }
                } catch (IOException e) {
                    if (e is EndOfStreamException)
                    {
                        // Ignore this one oo,
                    }
                    else
                    {
                        service.Logger.Error("IO Error during connection input", e);
                    }
                } finally {
                    // Make sure the socket is closed before we return from the thread,
                    try {
                        socket.Close();
                    } catch (IOException e) {
                        service.Logger.Error("IO Error on connection close", e);
                    }
                }
            }
Exemple #12
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);
        }