Exemple #1
0
        public Server(ServerInfo serverInfo)
        {
            if (serverInfo == null)
                throw new ArgumentNullException("The ServerInfo should'n be null.");

            if (TextUtils.isEmpty(serverInfo.Scheme) || serverInfo.Scheme.ToLower() != SCHEME_HTTP &&
                serverInfo.Scheme.ToLower() != SCHEME_HTTPS)
            {
                throw new ArgumentException("Invalid scheme for the server: " + serverInfo.Scheme);
            }

            if (TextUtils.isEmpty(serverInfo.Hostname))
                throw new ArgumentException("Invalid hostname for the server: " +
                    serverInfo.Hostname);

            if (serverInfo.Port < PORT_MIN || serverInfo.Port > PORT_MAX)
                throw new ArgumentException("Invalid port for the server: " +
                    serverInfo.Port);

            string str = serverInfo.ToString();

            httpListener = new HttpListener();
            httpListener.Prefixes.Add(serverInfo.ToString());

            Dumper.Log(TAG, "Listen on: " + serverInfo.ToString());
        }
        public ServerInfo Load()
        {
            if (File.Exists(DefaultConfiguration.DEFAULT_DEPLOY_FILENAME))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(DefaultConfiguration.DEFAULT_DEPLOY_FILENAME);

                object[] values;
                ServerInfo si = new ServerInfo();

                XmlNode nodeRoot = doc.SelectSingleNode("server");
                if (nodeRoot == null)
                {
                    return si;
                }

                // You can get this bad code here because of the user always drive the thing out of the way normal
                // Anyway, f**k user...

                values = ParseNode(nodeRoot,
                        new string[] { "hostname", "port", "scheme" },
                        new object[] { DefaultConfiguration.DEFAULT_SERVER_HOSTNAME,
                            DefaultConfiguration.DEFAULT_SERVER_PORT,
                            DefaultConfiguration.DEFAULT_SERVER_SCHEME },
                        new Type[] { typeof(string), typeof(ushort), typeof(string) });
                si.Hostname = (string) values[0];
                si.Port = (ushort) values[1];
                si.Scheme = (string) values[2];

                return si;
            }

            return new ServerInfo();
        }
        public bool Export(ServerInfo si, string fn)
        {
            bool bRet = false;

            if (si == null)
            {
                return bRet;
            }

            try
            {
                XmlDocument doc = new XmlDocument();
                XmlElement eleServer = doc.CreateElement("server");

                XmlElement eleServerAddress = doc.CreateElement("hostname");
                XmlElement eleServerPort = doc.CreateElement("port");
                XmlElement eleServerScheme = doc.CreateElement("scheme");

                eleServerAddress.InnerText = si.Hostname;
                eleServerPort.InnerText = si.Port + "";
                eleServerScheme.InnerText = si.Scheme;

                eleServer.AppendChild(eleServerAddress);
                eleServer.AppendChild(eleServerPort);
                eleServer.AppendChild(eleServerScheme);

                doc.AppendChild(eleServer);
                doc.Save(fn);

                bRet = true;
            } catch (Exception) {}

            return bRet;
        }
        public UserConfiguration()
        {
            Maintenance = false;
            SessionLifetime = DefaultConfiguration.DEFAULT_SESSION_LIFETIME;

            ServerInfo = new ServerInfo();
            DatabaseInfo = new DatabaseInfo();
        }
        public bool Save(bool maintance, int sessionLifetime,
            ServerInfo si, DatabaseInfo di)
        {
            bool bRet = false;

            if (sessionLifetime <= 0)
            {
                return bRet;
            }

            if (si == null || di == null)
            {
                return bRet;
            }

            // Create the file
            if (!Directory.Exists(DefaultConfiguration.DEFAULT_CONFIG_FOLDER))
            {
                Directory.CreateDirectory(DefaultConfiguration.DEFAULT_CONFIG_FOLDER);
            }

            if (File.Exists(DefaultConfiguration.DEFAULT_CONFIG_PATH))
            {
                File.Delete(DefaultConfiguration.DEFAULT_CONFIG_PATH);
            }

            try
            {
                // Save the configuration
                XmlDocument doc = new XmlDocument();

                XmlElement eleRoot = doc.CreateElement("settings");
                // Server configuration
                XmlElement eleMaintance = doc.CreateElement("maintenance");
                XmlElement eleSessionLifetime = doc.CreateElement("sessionLifetime");

                XmlElement eleServer = doc.CreateElement("server");
                XmlElement eleServerAddress = doc.CreateElement("hostname");
                XmlElement eleServerPort = doc.CreateElement("port");
                XmlElement eleServerScheme = doc.CreateElement("scheme");

                eleServerAddress.InnerText = si.Hostname;
                eleServerPort.InnerText = si.Port + "";
                eleServerScheme.InnerText = si.Scheme;

                eleServer.AppendChild(eleServerAddress);
                eleServer.AppendChild(eleServerPort);
                eleServer.AppendChild(eleServerScheme);

                // Database configuration
                XmlElement eleDb = doc.CreateElement("database");
                XmlElement eleDbAddress = doc.CreateElement("hostname");
                XmlElement eleDbPort = doc.CreateElement("port");
                XmlElement eleDbUser = doc.CreateElement("username");
                XmlElement eleDbPass = doc.CreateElement("password");
                XmlElement eleDbName = doc.CreateElement("database");
                XmlElement eleDbTimeout = doc.CreateElement("timeout");

                eleDbAddress.InnerText = di.Host;
                eleDbPort.InnerText = di.Port + "";
                eleDbUser.InnerText = di.Username;
                eleDbPass.InnerText = di.Password;
                eleDbName.InnerText = di.Database;
                eleDbTimeout.InnerText = di.Timeout + "";

                eleDb.AppendChild(eleDbAddress);
                eleDb.AppendChild(eleDbPort);
                eleDb.AppendChild(eleDbUser);
                eleDb.AppendChild(eleDbPass);
                eleDb.AppendChild(eleDbName);
                eleDb.AppendChild(eleDbTimeout);

                eleMaintance.InnerText = maintance.ToString();
                eleSessionLifetime.InnerText = sessionLifetime + "";

                // Append to the root element
                eleRoot.AppendChild(eleMaintance);
                eleRoot.AppendChild(eleSessionLifetime);
                eleRoot.AppendChild(eleServer);
                eleRoot.AppendChild(eleDb);

                doc.AppendChild(eleRoot);
                doc.Save(DefaultConfiguration.DEFAULT_CONFIG_PATH);

                bRet = true;
            } catch (Exception) { }

            return bRet;
        }