Exemplo n.º 1
0
 /// <summary>
 /// Method called when the message has been received.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     lock (Endpoint.lockedResources)
     {
         return(Task.FromResult <bool>(!Endpoint.lockedResources.ContainsKey(this.Resource)));
     }
 }
Exemplo n.º 2
0
        public void TestCleanup()
        {
            this.endpoint1?.Dispose();
            this.endpoint1 = null;

            this.endpoint2?.Dispose();
            this.endpoint2 = null;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Method called when the command has been received and is to be executed.
        /// </summary>
        /// <param name="LocalEndpoint">Cluster endpoint that received the message.</param>
        /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
        /// <returns>Result of the command.</returns>
        public Task <object> Execute(ClusterEndpoint LocalEndpoint, IPEndPoint RemoteEndpoint)
        {
            List <string> Result = new List <string>();

            foreach (Assembly A in Types.Assemblies)
            {
                Result.Add(A.FullName);
            }

            return(Task.FromResult <object>(Result.ToArray()));
        }
Exemplo n.º 4
0
        public void TestInitialize()
        {
            this.endpoint1 = new ClusterEndpoint(clusterAddress, 12345, "UnitTest",
                                                 new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.NewLine));
            this.endpoint1.GetStatus += (sender, e) => e.Status = 1;

            foreach (IPEndPoint Endpoint in this.endpoint1.Endpoints)
            {
                this.endpoint2            = new ClusterEndpoint(Endpoint.Address, Endpoint.Port, "UnitTest");
                this.endpoint2.GetStatus += (sender, e) => e.Status = 2;
                this.endpoint2.AddRemoteStatus(Endpoint, null);
                break;
            }
        }
Exemplo n.º 5
0
        internal ClusterUdpClient(ClusterEndpoint Endpoint, UdpClient Client, IPAddress LocalAddress)
        {
            this.endpoint     = Endpoint;
            this.client       = Client;
            this.localAddress = LocalAddress;

            if (LocalAddress is null)
            {
                this.hmac = null;
            }
            else
            {
                byte[] A = LocalAddress.GetAddressBytes();
                Array.Copy(A, 0, this.ivTx, 8, 4);

                this.hmac = new HMACSHA1(A);
            }
        }
Exemplo n.º 6
0
        private static Role GetRole(Worksheet workSheet, int rrow)
        {
            Role role = new Role();

            role.Name = GetCellValue(workSheet, rrow, 2);
            string aliasesString = GetCellValue(workSheet, rrow, 3);
            var    aliases       = string.IsNullOrEmpty(aliasesString) ? new string[] { } : aliasesString.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string a in aliases)
            {
                role.AddAlias(a);
            }
            role.Tag = GetCellValue(workSheet, rrow, 6);
            string accountsString = GetCellValue(workSheet, rrow, 7);
            var    accounts       = string.IsNullOrEmpty(accountsString) ? new string[] { } : accountsString.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            string csvTemplates = GetCellValue(workSheet, rrow, 4);
            string csvPorts     = GetCellValue(workSheet, rrow, 5);

            string[] templates = string.IsNullOrEmpty(csvTemplates) ? new string[] { } : csvTemplates.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] portLines = string.IsNullOrEmpty(csvPorts) ? new string[] { } : csvPorts.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            int numClusterEndpoints = templates.Length;

            for (int i = 0; i < numClusterEndpoints; i++)
            {
                string[] templateParts = string.IsNullOrEmpty(templates[i]) ? new string[] { } : templates[i].Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                string   type          = templateParts[0].Trim();
                string   template      = templateParts[1];
                string[] ports         = string.IsNullOrEmpty(portLines[i]) ? new string[] { } : portLines[i].Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                var      newEndpoint   = new ClusterEndpoint(template, type);
                foreach (string p in ports)
                {
                    newEndpoint.AddPort(int.Parse(p));
                }
                role.Endpoints.Add(newEndpoint);
            }
            foreach (string a in accounts)
            {
                role.AddAccount(a);
            }
            return(role);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Serializes an object.
        /// </summary>
        /// <param name="Output">Output</param>
        /// <param name="Object">Object to serialize.</param>
        public void Serialize(Serializer Output, object Object)
        {
            if (Object is null)
            {
                Output.WriteByte(0);
            }
            else
            {
                Type       T    = Object.GetType();
                ObjectInfo Info = this;

                if (T == this.Type)
                {
                    Output.WriteString(string.Empty);
                }
                else
                {
                    Output.WriteString(T.FullName);

                    IProperty P = ClusterEndpoint.GetProperty(T);
                    if (P is ObjectProperty OP)
                    {
                        Info = ClusterEndpoint.GetObjectInfo(T);
                    }
                    else
                    {
                        P.Serialize(Output, Object);
                        return;
                    }
                }

                foreach (PropertyReference Property in Info.Properties)
                {
                    Output.WriteString(Property.Name);
                    object Value = Property.Info.GetValue(Object);
                    Property.Property.Serialize(Output, Value);
                }

                Output.WriteByte(0);
            }
        }
Exemplo n.º 8
0
 public void TestCleanup()
 {
     this.endpoint?.Dispose();
     this.endpoint = null;
 }
Exemplo n.º 9
0
 public void TestInitialize()
 {
     this.endpoint = new ClusterEndpoint(EndpointTests.clusterAddress, 12345, "UnitTest",
                                         new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.NewLine));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Method called when the message has been received.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     Endpoint.Released(this.Resource);
     return(Task.FromResult <bool>(true));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Method called when the message has been received.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     Endpoint.StatusReported(this.Status, RemoteEndpoint);
     return(Task.FromResult <bool>(true));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Method called when the message has been received.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     Endpoint.AssuredTransport(this.MessageID, this.Message);
     return(Task.FromResult <bool>(true));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Message sent when an endpoint is shut down, to let the other endpoints know
 /// the current endpoint is no longer available.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     Endpoint.EndpointShutDown(RemoteEndpoint);
     return(Task.FromResult <bool>(true));
 }
Exemplo n.º 14
0
 public Task <object> Execute(ClusterEndpoint LocalEndpoint, IPEndPoint RemoteEndpoint)
 {
     return(Task.FromResult <object>(this.A + this.B));
 }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            try
            {
                Types.Initialize(typeof(ClusterEndpoint).Assembly, typeof(Program).Assembly);

                Console.WriteLine("Welcome to the cluster sandbox.");
                Console.WriteLine();
                Console.Write("Multicast address (default=239.255.0.0): ");
                string s = Console.In.ReadLine();
                if (string.IsNullOrEmpty(s))
                {
                    s = "239.255.0.0";
                }

                IPAddress Address = IPAddress.Parse(s);

                Console.Write("Port (default=12345): ");
                s = Console.In.ReadLine();
                if (string.IsNullOrEmpty(s))
                {
                    s = "12345";
                }

                int Port = int.Parse(s);

                Console.Write("Secret: ");
                s = Console.In.ReadLine();

                Dictionary <string, bool>       Locked = new Dictionary <string, bool>();
                Dictionary <IPEndPoint, string> Names  = new Dictionary <IPEndPoint, string>();
                string Name = Dns.GetHostName();

                using (ClusterEndpoint Endpoint = new ClusterEndpoint(Address, Port, s))
                {
                    Console.Out.WriteLine();
                    Console.Out.WriteLine("You are now connected to the cluster.");
                    Console.Out.WriteLine("Pressing a letter or a digit will lock/release a corresponding resource.");
                    Console.Out.WriteLine("Press ESC to quit the application.");

                    Endpoint.GetStatus += (sender, e) =>
                    {
                        e.Status = Name;
                    };

                    Endpoint.EndpointOnline += (sender, e) =>
                    {
                        lock (Names)
                        {
                            Names[e.Endpoint] = string.Empty;
                        }

                        Console.Out.WriteLine("New endpoint online: " + e.Endpoint.ToString());
                    };

                    Endpoint.EndpointOffline += (sender, e) =>
                    {
                        string RemoteName;

                        lock (Names)
                        {
                            if (!Names.TryGetValue(e.Endpoint, out RemoteName))
                            {
                                RemoteName = string.Empty;
                            }
                        }

                        Console.Out.WriteLine("Endpoint offline: " + e.Endpoint.ToString() + " (" + RemoteName + ")");
                    };

                    Endpoint.EndpointStatus += (sender, e) =>
                    {
                        if (e.Status is string RemoteName)
                        {
                            bool New;

                            lock (Names)
                            {
                                New = !Names.TryGetValue(e.Endpoint, out string s2) || RemoteName != s2;

                                if (New)
                                {
                                    Names[e.Endpoint] = RemoteName;
                                }
                            }

                            if (New)
                            {
                                Console.Out.WriteLine(e.Endpoint.ToString() + " is " + RemoteName);
                            }
                        }
                    };

                    ConsoleKeyInfo KeyInfo = Console.ReadKey(true);
                    while (KeyInfo.Key != ConsoleKey.Escape)
                    {
                        char ch = KeyInfo.KeyChar;
                        if (char.IsLetterOrDigit(ch))
                        {
                            s = new string(ch, 1);

                            if (Locked.ContainsKey(s))
                            {
                                Console.Out.WriteLine("Releasing " + s + ".");
                                Endpoint.Release(s);
                                Console.Out.WriteLine(s + " released.");
                                Locked.Remove(s);
                            }
                            else
                            {
                                Console.Out.WriteLine("Locking " + s + ".");

                                Endpoint.Lock(s, 10000, (sender, e) =>
                                {
                                    if (e.LockSuccessful)
                                    {
                                        Console.Out.WriteLine(e.State.ToString() + " locked.");
                                        Locked[e.State.ToString()] = true;
                                    }
                                    else if (e.LockedBy is null)
                                    {
                                        Console.Out.WriteLine(e.State.ToString() + " is already locked.");
                                    }
                                    else
                                    {
                                        Console.Out.WriteLine(e.State.ToString() + " is already locked by " + e.LockedBy.ToString());
                                    }
                                }, s);
                            }
                        }

                        KeyInfo = Console.ReadKey(true);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
            }
        }
Exemplo n.º 16
0
 public Task <object> Execute(ClusterEndpoint LocalEndpoint, IPEndPoint RemoteEndpoint)
 {
     throw new ArgumentException((this.A + this.B).ToString());
 }
Exemplo n.º 17
0
        /// <summary>
        /// Deserializes an object
        /// </summary>
        /// <param name="Input">Binary input</param>
        /// <param name="ExpectedType">Expected type of response (or derivative).</param>
        /// <returns>Deserialized object.</returns>
        /// <exception cref="KeyNotFoundException">If the corresponding type, or any of the embedded properties, could not be found.</exception>
        public object Deserialize(Deserializer Input, Type ExpectedType)
        {
            string     TypeName = Input.ReadString();
            ObjectInfo Info;

            if (TypeName is null)
            {
                return(null);
            }
            else if (string.IsNullOrEmpty(TypeName))
            {
                Info = this;
            }
            else
            {
                Type T = Types.GetType(TypeName);
                if (T is null)
                {
                    throw new KeyNotFoundException("Type name not recognized: " + TypeName);
                }

                IProperty P = ClusterEndpoint.GetProperty(T);
                if (P is ObjectProperty OP)
                {
                    Info = ClusterEndpoint.GetObjectInfo(T);
                }
                else
                {
                    return(P.Deserialize(Input, T));
                }
            }

            if (Info.sorted is null)
            {
                Dictionary <string, PropertyReference> Sorted = new Dictionary <string, PropertyReference>();

                foreach (PropertyReference P in Info.Properties)
                {
                    Sorted[P.Name] = P;
                }

                Info.sorted = Sorted;
            }

            object Result       = Activator.CreateInstance(Info.Type);
            string PropertyName = Input.ReadString();

            while (!string.IsNullOrEmpty(PropertyName))
            {
                if (!Info.sorted.TryGetValue(PropertyName, out PropertyReference P))
                {
                    throw new KeyNotFoundException("Property Name not found: " + Info.Type.FullName + "." + PropertyName);
                }

                P.Info.SetValue(Result, P.Property.Deserialize(Input, P.Property.PropertyType));

                PropertyName = Input.ReadString();
            }

            return(Result);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Method called when the message has been received.
 /// </summary>
 /// <param name="Endpoint">Cluster endpoint that received the message.</param>
 /// <param name="RemoteEndpoint">Endpoint sending the message.</param>
 /// <returns>If the message was accepted/processed or not.
 /// In Acknowledged service, this corresponds to ACK/NACK.</returns>
 public Task <bool> MessageReceived(ClusterEndpoint Endpoint, IPEndPoint RemoteEndpoint)
 {
     return(Endpoint.AssuredDelivery(this.MessageID, Endpoint, RemoteEndpoint));
 }