Example #1
0
            private bool disposedValue = false;             // To detect redundant calls

            // IDisposable
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: free other state (managed objects).

                        try
                        {
                            this._publicationSocketsServer.Dispose();
                        }
                        catch (Exception)
                        {
                        }

                        try
                        {
                            DisposePublicationDataTable(this._publicationsDataBaseConnectionString, this.PublicationName);
                        }
                        catch (Exception)
                        {
                        }

                        try
                        {
                            CustomEventLog.DisplayEvent(EventLogEntryType.Information, "Publication \'" + this.PublicationName + "\' was disposed properly");
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                this.disposedValue = true;
            }
Example #2
0
 public void BroadCastData(SocketData Data)
 {
     lock (this._connectedClientsTable)
     {
         IEnumerator enumm = default(IEnumerator);
         SocketsServerClientConnectionHandler client = default(SocketsServerClientConnectionHandler);
         enumm = this._connectedClientsTable.GetEnumerator();
         if (this._connectedClientsTable.NumOfConnectedClients > 0)
         {
             while (enumm.MoveNext())
             {
                 client = (SocketsServerClientConnectionHandler)enumm.Current;
                 try
                 {
                     this.SendDataToClient(client, Data);
                 }
                 catch (ObjectDisposedException)
                 {
                 }
                 catch (Exception ex)
                 {
                     string msg = "";
                     msg = "Error broadcasting data to connected clients : " + ex.ToString();
                     CustomEventLog.DisplayEvent(EventLogEntryType.Error, msg);
                 }
             }
         }
     }
 }
Example #3
0
            private void RegisterNewSubscriptorToPublication(DPE_Client subscriptorClient, SocketsServerClientConnectionHandler connectionHandler, DPE_ServerDefs.DPE_PublicationConnectionMode connectionMode)
            {
                try
                {
                    DataRow clientRow = this._STXDSSClienstSubscribedRegistryTable.NewRow();
                    clientRow["Client Name"]          = subscriptorClient.Name;
                    clientRow["Client ID"]            = subscriptorClient.ClientID;
                    clientRow["Client HostName"]      = subscriptorClient.HostName;
                    clientRow["Client AppDomain"]     = subscriptorClient.ApplicationDomainName;
                    clientRow["Connection Date Time"] = System.Convert.ToString(DateTime.Now);
                    clientRow["Client Network ID"]    = System.Convert.ToString(connectionHandler.ClientID);

                    string cnnmode = "";
                    switch (connectionMode)
                    {
                    case DPE_ServerDefs.DPE_PublicationConnectionMode.NotReceiveLastPublicationStatus:
                        cnnmode = "Not Receive Last Status";
                        break;

                    case DPE_ServerDefs.DPE_PublicationConnectionMode.ReceiveLastPublicationStatus:
                        cnnmode = "Receive Last Status";
                        break;

                    default:
                        cnnmode = "Undefined Status";
                        break;
                    }
                    clientRow["Connection Mode"] = cnnmode;

                    lock (this._STXDSSClienstSubscribedRegistryTable)
                    {
                        this._STXDSSClienstSubscribedRegistryTable.Rows.Add(clientRow);
                    }

                    CustomEventLog.DisplayEvent(EventLogEntryType.Information, "Client \'" + subscriptorClient.Name + "\' connected to publication \'" + this._publicationOwner.PublicationName + "\'");
                }
                catch (Exception)
                {
                }
            }
            internal void ThreadedFcn_ConnectionToEnqueuedDataPublications()
            {
                PublicationConnectionTaskInfo reg = null;
                bool continueCycle = true;

                while (continueCycle)
                {
                    try
                    {
                        //if there are connection registers
                        if (this._queueOfScheduledConnectionTasks.Count > 0)
                        {
                            //if the client is connected then performs connections

                            if (this._STXDataSocketClient.IsConnected)
                            {
                                lock (this._queueOfScheduledConnectionTasks)
                                {
                                    reg = (PublicationConnectionTaskInfo)this._queueOfScheduledConnectionTasks.Dequeue();
                                }

                                if (!(reg == null))
                                {
                                    try
                                    {
                                        this.ConnectoToPublication(reg.connectionType, reg.publicationName, reg.connectionMode);


                                        string msg = "";

                                        msg = "Connection with publication \'" + reg.publicationName + "\' was Performed succesfully";
                                        CustomEventLog.DisplayEvent(EventLogEntryType.SuccessAudit, msg);

                                        lock (this._tableOfConnectionRegistersForScheduledConnectionTasks)
                                        {
                                            this._tableOfConnectionRegistersForScheduledConnectionTasks.Remove(reg.publicationName);
                                        }

                                        reg.keepScheduledConnectionTask = false;
                                    }
                                    catch (Exception ex)
                                    {
                                        if (reg.GetElapsedTimeInSeconds() > MAX_CONNECTION_ELAPSED_SECONDS_AFTER_THROW_ERROR)
                                        {
                                            CustomEventLog.DisplayEvent(EventLogEntryType.Error, "Error connecting to publication \'" + reg.publicationName + "\' : " + ex.Message);
                                            reg.ResetTime();
                                        }


                                        //the connection failed and then the connection attempt is enqueed again only it theres is a record on the schedule table
                                        //that indicates that is pending to conenct to such publication

                                        lock (this._tableOfConnectionRegistersForScheduledConnectionTasks)
                                        {
                                            if (this._tableOfConnectionRegistersForScheduledConnectionTasks.ContainsKey(reg.publicationName))
                                            {
                                                lock (this._queueOfScheduledConnectionTasks)
                                                {
                                                    this._queueOfScheduledConnectionTasks.Enqueue(reg);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        CustomEventLog.DisplayEvent(EventLogEntryType.Error, ex.ToString());
                    }
                    finally
                    {
                        if (this._tableOfConnectionRegistersForScheduledConnectionTasks.Count == 0)
                        {
                            this._publicationsConnectionThread = null;
                            continueCycle = false;
                        }
                        else
                        {
                            if (this._STXDataSocketClient.IsConnected)
                            {
                                //is sleeps for 250 ms after attempt to connecto to another publication if the client is connected
                                System.Threading.Thread.Sleep(250);
                            }
                            else
                            {
                                //if the connection with the server is broken then waits a little bit more to attempt to connect to a publication
                                System.Threading.Thread.Sleep(2000);
                            }
                        }
                    }
                }
            }
            private DateTime GetPublicationsServerDAtetime()
            {
                DateTime dtime        = default(DateTime);
                bool     dateObtained = false;

                try
                {
                    using (System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(this._dataBaseSonnectionString))
                    {
                        cnn.Open();

                        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
                        {
                            cmd.Connection = cnn;

                            using (System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd))
                            {
                                using (DataTable dt = new DataTable())
                                {
                                    //selects a datetime as to use as reference to retrieve data
                                    while (!dateObtained)
                                    {
                                        try
                                        {
                                            //using the server current datetime
                                            cmd.CommandText = "Select getdate()";
                                            da.Fill(dt);
                                            if (dt.Rows.Count > 0)
                                            {
                                                dtime        = System.Convert.ToDateTime(dt.Rows[0][0]);
                                                dateObtained = true;
                                            }
                                        }
                                        catch (Exception)
                                        {
                                            //if retrieving the server datetime fails then tries to use the las datetime
                                            //of the current data on the table
                                            try
                                            {
                                                cmd.CommandText = "select max([datetime]) from " + this._tablename;
                                                dtime           = System.Convert.ToDateTime(cmd.ExecuteScalar());
                                                dateObtained    = true;
                                            }
                                            catch (Exception)
                                            {
                                            }
                                        }
                                    }
                                    try
                                    {
                                        dt.Dispose();
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }                                 //datatable



                                try
                                {
                                    da.Dispose();
                                }
                                catch (Exception)
                                {
                                }
                            }                     //SqlDataAdapter
                        }                         //SqlCommand


                        try
                        {
                            cnn.Close();
                            cnn.Dispose();
                        }
                        catch (Exception)
                        {
                        }
                    }                     //SqlConnection
                }
                catch (Exception ex)
                {
                    CustomEventLog.DisplayEvent(EventLogEntryType.Error, ex.ToString());
                }

                return(dtime);
            }
            private void EventHandling_readTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                //*****************************************************************************
                // STOPS THE TIMER
                //*****************************************************************************
                this._readTimer.Stop();

                try
                {
                    if (this._isFirstTimeRead)
                    {
                        //retrieves the server datetime to retrieve the data using datetime
                        this._lastReadDatetime    = this.GetPublicationsServerDAtetime();
                        this.dateAsstring         = NowToStandardODBCStringFormat(this._lastReadDatetime);
                        this._dataReadCommandTest = "Select * from " + this._tablename + " where datetime >= \'" + dateAsstring + "\' order by [datetime] asc";
                        this._isFirstTimeRead     = false;
                    }
                    else
                    {
                        if (this._lastRowIDReaded > 0)
                        {
                            //uses the row id as to retrieve new data
                            this._dataReadCommandTest = "Select * from " + this._tablename + " where rowID > " + System.Convert.ToString(this._lastRowIDReaded) + " order by [rowID] asc";
                        }
                        else
                        {
                            //uses the datetime as to retrieve new data
                            this.dateAsstring         = NowToStandardODBCStringFormat(this._lastReadDatetime);
                            this._dataReadCommandTest = "Select * from " + this._tablename + " where datetime >= \'" + dateAsstring + "\' order by [datetime] asc";
                        }
                    }

                    using (System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(this._dataBaseSonnectionString))
                    {
                        try
                        {
                            cnn.Open();


                            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
                            {
                                cmd.Connection = cnn;

                                //************************************************************
                                //PREPARES THE QUERY TO THE PUBLICATION
                                //************************************************************

                                cmd.CommandText = this._dataReadCommandTest;

                                using (System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter())
                                {
                                    da.SelectCommand = cmd;


                                    using (DataTable dt = new DataTable())
                                    {
                                        //******************************************
                                        // RETRIEVS THE DATA FROM DATA SOURCE
                                        da.Fill(dt);
                                        //******************************************


                                        //************************************************************
                                        //DATA TREATMENT
                                        if (dt.Rows.Count > 0)
                                        {
                                            DataRow     row   = default(DataRow);
                                            IEnumerator enumm = dt.Rows.GetEnumerator();
                                            while (enumm.MoveNext())
                                            {
                                                row = (DataRow )enumm.Current;

                                                //*****************************************************
                                                // transfors the row into a data update or data reset
                                                //*****************************************************
                                                try
                                                {
                                                    long     _rowID;
                                                    string   _dataType        = "";
                                                    string   _value           = "";
                                                    string   _dataName        = "";
                                                    DateTime _dataDatetime    = System.Convert.ToDateTime(null);
                                                    bool     _isDataReset     = false;
                                                    string   _attributesTAble = "";


                                                    _rowID           = (long)(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_ROWID]);
                                                    _dataType        = System.Convert.ToString(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_DATA_TYPE]);
                                                    _value           = System.Convert.ToString(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_VALUE]);
                                                    _dataName        = System.Convert.ToString(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_DATA_NAME]);
                                                    _attributesTAble = System.Convert.ToString(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_ATTRIBUTES_TABLE]);

                                                    try
                                                    {
                                                        _dataDatetime = System.Convert.ToDateTime(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_DATA_DATETIME]);
                                                    }
                                                    catch (Exception)
                                                    {
                                                        _dataDatetime = DateTime.Now;
                                                    }
                                                    _isDataReset = System.Convert.ToBoolean(row[DPE_GlobalDefinitions.DPE_PUBLICATION_TABLE_DEFINITION_IS_DATA_RESET]);


                                                    //------------------------------------------------

                                                    if (!_isDataReset)
                                                    {
                                                        DPE_PublicationData data = default(DPE_PublicationData);
                                                        data = DPE_GlobalDefinitions.GetPublicationValueFromString(this._publicationName, _dataName, _dataType, _value);


                                                        AttributesTable attrTAble = default(AttributesTable);
                                                        //------------------------------------------------
                                                        //verifies if there are a parameters table
                                                        if (_attributesTAble.Length > 0)
                                                        {
                                                            DataVariable    dataVar = XMLDataFormatting.RetrieveDataVariableFromXMLString(_attributesTAble);
                                                            CustomHashTable table   = (CustomHashTable)dataVar.Data;
                                                            attrTAble = new AttributesTable(table);
                                                            data.AttachAttibutesTable(attrTAble);
                                                        }

                                                        this._DATA_UPDATE_PCQ.Enqueue(data);
                                                    }
                                                    else
                                                    {
                                                        this._DATA_RESET_PCQ.Enqueue(_dataName);
                                                    }
                                                }
                                                catch (Exception ex)
                                                {
                                                    CustomEventLog.WriteEntry(ex);
                                                }
                                            }
                                        }
                                        //************************************************************


                                        //************************************************************
                                        //GETS THE LAST TABLE REFERENCE DATE TIME and ROWid FROM THE DATA IF IS AVAILABLE
                                        try
                                        {
                                            if (dt.Rows.Count > 0)
                                            {
                                                this.SetLastReadDateTiemeAndRowID(dt);
                                            }
                                        }
                                        catch (Exception)
                                        {
                                        }
                                        //************************************************************
                                    }                     //----------- data table
                                }                         //-------- SqlDataAdapter
                            }                             //-------- SqlCommand
                        }
                        catch (System.Data.SqlClient.SqlException sqlex)
                        {
                            //overrides the error qhen
                            if (sqlex.ErrorCode != -2146232060)
                            {
                                CustomEventLog.WriteEntry(sqlex);
                            }
                        }
                        catch (Exception ex)
                        {
                            CustomEventLog.DisplayEvent(EventLogEntryType.Error, ex.ToString());
                        }
                        finally
                        {
                            if (this._keepReading)
                            {
                                this._readTimer.Start();
                            }

                            try
                            {
                                cnn.Close();
                                cnn.Dispose();
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }                     //-------  sqlConnection
                }
                catch (Exception ex)
                {
                    CustomEventLog.DisplayEvent(EventLogEntryType.Error, ex.ToString());
                }
            }