Ejemplo n.º 1
0
        /// <summary>
        /// Set things in motion.
        /// </summary>
        protected void OnStart(string[] args)
        {
            try
            {
                int i;
                if (!EventLog.Exists("Opera"))
                {
                    EventLog.CreateEventSource("OperaDataProcServer", "Opera");
                }
                EventLog.Source = "OperaDataProcServer";
                EventLog.Log    = "Opera";

                System.Configuration.AppSettingsReader asr = new System.Configuration.AppSettingsReader();
                try
                {
                    DBServer   = SySal.OperaDb.OperaDbCredentials.Decode((string)asr.GetValue("DBServer", typeof(string)));
                    DBUserName = SySal.OperaDb.OperaDbCredentials.Decode((string)asr.GetValue("DBUserName", typeof(string)));
                    DBPassword = SySal.OperaDb.OperaDbCredentials.Decode((string)asr.GetValue("DBPassword", typeof(string)));
                }
                catch (Exception x)
                {
                    throw new Exception("Encryption error in credentials.\r\nPlease fill in valid encrypted data (you can use OperaDbGUILogin, for instance), or run the service as the appropriate user.");
                }
                SySal.OperaDb.OperaDbConnection conn = new SySal.OperaDb.OperaDbConnection(DBServer, DBUserName, DBPassword);
                conn.Open();

                IPHostEntry iph   = Dns.Resolve(Dns.GetHostName());
                string []   idstr = new string[iph.Aliases.Length + iph.AddressList.Length];
                idstr[0] = iph.HostName;
                for (i = 0; i < iph.Aliases.Length; i++)
                {
                    idstr[i] = iph.Aliases[i];
                }
                for (i = 0; i < iph.AddressList.Length; i++)
                {
                    idstr[i + iph.Aliases.Length] = iph.AddressList[i].ToString();
                }
                string selstr = "LOWER(TB_MACHINES.ADDRESS)='" + iph.HostName.ToLower() + "'";
                foreach (string s in idstr)
                {
                    selstr += (" OR ADDRESS='" + s + "'");
                }
                DataSet ds = new DataSet();
                SySal.OperaDb.OperaDbDataAdapter da = new SySal.OperaDb.OperaDbDataAdapter("SELECT TB_SITES.ID, TB_SITES.NAME, TB_MACHINES.ID, TB_MACHINES.NAME, TB_MACHINES.ADDRESS FROM TB_SITES INNER JOIN TB_MACHINES ON (TB_MACHINES.ID_SITE = TB_SITES.ID AND TB_MACHINES.ISDATAPROCESSINGSERVER = 1 AND (" + selstr + "))", conn, null);
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count < 1)
                {
                    throw new Exception("Can't find myself in OperaDb registered machines. This service is made unavailable.");
                }
                IdSite         = SySal.OperaDb.Convert.ToInt64(ds.Tables[0].Rows[0][0]);
                SiteName       = ds.Tables[0].Rows[0][1].ToString();
                IdMachine      = SySal.OperaDb.Convert.ToInt64(ds.Tables[0].Rows[0][2]);
                MachineName    = ds.Tables[0].Rows[0][3].ToString();
                MachineAddress = ds.Tables[0].Rows[0][4].ToString();

                object val;
                val = ReadOverride("DPS_MachinePowerClass", conn);
                if (val != System.DBNull.Value)
                {
                    MachinePowerClass = Convert.ToInt32(val.ToString());
                }
                else
                {
                    MachinePowerClass = (int)asr.GetValue("MachinePowerClass", typeof(int));
                }

                val = ReadOverride("DPS_PeakWorkingSetMB", conn);
                if (val != System.DBNull.Value)
                {
                    PeakWorkingSetMB = Convert.ToInt32(val.ToString());
                }
                else
                {
                    PeakWorkingSetMB = (int)asr.GetValue("PeakWorkingSetMB", typeof(int));
                }

                val = ReadOverride("DPS_ResultLiveSeconds", conn);
                if (val != System.DBNull.Value)
                {
                    ResultLiveTime = System.TimeSpan.FromSeconds(Convert.ToInt32(val.ToString()));
                }
                else
                {
                    ResultLiveTime = System.TimeSpan.FromSeconds((int)asr.GetValue("ResultLiveSeconds", typeof(int)));
                }

                val = ReadOverride("DPS_LowPriority", conn);
                if (val != System.DBNull.Value)
                {
                    LowPriority = Convert.ToBoolean(val.ToString());
                }
                else
                {
                    LowPriority = (bool)asr.GetValue("LowPriority", typeof(bool));
                }

                ChannelServices.RegisterChannel(new TcpChannel((int)SySal.DAQSystem.OperaPort.DataProcessingServer));
                DPS = new SySal.DAQSystem.MyDataProcessingServer(EventLog);
                RemotingServices.Marshal(DPS, "DataProcessingServer.rem");
                conn.Close();

                EventLog.WriteEntry("Service starting\r\nThread Priority: " + (LowPriority ? "Below normal" : "Normal") + "\r\nPeak working set (MB): " + PeakWorkingSetMB + "\r\nMachine power class: " + MachinePowerClass + "\r\nSelf-identification yields:\r\nSite: " + SiteName + "\r\nSite Id: " + IdSite + "\r\nMachine: " + MachineName + "\r\nMachine Id: " + IdMachine + "\r\nMachine Address: " + MachineAddress, EventLogEntryType.Information);
            }
            catch (System.Exception x)
            {
                EventLog.WriteEntry("Service startup failure:\n" + x.ToString(), EventLogEntryType.Error);
                throw x;
            }
        }