internal void AddPublicationConnectionHandler(DPE_PublicationConnectionHandler publicationConnectionHandler)
 {
     lock (this._tableOfPublicationConnectionHandlers)
     {
         if (!this._tableOfPublicationConnectionHandlers.ContainsKey(publicationConnectionHandler.PublicationName))
         {
             this._tableOfPublicationConnectionHandlers.Add(publicationConnectionHandler.PublicationName, publicationConnectionHandler);
         }
         else
         {
             throw (new Exception("The connection handler for publication \'" + publicationConnectionHandler.PublicationName + "\' is already added."));
         }
     }
 }
            internal void DisposeConnectionHandler(string publicationName)
            {
                if (this.ContainsConnectionHandler(publicationName))
                {
                    DPE_PublicationConnectionHandler publicationCnnHandler = default(DPE_PublicationConnectionHandler);
                    publicationCnnHandler = this._STXDSSPublicationConnectionHandlersContainer.GetConnectionHandler(publicationName);

                    //--------------------------------------------------------------
                    try
                    {
                        publicationCnnHandler.PublicationDataReceived      -= STXDSSPublicationConnectionHandler_PublicationDataReceived;
                        publicationCnnHandler.ConnectionToPublicationLost  -= STXDSSPublicationConnectionHandler_ConnectionLost;
                        publicationCnnHandler.PublicationDataResetReceived -= STXDSSPublicationConnectionHandler_PublicationDataResetReceived;
                    }
                    catch (Exception)
                    {
                    }

                    try
                    {
                        publicationCnnHandler.Dispose();
                    }
                    catch (Exception)
                    {
                    }

                    //stops any attempt to connect to a publication
                    if (this.ContainsScheduledPublicationConnectionTask(publicationName))
                    {
                        this.AbortScheduledConnectionToAPublication(publicationName);
                    }

                    this._STXDSSPublicationConnectionHandlersContainer.RemoveConnectionHandler(publicationName);

                    //If Me._tableOfConnectionInfoRegistersOfConnectionsToPublications.ContainsKey(publicationName) Then
                    //    Me._tableOfConnectionInfoRegistersOfConnectionsToPublications.Remove(publicationName)
                    //End If
                }
            }
 internal void DiscardAllLocalConnectionsToPublications()
 {
     lock (this._tableOfPublicationConnectionHandlers)
     {
         IEnumerator enumm = default(IEnumerator);
         DPE_PublicationConnectionHandler handler = default(DPE_PublicationConnectionHandler);
         enumm = this._tableOfPublicationConnectionHandlers.GetEnumerator();
         while (enumm.MoveNext())
         {
             handler = (DPE_PublicationConnectionHandler)((DictionaryEntry)enumm.Current).Value;
             try
             {
                 handler.DisconnectFromPublication();
                 handler.Dispose();
             }
             catch (Exception)
             {
             }
         }
         this._tableOfPublicationConnectionHandlers.Clear();
     }
 }
            private void ReconnectToDataPublication(string publicationName)
            {
                //***********************************************************************
                //creates a task to restablisht the connection to the publication
                if (this._tableOfConnectionInfoRegistersOfConnectionsToPublications.ContainsKey(publicationName))
                {
                    PublicationConnectionInfo cnnInfo = new PublicationConnectionInfo();
                    cnnInfo = (PublicationConnectionInfo)this._tableOfConnectionInfoRegistersOfConnectionsToPublications[publicationName];
                    this.ScheduleConnectionTaskToAPublication(cnnInfo.PublicationName, cnnInfo.ConnectionMode);
                }

                //***********************************************************************
                //frees the connection handler resources of previous connection to the publication
                DPE_PublicationConnectionHandler publicationCnnHandler = default(DPE_PublicationConnectionHandler);

                publicationCnnHandler = this._STXDSSPublicationConnectionHandlersContainer.GetConnectionHandler(publicationName);

                if (!(publicationCnnHandler == null))
                {
                    try
                    {
                        publicationCnnHandler.PublicationDataReceived      -= STXDSSPublicationConnectionHandler_PublicationDataReceived;
                        publicationCnnHandler.ConnectionToPublicationLost  -= STXDSSPublicationConnectionHandler_ConnectionLost;
                        publicationCnnHandler.PublicationDataResetReceived -= STXDSSPublicationConnectionHandler_PublicationDataResetReceived;
                    }
                    catch (Exception)
                    {
                    }

                    try
                    {
                        publicationCnnHandler.Dispose();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            internal void ConnectoToPublication(DPE_PublicationConnectionHandler_Type connectionHandlerType, string publicationName, DPE_ServerDefs.DPE_PublicationConnectionMode connectionMode)
            {
                //*****************************************************************************
                //request to the server the connection parameters of the publication
                P2PDataRequest dataREquest = new P2PDataRequest(DPE_ServerDefs.DPE_CMD_PUBLICATION_SUBSCRIPTION_DATA);

                dataREquest.AddRequestParameter(DPE_PublicationsDefinitions.DPE_PUBLICATION_NAME, publicationName);

                P2PData publicationPArams = null;
                string  msg = "";

                Services.P2PCommunicationsScheme.P2PPortClient publicationsInformationRetrieveP2PPortClient = null;
                int trialsCount = 0;


                publicationsInformationRetrieveP2PPortClient = new Services.P2PCommunicationsScheme.P2PPortClient(this._STXDataSocketClient.DSSServerHostName, this._STXDataSocketClient.PublicationsInformationRetrieve_PortNumber);
                publicationsInformationRetrieveP2PPortClient.Connect();


                //performs a  while cycle until the client gets the connection parameters
                while (true)
                {
                    try
                    {
                        publicationPArams = publicationsInformationRetrieveP2PPortClient.RetrieveData(dataREquest);
                        break;
                    }
                    catch (Exception ex)
                    {
                        trialsCount++;

                        if (trialsCount >= MAX_DATA_CONNECTION_PARAMETERS_INQUIRY_TRIALS)
                        {
                            msg = "Error trying to connect to publication \'" + publicationName + "\': " + ex.Message;
                            throw (new Exception(msg));
                        }
                    }

                    System.Threading.Thread.Sleep(10);
                }

                try
                {
                    publicationsInformationRetrieveP2PPortClient.Dispose();
                }
                catch (Exception)
                {
                }

                CustomHashTable paramsList          = (CustomHashTable)publicationPArams.Value;
                string          publicationHostNAme = System.Convert.ToString(paramsList.Item(DPE_PublicationsDefinitions.DPE_PUBLICATION_HOSTNAME));
                int             publicationPort     = System.Convert.ToInt32(paramsList.Item(DPE_PublicationsDefinitions.DPE_PUBLICATION_PORT));

                //*****************************************************************************
                //CREATION OF THE PUBLICATION HANDLER and CONNECTION WITH publication
                DPE_PublicationConnectionHandler publicationConnectionHandler = default(DPE_PublicationConnectionHandler);

                publicationConnectionHandler = this.CreatePublicationConnectionHandler(connectionHandlerType, this._STXDataSocketClient, publicationName, publicationHostNAme, publicationPort, connectionMode);


                //*****************************************************************************
                //Ciclic process in order to allow the server to register the client socket connection . -> to synchronize the client registration
                //by ser client with the server event to log the publication event

                msg = "Error trying to perform the client connection registration to the publication \'" + publicationName + "\' : ";

                //*****************************************************************************
                //registration of the client connection to a publication in the server
                //the client sends to the server a information telling to which publication has already connected
                //and also for the TCP client mode to tell the publication to which port send the data

                int  connectionRegistrationTrialCounter = 0;
                bool connectionRegistration             = false;

                //preparation of the connection information into a p2p data
                P2PData clientPubCnnData = new P2PData(DPE_ServerDefs.DPE_CMD_CLIENT_PUBLICATION_CONNECTION_REGISTRATION, DPE_ServerDefs.DPE_CMD_CLIENT_PUBLICATION_CONNECTION_REGISTRATION);

                clientPubCnnData.DataAttributesTable.AddAttribute(DPE_PublicationsDefinitions.DPE_CLIENT_ID, this._STXDataSocketClient.ClientID);
                clientPubCnnData.DataAttributesTable.AddAttribute(DPE_PublicationsDefinitions.DPE_PUBLICATION_NAME, publicationName);
                clientPubCnnData.DataAttributesTable.AddAttribute(DPE_PublicationsDefinitions.DPE_PUBLICATION_CONNECTION_HANDLER_ID, publicationConnectionHandler.HandlerID);
                //---------------------HANDLING FOR THE CONNECTION MODE ------------------------------
                string connectionModeAsString = "";

                connectionModeAsString = DPE_ServerDefs.Get_STXDSS_PublicationConnectionMode_ToAString(connectionMode);
                clientPubCnnData.DataAttributesTable.AddAttribute(DPE_PublicationsDefinitions.DPE_PUBLICATION_CLIENT_CONNECTION_MODE, connectionModeAsString);


                msg = "Error trying to perform the client connection registration to the publication \'" + publicationName + "\' : ";


                //sends to server the information to the server several times until the information reaches the server
                Services.P2PCommunicationsScheme.P2PPortClient publicationsClientRegistrationP2PPortClient = default(Services.P2PCommunicationsScheme.P2PPortClient);

                publicationsClientRegistrationP2PPortClient = new Services.P2PCommunicationsScheme.P2PPortClient(this._STXDataSocketClient.DSSServerHostName, this._STXDataSocketClient.PublicationsClientRegistration_PortNumber);
                publicationsClientRegistrationP2PPortClient.Connect();

                while (connectionRegistration == false)
                {
                    try
                    {
                        publicationsClientRegistrationP2PPortClient.SendData(P2PDataSendMode.SyncrhonicalSend, clientPubCnnData);

                        connectionRegistration = true;

                        publicationsClientRegistrationP2PPortClient.Dispose();

                        CustomEventLog.WriteEntry(EventLogEntryType.SuccessAudit, "Connection to publication \'" + publicationName + "\' succesfull");

                        break;
                    }
                    catch (Exception ex)
                    {
                        System.Threading.Thread.Sleep(10);

                        connectionRegistrationTrialCounter++;

                        if (connectionRegistrationTrialCounter >= MAX_CONNECTION_REGISTRATION_TRIALS)
                        {
                            msg = msg + ex.Message;

                            try
                            {
                                publicationsClientRegistrationP2PPortClient.Dispose();
                            }
                            catch (Exception)
                            {
                            }

                            try
                            {
                                this.DisposeConnectionHandler(publicationName);
                            }
                            catch (Exception)
                            {
                            }

                            throw (new Exception(msg));
                        }
                    }
                }
            }
            internal DPE_PublicationConnectionHandler CreatePublicationConnectionHandler(DPE_PublicationConnectionHandler_Type handlerType, DPE_DataPublicationsClient client, string PublicationName, string PublicationHostName, int publicationsSocketsServerPortNumber, DPE_ServerDefs.DPE_PublicationConnectionMode connectionMode)
            {
                //***********************************************************************************************
                //creates the handler according to the type of connection
                DPE_PublicationConnectionHandler publicationCnnHandler = null;

                publicationCnnHandler = new DPE_PublicationConnectionHandler(handlerType, client, PublicationName, PublicationHostName, publicationsSocketsServerPortNumber, connectionMode);

                if (!(publicationCnnHandler == null))
                {
                    try
                    {
                        //evaluates if the client is not the publisher of the publication, in order to avoid thepublisher client to
                        //receive its own publication update
                        if (!this._STXDataSocketClient.PublicationsPostManager.ContainsPublicationDefinition(PublicationName))
                        {
                            if (publicationCnnHandler.HandlerType == DPE_PublicationConnectionHandler_Type.subscriptorHandler)
                            {
                                publicationCnnHandler.PublicationDataReceived      += STXDSSPublicationConnectionHandler_PublicationDataReceived;
                                publicationCnnHandler.PublicationDataResetReceived += STXDSSPublicationConnectionHandler_PublicationDataResetReceived;
                            }
                        }
                        //for both type of handlers is created a handler to the connectio lost event in order to know when a connection to a publication
                        //is lost
                        publicationCnnHandler.ConnectionToPublicationLost += STXDSSPublicationConnectionHandler_ConnectionLost;
                    }
                    catch (Exception)
                    {
                        publicationCnnHandler.PublicationDataReceived      -= STXDSSPublicationConnectionHandler_PublicationDataReceived;
                        publicationCnnHandler.ConnectionToPublicationLost  -= STXDSSPublicationConnectionHandler_ConnectionLost;
                        publicationCnnHandler.PublicationDataResetReceived -= STXDSSPublicationConnectionHandler_PublicationDataResetReceived;
                    }

                    try
                    {
                        this._STXDSSPublicationConnectionHandlersContainer.AddPublicationConnectionHandler(publicationCnnHandler);
                    }
                    catch (Exception ex)
                    {
                        CustomEventLog.WriteEntry(EventLogEntryType.Error, ex.ToString());
                    }

                    if (handlerType == DPE_PublicationConnectionHandler_Type.subscriptorHandler)
                    {
                        //---------------------------------------
                        //saves into a has table the publication name and its connection mode for further reference when is needed to perfofm
                        //automatic reconnections just for connections handlers defined as subscriptors handler
                        PublicationConnectionInfo cnnInfo = new PublicationConnectionInfo();
                        cnnInfo.connectionType  = handlerType;
                        cnnInfo.PublicationName = PublicationName;
                        cnnInfo.ConnectionMode  = connectionMode;

                        try
                        {
                            //saves the connection information for the subscriptors connection type handlers
                            if (this._tableOfConnectionInfoRegistersOfConnectionsToPublications.ContainsKey(PublicationName))
                            {
                                this._tableOfConnectionInfoRegistersOfConnectionsToPublications.Remove(PublicationName);
                            }
                            this._tableOfConnectionInfoRegistersOfConnectionsToPublications.Add(PublicationName, cnnInfo);
                        }
                        catch (Exception ex)
                        {
                            CustomEventLog.WriteEntry(EventLogEntryType.Error, ex.ToString());
                        }
                    }
                }

                //---------------------------------------
                return(publicationCnnHandler);
            }