public void Load()
        {
            //
            // Check if instance file exists
            //
            if (!File.Exists(activeConfigFileName))
            {
                throw (new ConfigurationErrorsException(
                           String.Format(
                               "The Instance Repository File is missing, please check the configuration. File = \"{0}\"", activeConfigFileName)));
            }
            else
            {
                // Read content
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(ServerConfig));

                StreamReader reader = null;

                try
                {
                    try
                    {
                        reader = new StreamReader(activeConfigFileName);
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Problems reading the Instance Repository File, file = \"{0}\"", activeConfigFileName), ex));
                    }

                    // Validate the contents
                    String       schemaFileName = String.Format("{0}.xsd.ServerConfig.xsd", defaultNameSpace);
                    StreamReader schemaReader;

                    try
                    {
                        schemaReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(schemaFileName));
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Failed to load embedded schema file, cannot validate instance repository file. Schema file = \"{0}\"", schemaFileName), ex));
                    }

                    // Validate file, get 10 errors max
                    XmlValidator xmlValidator = new XmlValidator();
                    String       errors       = xmlValidator.ValidateStream(reader.BaseStream, schemaReader.BaseStream, 10);

                    if (errors != "")
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "The instance repository file contains syntax errors, file = \"{0}\"\n{1}", activeConfigFileName, errors)));
                    }

                    try
                    {
                        reader.BaseStream.Position = 0;
                        ServerConfig = xmlSerializer.Deserialize(reader) as ServerConfig;

                        #region CompensateBadConfig
                        // make sure refreshrate is always specified
                        if (!ServerConfig.RefreshRateSpecified)
                        {
                            ServerConfig.RefreshRate = ServerConfigRefreshRate.Normal;
                        }

                        // Make sure at least one Broadcast entry exists
                        if ((ServerConfig.Broadcast == null) || (ServerConfig.Broadcast.Length == 0))
                        {
                            ServerConfig.Broadcast            = new BroadcastType[1];
                            ServerConfig.Broadcast[0].Ip      = "239.255.255.255";
                            ServerConfig.Broadcast[0].Port    = 11000;
                            ServerConfig.Broadcast[0].Timeout = 5000;
                        }
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Problems reading the instance repository file, file = \"{0}\"", activeConfigFileName), ex));
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
        }
Esempio n. 2
0
        public void Load()
        {
            //
            // Check if instance file exists
            //
            if (!File.Exists(activeConfigFileName))
            {
                return;
            }
            else
            {
                // Read content
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(ServerList));

                StreamReader reader = null;

                try
                {
                    try
                    {
                        reader = new StreamReader(activeConfigFileName);
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Problems reading the Instance Repository File, file = \"{0}\"", activeConfigFileName), ex));
                    }

                    // Validate the contents
                    String       schemaFileName = String.Format("{0}.xsd.ServerConfig.xsd", defaultNameSpace);
                    StreamReader schemaReader;

                    try
                    {
                        schemaReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(schemaFileName));
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Failed to load embedded schema file, cannot validate instance repository file. Schema file = \"{0}\"", schemaFileName), ex));
                    }

                    // Validate file, get 10 errors max
                    XmlValidator xmlValidator = new XmlValidator();
                    String       errors       = xmlValidator.ValidateStream(reader.BaseStream, schemaReader.BaseStream, 10);

                    if (errors != "")
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "The instance repository file contains syntax errors, file = \"{0}\"\n{1}", activeConfigFileName, errors)));
                    }

                    try
                    {
                        reader.BaseStream.Position = 0;
                        serverList = xmlSerializer.Deserialize(reader) as ServerList;

                        // make sure refreshrate is always specified

                        //if (serverList.RefreshRateSpecified)
                        //    refreshRate = serverList.RefreshRate;
                        //else
                        //    refreshRate = ServerListRefreshRate.Normal;

                        // Set default values for broadcast if missing

                        //if ((serverList.Broadcast != null) && (serverList.Broadcast.Length > 0))
                        //else
                        //{
                        //    broadcast.Ip = "239.255.255.255";
                        //    broadcast.Port = 11000;
                        //    broadcast.Timeout = 5000;
                        //}
                    }
                    catch (Exception ex)
                    {
                        throw (new ConfigurationErrorsException(
                                   String.Format(
                                       "Problems reading the instance repository file, file = \"{0}\"", activeConfigFileName), ex));
                    }
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
        }
Esempio n. 3
0
        public ServerConfig ReadServerConfig(String name, String fileName)
        {
            ServerConfig sc = null;

            // Init
            XmlSerializer s = new XmlSerializer(typeof(ServerConfig));
            StreamReader  r = null;

            try
            {
                try
                {
                    int retryCount = 1;

                    if (FileIO.FileExists(fileName))
                    {
                        retryCount = 10;
                    }

                    while (true)
                    {
                        try
                        {
                            r = new StreamReader(fileName);
                            break;
                        }
                        catch (IOException ioe)
                        {
                            retryCount--;
                            if (retryCount > 0)
                            {
                                Thread.Sleep(200);
                            }
                            else
                            {
                                throw(ioe);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Problems reading the instance {0} configuration file, file = \"{1}\"", name, fileName), e));
                }

                // Validate the contents
                String       schemaFileName = String.Format("{0}.xsd.ServerConfig.xsd", defaultNameSpace);
                StreamReader schema;

                try
                {
                    schema = FileIO.GetFileFromResources(schemaFileName);
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Failed to load embedded schema file, cannot validate server configuration file. Schema file = \"{0}\"", schemaFileName), e));
                }

                // Validate file, get 10 errors max
                XmlValidator xv     = new XmlValidator();
                String       errors = xv.ValidateStream(r.BaseStream, schema.BaseStream, 10);

                if (errors != "")
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "The instance {0} configuration file contains syntax errors, file = \"{1}\"\n{2}", name, fileName, errors)));
                }

                try
                {
                    r.BaseStream.Position = 0;
                    sc = s.Deserialize(r) as ServerConfig;
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Problems reading the instance {0} configuration file, file = \"{1}\"", name, fileName), e));
                }
            }
            finally
            {
                if (r != null)
                {
                    r.Close();
                }
            }

            return(sc);
        }
Esempio n. 4
0
        public ApplicationConfig()
        {
            me = this;

            repoFileName = FileIO.FindAppConfigFile("InstanceRepositoryFile", "instance.config");

            #region oldcode
//      ConfigurationSettings.AppSettings["InstanceRepositoryFile"];
//
//      if(repoFileName == null)
//      {
//        // Try to locate the file manually
//        String initDir = FindInitDirectory();
//
//        if(FileIO.FileExists(initDir +@"\instance.config"))
//        {
//          repoFileName = initDir + @"\instance.config";
//        }
//        else
//        {
//          String configFile = Application.ExecutablePath + ".config";
//
//          if(!FileIO.FileExists(configFile))
//          {
//            throw(new ConfigurationErrorsException(
//              String.Format("The application config file is missing, file = \"{0}\"", configFile)));
//          }
//
//          // Show sample file
//          String message = "Application config file does not contain the InstanceRepositoryFile value.";
//
//          try
//          {
//            String sampleFileName = String.Format("{0}.sample.sample.config",defaultNameSpace);
//            StreamReader sample = FileIO.GetFileFromResources(sampleFileName);
//
//            String sampleText = sample.ReadToEnd().Replace("\r","");
//
//            message = String.Format("{0}\nExample:\n{1}\n",message,sampleText);
//          }
//          catch(Exception)
//          {
//          }
//
//          message = String.Format("{0}\nFileName = \"{1}\"",message,configFile);
//
//          throw(new ConfigurationErrorsException(message));
//        }
//      }
            #endregion
            //
            // Check that instance file exists and is readable
            //

            // Read content
            XmlSerializer s = new XmlSerializer(typeof(InstanceList));

            StreamReader r = null;

            try
            {
                try
                {
                    r = new StreamReader(repoFileName);
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Problems reading the Instance Repository File, file = \"{0}\"", repoFileName), e));
                }

                // Validate the contents
                String       schemaFileName = String.Format("{0}.xsd.InstanceList.xsd", defaultNameSpace);
                StreamReader schema;

                try
                {
                    schema = FileIO.GetFileFromResources(schemaFileName);
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Failed to load embedded schema file, cannot validate instance repository file. Schema file = \"{0}\"", schemaFileName), e));
                }

                // Validate file, get 10 errors max
                XmlValidator xv     = new XmlValidator();
                String       errors = xv.ValidateStream(r.BaseStream, schema.BaseStream, 10);

                if (errors != "")
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "The instance repository file contains syntax errors, file = \"{0}\"\n{1}", repoFileName, errors)));
                }

                il = null;

                try
                {
                    r.BaseStream.Position = 0;
                    il = s.Deserialize(r) as InstanceList;
                }
                catch (Exception e)
                {
                    throw(new ConfigurationErrorsException(
                              String.Format(
                                  "Problems reading the instance repository file, file = \"{0}\"", repoFileName), e));
                }
            }
            finally
            {
                if (r != null)
                {
                    r.Close();
                }
            }
        }
Esempio n. 5
0
        public void Config()
        {
            string setupFileName = FileIO.FindAppConfigFile("BroadcastConfigFile", "broadcast.config");

            //
            // Check that instance file exists and is readable
            //

            // Read content
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(BroadcastSetup));

            StreamReader reader = null;

            try
            {
                try
                {
                    reader = new StreamReader(setupFileName);
                }
                catch (Exception e)
                {
                    throw (new ConfigurationErrorsException(
                               string.Format(
                                   "Problems reading the broadcast config File, file = \"{0}\"", setupFileName), e));
                }

                // Validate the contents
                string       schemaFilename = string.Format("{0}.xsd.BroadcastSetup.xsd", defaultNameSpace);
                StreamReader schemaReader;

                try
                {
                    schemaReader = new StreamReader(Assembly.GetCallingAssembly().GetManifestResourceStream(schemaFilename));
                }
                catch (Exception e)
                {
                    throw (new ConfigurationErrorsException(
                               string.Format(
                                   "Failed to load embedded schema file, cannot validate broadcast config file. Schema file = \"{0}\"", schemaFilename), e));
                }

                // Validate file, get 10 errors max
                XmlValidator xmlValidator = new XmlValidator();
                string       errors       = xmlValidator.ValidateStream(reader.BaseStream, schemaReader.BaseStream, 10);

                if (errors != "")
                {
                    throw (new ConfigurationErrorsException(
                               string.Format(
                                   "The broadcast config file contains syntax errors, file = \"{0}\"\n{1}", setupFileName, errors)));
                }

                conf = null;

                try
                {
                    reader.BaseStream.Position = 0;
                    conf = xmlSerializer.Deserialize(reader) as BroadcastSetup;
                }
                catch (Exception e)
                {
                    throw (new ConfigurationErrorsException(
                               string.Format(
                                   "Problems reading the broadcast config file, file = \"{0}\"", setupFileName), e));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }