Пример #1
0
        private bool Init()
        {
            string configfile = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + ".xml";

            System.Xml.Serialization.XmlSerializer xmlc = new System.Xml.Serialization.XmlSerializer(typeof(Configuration));
            System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(NExT.NExTConfiguration));
            SySal.OperaDb.OperaDbConnection        conn = null;
            try
            {
                TheConfig = (Configuration)xmlc.Deserialize(new System.IO.StringReader(System.IO.File.ReadAllText(configfile)));
                System.Collections.ArrayList dynsrvparams = new System.Collections.ArrayList();
                if (TheConfig.DBServer.Length > 0)
                {
                    NExT.NExTConfiguration.ServerParameter sp = new SySal.NExT.NExTConfiguration.ServerParameter();
                    sp.Name  = "DBServer";
                    sp.Value = SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBServer);
                    dynsrvparams.Add(sp);
                    sp       = new SySal.NExT.NExTConfiguration.ServerParameter();
                    sp.Name  = "DBUsr";
                    sp.Value = SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBUserName);
                    sp       = new SySal.NExT.NExTConfiguration.ServerParameter();
                    sp.Name  = "DBPwd";
                    sp.Value = SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBPassword);
                    conn     = new SySal.OperaDb.OperaDbConnection(
                        SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBServer),
                        SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBUserName),
                        SySal.OperaDb.OperaDbCredentials.Decode(TheConfig.DBPassword)
                        );
                    conn.Open();

                    IPHostEntry iph   = Dns.Resolve(Dns.GetHostName());
                    string[]    idstr = new string[iph.Aliases.Length + iph.AddressList.Length];
                    idstr[0] = iph.HostName;
                    int i;
                    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.");
                    }
                    long   IdSite         = SySal.OperaDb.Convert.ToInt64(ds.Tables[0].Rows[0][0]);
                    string SiteName       = ds.Tables[0].Rows[0][1].ToString();
                    long   IdMachine      = SySal.OperaDb.Convert.ToInt64(ds.Tables[0].Rows[0][2]);
                    string MachineName    = ds.Tables[0].Rows[0][3].ToString();
                    string MachineAddress = ds.Tables[0].Rows[0][4].ToString();
                    object o = (TheConfig.DBConfigQuery.Length <= 0) ? null : new SySal.OperaDb.OperaDbCommand(TheConfig.DBConfigQuery.Replace("_ID_MACHINE_", IdMachine.ToString()), conn, null).ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                    {
                        TheConfig.Services = (NExT.NExTConfiguration)xmls.Deserialize(new System.IO.StringReader(o.ToString()));
                    }
                    conn.Close();
                    conn = null;
                }
                SySal.NExT.NExTServer.SetupConfiguration(TheConfig.Services, (SySal.NExT.NExTConfiguration.ServerParameter [])dynsrvparams.ToArray(typeof(SySal.NExT.NExTConfiguration.ServerParameter)));
                WS = new SySal.Web.WebServer(TheConfig.WWWPort, this);
            }
            catch (Exception x)
            {
                MessageBox.Show("Cannot read/apply server configuration file \"" + configfile + "\".\r\nAttempting to write a sample file.\r\n\r\nError:\r\n" + x.ToString(), "Error reading configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
                try
                {
                    System.IO.StringWriter sw = new System.IO.StringWriter();
                    xmlc.Serialize(sw, TheConfig);
                    sw.WriteLine();
                    sw.WriteLine("------------");
                    sw.WriteLine();
                    xmls.Serialize(sw, TheConfig.Services);
                    System.IO.File.WriteAllText(configfile, sw.ToString());
                }
                catch (Exception) { }
                return(false);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Set things in motion.
        /// </summary>
        protected override 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));
                }

                val = ReadOverride("ExeRepository", conn);
                if (val != System.DBNull.Value)
                {
                    ExeRepository = val.ToString();
                }
                else
                {
                    ExeRepository = asr.GetValue("MachinePowerClass", typeof(string)).ToString();
                }

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

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

                val = ReadOverride("DPS_MaxOutputText", conn);
                if (val != System.DBNull.Value)
                {
                    MaxOutputText = Convert.ToInt32(val.ToString());
                }
                else
                {
                    try
                    {
                        MaxOutputText = (int)asr.GetValue("MaxOutputText", typeof(int));
                    }
                    catch (Exception)
                    {
                        MaxOutputText = 65536;
                    }
                }

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

                WebAccess wa = null;
                if (WebPort > 0)
                {
                    WA = new SySal.Web.WebServer(WebPort, wa = new WebAccess());
                }
                if (wa != null)
                {
                    wa.SetShowExceptions(showexc);
                }

                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;
            }
        }