예제 #1
0
 /// <summary>
 /// Use the service access base to call an arbitrary service
 /// </summary>
 /// <param name="address"></param>
 /// <param name="parameters"></param>
 /// <returns></returns>
 public Dictionary <string, string> Consume(MIPAddress address, Dictionary <string, string> parameters)
 {
     //Create a base service client
     using (MMIServiceBaseClient client = new MMIServiceBaseClient(address.Address, address.Port))
     {
         return(client.Access.Consume(parameters));
     }
 }
예제 #2
0
 /// <summary>
 /// Basic constructor
 /// </summary>
 /// <param name="description">The service description</param>
 /// <param name="mmiRegisterAddress">The address of the mmi register</param>
 /// <param name="processor">The assigned processor of the service controller</param>
 public ServiceController(MServiceDescription description, MIPAddress mmiRegisterAddress, TProcessor processor)
 {
     //Assign the adapter description
     this.serviceDescription = description;
     //Assign the addresses
     this.address            = description.Addresses[0];
     this.mmiRegisterAddress = mmiRegisterAddress;
     //Assign the processor
     this.processor = processor;
 }
예제 #3
0
        /// <summary>
        /// Constructor to create a new server
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <param name="implementation"></param>
        public RemoteSceneAccessServer(MIPAddress address, MIPAddress registerAddress, MSceneAccess.Iface implementation)
        {
            //Add the address to the description
            this.description.Addresses = new List <MIPAddress>()
            {
                address
            };

            //Create a new controller
            this.controller = new ServiceController(description, registerAddress, new MSceneAccess.Processor(implementation));
        }
예제 #4
0
        /// <summary>
        /// Connects to the given adapters
        /// </summary>
        /// <param name="adapterAddresses"></param>
        /// <param name="timeout">The timout in milliseconds</param>
        public async Task <bool> ConnectAsync(MIPAddress mmiRegisterAddress, TimeSpan timeout, Action <bool> callback, string AvatarID)
        {
            return(await Task.Factory.StartNew(() =>
            {
                bool success = this.Connect(mmiRegisterAddress, timeout, AvatarID);

                callback?.Invoke(success);

                return success;
            }));;
        }
예제 #5
0
        /// <summary>
        /// Basic constructor
        /// </summary>
        /// <param name="registerAddress"></param>
        /// <param name="description"></param>
        public AdapterRegistrationHandler(MIPAddress registerAddress, MAdapterDescription description, bool autoStart = true)
        {
            this.address     = registerAddress;
            this.description = description;
            this.thread      = new Thread(new ThreadStart(this.HandleRegistration));
            this.cts         = new CancellationTokenSource();

            if (autoStart)
            {
                this.Start();
            }
        }
예제 #6
0
        /// <summary>
        /// Basic constructor
        /// </summary>
        /// <param name="webserverAddress"></param>
        /// <param name="filepath"></param>
        public ExecutableController(MExecutableDescription description, MIPAddress address, MIPAddress registerAddress, string mmuPath, string filepath, bool hideWindow)
        {
            this.Name            = description.Name;
            this.Description     = description;
            this.Address         = address.Address;
            this.Port            = address.Port;
            this.registerAddress = registerAddress;

            this.Filepath   = filepath;
            this.mmuPath    = mmuPath;
            this.HideWindow = hideWindow;
        }
예제 #7
0
        /// <summary>
        /// Unregisters at a specific event given the event type (e.g. simulation end).
        /// The given clientAddress is used to provide an event based communication.
        /// </summary>
        /// <param name="clientAddress"></param>
        /// <param name="eventType"></param>
        /// <returns></returns>
        public MBoolResponse RegisterAtEvent(MIPAddress clientAddress, string eventType)
        {
            this.registrationMutex.WaitOne();

            if (!this.callbackClients.ContainsKey(eventType))
            {
                this.callbackClients.Add(eventType, new List <CoSimulationEventCallbackClient>());
            }

            this.callbackClients[eventType].Add(new CoSimulationEventCallbackClient(clientAddress));

            this.registrationMutex.ReleaseMutex();


            return(new MBoolResponse(true));
        }
예제 #8
0
        /// <summary>
        /// Basic constructor
        /// </summary>
        /// <param name="coSimulator">The instance of the co-simulator</param>
        /// <param name="address">The address where the CoSimulationAccess should be hosted</param>
        /// <param name="registerAddress">The address of the register</param>
        public CoSimulationAccess(MMICoSimulator coSimulator, MIPAddress address, MIPAddress registerAddress)
        {
            //Assign the co simulator
            this.coSimulator = coSimulator;

            //Register at on result event
            this.coSimulator.OnResult += CoSimulator_OnResult;

            //Add the address to the description
            this.description.Addresses = new List <MIPAddress>()
            {
                address
            };

            //Create a new service controller
            this.controller = new ServiceController(this.description, registerAddress, new MCoSimulationAccess.Processor(this));
        }
예제 #9
0
        /// <summary>
        /// Unregisters at a specific event given the event type (e.g. simulation end)
        /// </summary>
        /// <param name="clientAddress"></param>
        /// <param name="eventType"></param>
        /// <returns></returns>
        public MBoolResponse UnregisterAtEvent(MIPAddress clientAddress, string eventType)
        {
            if (this.callbackClients.ContainsKey(eventType))
            {
                //Remove the client with the respective address
                for (int i = this.callbackClients[eventType].Count - 1; i >= 0; i--)
                {
                    var client = this.callbackClients[eventType][i];

                    if (client.Address.Address == clientAddress.Address && client.Address.Port == clientAddress.Port)
                    {
                        this.registrationMutex.WaitOne();

                        client.Dispose();
                        this.callbackClients[eventType].RemoveAt(i);

                        this.registrationMutex.ReleaseMutex();
                    }
                }
            }

            return(new MBoolResponse(true));
        }
예제 #10
0
        /// <summary>
        /// Basic constructor
        /// </summary>
        /// <param name="description">The adapter description. The first address in  the Addressed field is used for hosting the server.</param>
        /// <param name="mmiRegisterAddress">The address of the register (where all services, adapters and mmus are registered)</param>
        /// <param name="mmuPath">The path where the MMUs are located</param>
        /// <param name="mmuProvider">A class which provides information regarding the available MMUs</param>
        /// <param name="mmuInstantiatior">A class which can instantiate MMUs based on the file and description</param>
        /// <param name="customAdapterImplementation">An optionally specified customAdapterImplementation which is utilized instead of the default one</param>
        public AdapterController(SessionData sessionData, MAdapterDescription description, MIPAddress mmiRegisterAddress, IMMUProvider mmuProvider, IMMUInstantiation mmuInstantiatior, MMIAdapter.Iface customAdapterImplementation = null)
        {
            //Assign the session data
            this.SessionData = sessionData;

            //Is the default implementation if not explicitly set
            if (customAdapterImplementation == null)
            {
                this.adapterImplementation = new ThriftAdapterImplementation(sessionData, mmuInstantiatior);
            }
            else
            {
                this.adapterImplementation = customAdapterImplementation;
            }

            //Assign the adapter implementation instance
            SessionData.AdapterInstance = this.adapterImplementation;

            //Assign the mmu instantiator
            this.mmuInstantiator = mmuInstantiatior;
            this.mmuProvider     = mmuProvider;

            //Assign the adapter description
            this.adapterDescription        = description;
            SessionData.AdapterDescription = description;

            //Assign the addresses
            this.address            = description.Addresses[0];
            this.mmiRegisterAddress = mmiRegisterAddress;

            //Assign the MMI register address
            SessionData.MMIRegisterAddress = mmiRegisterAddress;

            //Register on changed event
            this.mmuProvider.MMUsChanged += MmuProvider_MMUsChanged;
        }
예제 #11
0
 /// <summary>
 /// Basic constructor
 /// </summary>
 /// <param name="address"></param>
 /// <param name="port"></param>
 public CoSimulationEventCallbackClient(MIPAddress address, bool autoStart = true) : base(address.Address, address.Port, autoStart)
 {
     this.Address = address;
 }
예제 #12
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);
        }
예제 #13
0
 /// <summary>
 /// Creates a new service access with a given root address.
 /// The root address is used to get the information about all available services and accessing them
 /// </summary>
 /// <param name="address"></param>
 public ServiceAccess(MIPAddress registerAddress, string sessionID)
 {
     this.registerAddress = registerAddress;
     this.sessionID       = sessionID;
 }