public HttpResponseMessage RegisterServer(uint port, uint type, string format, uint threadCount, uint queueSize)
        {
            try
            {
                if (CheckRegisterParametrs(port, type, format, threadCount, queueSize))
                {
                    //Создаем новый объект типа сервер
                    Server server = new Server(port, (Server.ServerType)type, GetClientIp(Request), format.ToLower(), threadCount, queueSize);

                    //Регистрируем его
                    ServerCollection.registerServer(server);

                    //Возвращаем успех
                    return Request.CreateResponse(HttpStatusCode.OK, server);

                }
                else
                {
                    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Регистарция сервера невозможна. Неверные параметры сервера");
                }

            }
            catch (Exception ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
            }


        }
Example #2
0
        //Регистрирует указанный сервер
        public static void registerServer(Server server)
        {
            if (server.isAvailable())
            {
                Server sameAddressServer = getServerByAddress(server.Address, server.Port);
                if (sameAddressServer == null)
                {
                    addServer(server);
                }
                else
                {
                    updateServer(sameAddressServer.Id, server);
                    server.Id = sameAddressServer.Id;
                }
            }
            else
            {
                throw new Exception("Регистрация невозможна, сервер не отвечает на запросы");
            }

        }
Example #3
0
        //Возвращает список серверов по типу и формату
        public static List<Server> getServersByTypeAndFormat(uint type, string format)
        {
            MySqlCommand command = new MySqlCommand();
            command.CommandText = "SELECT * FROM SERVER, FORMAT WHERE FORMAT.NAME_FORMAT=@format AND SERVER.TYPE=@type AND SERVER.FORMAT_ID=FORMAT.ID";
            command.Parameters.AddWithValue("@type", type);
            command.Parameters.AddWithValue("@format", format);

            DataTable serversDt = QueryExecutor.ExecuteQuery(command);

            List<Server> serversList = new List<Server>();

            foreach (DataRow row in serversDt.Rows)
            {
                uint id = uint.Parse(row[0].ToString());
                uint port = uint.Parse(row[2].ToString());
                uint threadCount = uint.Parse(row[5].ToString());
                uint queueSize = uint.Parse(row[6].ToString());
                Server newServer = new Server(id, port, (Server.ServerType)type, row[1].ToString(), format, threadCount, queueSize);
                serversList.Add(newServer);
            }

            return serversList;
        }
Example #4
0
        //Обнавляет сервер с указаным id, беря параметры из второй переменной
        public static void updateServer(uint serverId, Server server)
        {
            //Созадем подключение к БД
            using (MySqlConnection connection = new MySqlConnection(QueryExecutor.mysqlCSB.ConnectionString))
            {

                MySqlTransaction transaction = null;

                try
                {
                    //Открываем его
                    connection.Open();

                    //Начинаем транзакцию
                    transaction = connection.BeginTransaction();

                    //ID формата добавляемого сервера
                    int formatId = addRowReferencedTable(connection, transaction, "FORMAT", server.Format);


                    MySqlCommand command = new MySqlCommand("SET foreign_key_checks = 0;", connection, transaction);
                    command.ExecuteNonQuery();

                    //Выполняем запрос по изменению
                    command.CommandText = "UPDATE SERVER SET ADDRESS=@address, PORT=@port, TYPE=@type, "
                        + "FORMAT_ID=@formatId, THREAD_COUNT=@threadCount, QUEUE_SIZE=@queueSize WHERE SERVER.ID = @serverId";
                    command.Parameters.AddWithValue("@serverId", serverId);
                    command.Parameters.AddWithValue("@address", server.Address);
                    command.Parameters.AddWithValue("@port", server.Port);
                    command.Parameters.AddWithValue("@type", server.Type);
                    command.Parameters.AddWithValue("@formatId", formatId);
                    command.Parameters.AddWithValue("@threadCount", server.ThreadCount);
                    command.Parameters.AddWithValue("@queueSize", server.QueueSize);
                    command.ExecuteNonQuery();


                    command.CommandText = "SET foreign_key_checks = 1;";
                    command.ExecuteNonQuery();

                    //Применяем изменения
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
Example #5
0
        //Добавляет указанный сервер
        public static void addServer(Server server)
        {
            //Созадем подключение к БД
            using (MySqlConnection connection = new MySqlConnection(QueryExecutor.mysqlCSB.ConnectionString))
            {

                MySqlTransaction transaction = null;

                try
                {
                    //Открываем его
                    connection.Open();

                    //Начинаем транзакцию
                    transaction = connection.BeginTransaction();

                    //ID формата добавляемого сервера
                    int formatId = addRowReferencedTable(connection, transaction, "FORMAT", server.Format);


                    MySqlCommand command = new MySqlCommand("SET foreign_key_checks = 0;", connection, transaction);
                    command.ExecuteNonQuery();

                    //Выполняем запрос по вставке сервера
                    command.CommandText = "INSERT INTO SERVER VALUES(NULL, @address, @port, @type, @format_id, @thread_count, @queue_size)";
                    command.Parameters.AddWithValue("@address", server.Address);
                    command.Parameters.AddWithValue("@port", server.Port);
                    command.Parameters.AddWithValue("@type", server.Type);
                    command.Parameters.AddWithValue("@format_id", formatId);
                    command.Parameters.AddWithValue("@thread_count", server.ThreadCount);
                    command.Parameters.AddWithValue("@queue_size", server.QueueSize);
                    command.ExecuteNonQuery();

                    //Получаем Id сервера
                    command.CommandText = "SELECT LAST_INSERT_ID()";
                    server.Id = Convert.ToUInt32(command.ExecuteScalar());

                    command.CommandText = "SET foreign_key_checks = 1;";
                    command.ExecuteNonQuery();

                    //Применяем изменения
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }