예제 #1
0
        public void Initialize()
        {
            Console.WriteLine("Trying to fetch the services from: " + this.registerAddress.Address + " " + this.registerAddress.Port);

            try
            {
                //Create a new client to connect to the specific register server
                using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(this.registerAddress.Address, this.registerAddress.Port))
                {
                    foreach (MServiceDescription serviceDescription in client.Access.GetRegisteredServices(this.sessionID))
                    {
                        if (!this.serviceDescriptions.ContainsKey(serviceDescription.Name))
                        {
                            this.serviceDescriptions.Add(serviceDescription.Name, serviceDescription);
                        }

                        Console.WriteLine(serviceDescription.Name + " " + serviceDescription.Addresses[0].Address + " " + serviceDescription.Addresses[0].Port);
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception("Problem at fetching available services from the register. Wrong address? Server down?");
            }
        }
예제 #2
0
        /// <summary>
        /// Disposes the handler
        /// </summary>
        public void Dispose()
        {
            this.cts.Cancel();

            try
            {
                //Try to register
                using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(this.address.Address, this.address.Port))
                {
                    client.Access.UnregisterAdapter(description);
                }
            }
            catch (Exception)
            {
            }

            this.cts.Dispose();
        }
        /// <summary>
        /// Checks whether the registration is already available at the register
        /// </summary>
        /// <param name="serviceDescription"></param>
        /// <returns></returns>
        private bool RegistrationAvailable(MServiceDescription serviceDescription)
        {
            try
            {
                using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(this.address.Address, this.address.Port))
                {
                    List <MServiceDescription> serviceDescriptions = client.Access.GetRegisteredServices("");

                    if (serviceDescriptions.Exists(s => s.ID == serviceDescription.ID && s.Addresses.Exists(x => x.Address == serviceDescription.Addresses[0].Address && x.Port == serviceDescription.Addresses[0].Port)))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Method to actually handle the registration
        /// </summary>
        private void HandleRegistration()
        {
            while (!this.cts.IsCancellationRequested)
            {
                //Check if registration is already available
                if (!RegistrationAvailable(description))
                {
                    try
                    {
                        //Try to register
                        using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(this.address.Address, this.address.Port))
                        {
                            client.Access.RegisterAdapter(description);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }

                Thread.Sleep(this.UpdateTime);
            }
        }
예제 #5
0
        /// <summary>
        /// Connects to the given adapters
        /// </summary>
        /// <param name="adapterAddresses"></param>
        /// <param name="timeout">The timout in milliseconds</param>
        public bool Connect(MIPAddress mmiRegisterAddress, TimeSpan timeout, string AvatarID)
        {
            //Create a list representing the adapter descriptions
            List <MAdapterDescription> adapterDescriptions = new List <MAdapterDescription>();

            //Create a concurrent dictionary for storing the createad MMUAccesses
            ConcurrentDictionary <IAdapter, List <MotionModelUnitAccess> > adapterMMUAccesses = new ConcurrentDictionary <IAdapter, List <MotionModelUnitAccess> >();

            //Get all registered adapters -> Execute this task in background with the imposed timeout
            bool adapterDescriptionReceived = Threading.ExecuteTask((CancellationTokenSource cls) =>
            {
                //The actual function which is executed in the background
                while (!cls.IsCancellationRequested)
                {
                    try
                    {
                        //Get all adapter descriptions and create a client for this purpose
                        using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(mmiRegisterAddress.Address, mmiRegisterAddress.Port))
                        {
                            adapterDescriptions = client.Access.GetRegisteredAdapters(SessionId);
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(100);
                    }
                }
            }, timeout);

            //Directly return if no adapter description was received
            if (!adapterDescriptionReceived)
            {
                return(false);
            }

            //Create the service access
            this.ServiceAccess = new ServiceAccess(mmiRegisterAddress, SessionId);

            //Fetch all adapter descriptions and create a new adapter instance
            foreach (MAdapterDescription description in adapterDescriptions)
            {
                //Skip if adapter is already available
                if (this.Adapters.Exists(s => s.Description.Name == description.Name && description.Addresses[0] == description.Addresses[0] && s.Description.Addresses[0].Port == description.Addresses[0].Port))
                {
                    continue;
                }

                //Add a new remote adapter instance to the list
                this.Adapters.Add(new RemoteAdapterAccess(description.Addresses[0].Address, description.Addresses[0].Port, description, this));
            }


            ///Dictionary which contains the connected adapters
            ConcurrentDictionary <IAdapter, bool> connectedAdapters = new ConcurrentDictionary <IAdapter, bool>();


            //Create the sessions for each adapter in parallel
            bool success = Threading.ExecuteTasksParallel(this.Adapters, (IAdapter adapter, CancellationTokenSource cls) =>
            {
                //Start the adapter
                adapter.Start();

                //Create the session
                adapter.CreateSession(this.SessionId, this.SkeletonAccess.GetAvatarDescription(AvatarID));

                //Add flag if finished
                connectedAdapters.TryAdd(adapter, true);
            }, timeout);

            //Remove all adapters not being connected (therfore iterate over all added adapters)
            for (int i = this.Adapters.Count - 1; i >= 0; i--)
            {
                //Remove the adapter if not connected
                if (!connectedAdapters.ContainsKey(this.Adapters[i]))
                {
                    //Close the connection
                    this.Adapters[i].CloseConnection();
                    this.Adapters.Remove(this.Adapters[i]);
                }
            }

            //Return true if at least one adapter is connected
            return(this.Adapters.Count > 0);
        }
예제 #6
0
        /// <summary>
        /// Connects to the given adapters
        /// </summary>
        /// <param name="adapterEndpoint"></param>
        /// <param name="allowRemoteConnections"></param>
        public bool Connect(AdapterEndpoint adapterEndpoint, string AvatarID, bool allowRemoteConnections = true)
        {
            //Create a list for the adapter descriptions
            List <MAdapterDescription> adapterDescriptions = new List <MAdapterDescription>();

            //Add the local adapter
            this.Adapters.Add(new LocalAdapterAccess(adapterEndpoint.Description, adapterEndpoint.Instance, this));


            //If remote connections are allowed
            if (allowRemoteConnections)
            {
                try
                {
                    //Get the adapter names
                    using (MMIRegisterServiceClient client = new MMIRegisterServiceClient(adapterEndpoint.MMIRegisterAddress.Address, adapterEndpoint.MMIRegisterAddress.Port))
                    {
                        client.Start();

                        //To do
                        adapterDescriptions = client.Access.GetRegisteredAdapters(SessionId);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot connect to mmi register " + e.Message);
                    return(false);
                }

                foreach (MAdapterDescription description in adapterDescriptions)
                {
                    Console.WriteLine(description.Name);

                    //Skip if adapter is already available
                    if (this.Adapters.Exists(s => s.Description.Name == description.Name && description.Addresses[0] == description.Addresses[0] && s.Description.Addresses[0].Port == description.Addresses[0].Port))
                    {
                        continue;
                    }

                    Console.WriteLine("Add new remote adapter: " + description.Name);

                    //Add the adapter
                    this.Adapters.Add(new RemoteAdapterAccess(description.Addresses[0].Address, description.Addresses[0].Port, description, this));
                }
            }


            //Create the service access
            this.ServiceAccess = new ServiceAccess(adapterEndpoint.MMIRegisterAddress, SessionId);


            //Start all adapters
            foreach (IAdapter adapter in this.Adapters)
            {
                //Start the adapter
                adapter.Start();

                //Create the session
                adapter.CreateSession(this.SessionId, this.SkeletonAccess.GetAvatarDescription(AvatarID));
            }

            return(true);
        }