Esempio n. 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);
			}
		}
Esempio n. 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));
        }
Esempio n. 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);
            }
        }
Esempio n. 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());
        }
Esempio n. 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);
        }
Esempio n. 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;
            }
        }
Esempio n. 7
0
        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();
        }
Esempio n. 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);
        }
Esempio n. 9
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();
        }
Esempio n. 10
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);
        }