Пример #1
0
        //
        //
        //
        //
        #endregion//Constructors


        #region no Properties
        // *****************************************************************
        // ****                     Properties                          ****
        // *****************************************************************
        //
        //
        #endregion//Properties


        #region Public Methods
        // *****************************************************************
        // ****                     Public Methods                      ****
        // *****************************************************************
        //
        //
        // *************************************************
        // ****             TryAddService()             ****
        // *************************************************
        /// <summary>
        /// This ForeignServer will try to register this ForeignSerivce with AppServices.
        /// It will try to create a new LocalServiceName = "Remote Service Name" + "@" + "ServerId"
        /// so that it will be unique in the *local* application service list.
        /// </summary>
        /// <param name="foreignService"></param>
        /// <returns></returns>
        public bool TryAddService(ForeignService foreignService)
        {
            bool isSuccess = true;

            // Store service inside this server.
            foreignService.Parent = this;            // Connect events to the service before registering it.
            if (this.Services.ContainsKey(foreignService.RemoteServiceName))
            {
                return(false);
            }
            else
            {
                this.Services.Add(foreignService.RemoteServiceName, foreignService);
            }

            // Create unique name and register with AppServices.
            AppServices app = AppServices.GetInstance();

            /*
             * foreignService.ServiceName = string.Format("{0}@{1}",foreignService.RemoteServiceName,this.ServerId);   // Usually this is enough for uniqueness.
             * if (! app.TryAddService(foreignService) )
             * {   // This should never happen since the remote name is unique on the foreign server,
             *  // and my server id is unique.
             *  int n = (int)'A';
             *  foreignService.ServiceName = string.Format("{0}@{1}{2}", foreignService.RemoteServiceName, this.ServerId, Convert.ToChar(n));
             *  while ( ! app.TryAddService(foreignService) && n < 123)
             *      foreignService.ServiceName = string.Format("{0}@{1}{2}", foreignService.RemoteServiceName, this.ServerId, Convert.ToChar(++n));
             *  if (n >= 123)
             *      isSuccess = false;
             * }
             */
            // new approach is to assume unique names are employed.
            foreignService.ServiceName = foreignService.RemoteServiceName;
            if (!app.TryAddService(foreignService))
            {
                isSuccess = false;
            }
            return(isSuccess);
        }// TryAddService()
Пример #2
0
        //
        // *****************************************
        // ****         Send Services()         ****
        // *****************************************
        /// <summary>
        /// Sends Messages specific to the addition/removal of
        /// local services.  These messages are broadcast to all
        /// currently connected servers.
        /// TODO: Perhaps we need only broadcast to those connections
        /// that initiated the connection?
        /// </summary>
        /// <param name="conversationId"></param>
        /// <param name="msgType"></param>
        /// <param name="iServiceList"></param>
        private void SendServices(int conversationId, MessageType msgType, List <IService> iServiceList)
        {
            Message msg = GetMessage(MessageType.ServiceAdded, MessageState.Confirmed);

            foreach (IService iService in iServiceList)
            {
                if (iService is ServiceManager)
                {
                    continue;                           // skip services we don't want to list on remote server.
                }
                else if (iService is IEngineHub)
                {   // EngineHubs are special kinds of Services.
                    ForeignEngineHub service = new ForeignEngineHub(iService);
                    msg.Data.Add(service);
                }
                else
                {   // All other services.
                    ForeignService service = new ForeignService(iService);
                    msg.Data.Add(service);
                }
            }
            SendMessage(conversationId, msg);
        }// SendServices()
Пример #3
0
        }     // ProcessRequestMessage()

        //
        //
        //
        //
        // *************************************************************
        // ****             ProcessConfirmMessage()                 ****
        // *************************************************************
        private void ProcessMessageResponses(SocketEventArgs eventArg, Message msg)
        {
            // Get server responding
            ForeignServer foreignServer = null;

            if (!m_ServersConnected.TryGetValue(eventArg.ConversationId, out foreignServer))
            {
                Log.NewEntry(LogLevel.Warning, "ProcessConfirmMessage: Unknown server {0} with message: {1}", eventArg, msg);
                return;
            }

            switch (msg.MessageType)
            {
            case MessageType.Credentials:

                if (msg.State == MessageState.Confirmed)
                {       // Our credentials were accepted.
                    Log.NewEntry(LogLevel.Minor, "ProcessConfirmMessage: Credentials accepted by {0}", foreignServer);
                }
                else
                {
                    Log.NewEntry(LogLevel.Minor, "ProcessConfirmMessage: Credentials rejected by {0}. Disconnecting.", foreignServer);
                    m_Socket.StopConversation(foreignServer.ConversationId);
                    ForeignServerDisconnect(foreignServer);
                }
                break;

            case MessageType.CreateServices:

                break;

            case MessageType.ServiceAdded:
                // ForeignServer has added a new service.
                // Here, I want to make a ghost-service that represents each service provided by the foreign server.
                Log.BeginEntry(LogLevel.Minor, "ProcessConfirmMessage: Services discovered on {0}:", foreignServer);
                foreach (IStringifiable iStr in msg.Data)
                {
                    if (iStr is ForeignService)
                    {
                        ForeignService foreignService = (ForeignService)iStr;
                        if (foreignServer.TryAddService(foreignService))
                        {
                            Log.AppendEntry(" {0}", foreignService);
                        }
                        else
                        {
                            Log.AppendEntry(" {0}[Failed to add]", foreignService);
                        }
                    }
                }
                Log.EndEntry();


                break;

            case MessageType.ServiceRemoved:
                foreach (IStringifiable istring in msg.Data)
                {
                }
                break;

            case MessageType.EngineEvent:
                // Engine events from foreign IEngineHubs.
                foreach (IStringifiable iString in msg.Data)
                {
                    if (iString is EngineEventArgs)
                    {
                        EngineEventArgs e = (EngineEventArgs)iString;
                        if (e.Status == EngineEventArgs.EventStatus.Request)
                        {       // Send this to a single hub
                            IService iService = null;
                            if (m_AppServices.TryGetService(e.EngineHubName, out iService) && iService is IEngineHub)
                            {
                                ((IEngineHub)iService).HubEventEnqueue(e);
                            }
                        }
                        else
                        {       // Broadcast to all subscribed to this hub.
                            ForeignService fs;
                            if (foreignServer.TryGetService(e.EngineHubName, out fs) && (fs is IEngineHub))
                            {
                                e.EngineHubName = fs.ServiceName;
                                ((IEngineHub)fs).OnEngineChanged(e);
                            }
                        }
                    }
                }
                break;
            } //switch
        }     //ProcessConfirmMessage()
Пример #4
0
        }// TrySendMessage()

        //
        public bool TryGetService(string serviceName, out ForeignService iService)
        {
            return(Services.TryGetValue(serviceName, out iService));
        }