Hatred's Log Place

DON'T PANIC!

Dec 30, 2010 - 3 minute read - programming c++

Astxx: action для команды QueueStatus (+пример)

Логическое продолжение для этой статьи, команда для получения состояния очереди.

Подкатом реализация и пример использования команд QueuePause и QueueStatus, какие эвенты получаются смотрите в документации(( тут или тут, вторая ссылка более полная, но на буржуйском, хотя кого это пугает? :wink:))

Реализацяи QueueStatus

/* 
 * Additonal actions for Astxx
 * Copyright (C) 2010  Alexander Drozdov <hatred@inbox.ru>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 *
 * Include this file to use the QueueStatus action. You should include it by hands.
 *
 */

#ifndef ASTXX_MANAGER_ACTION_QUEUE_STATUS_H
#define ASTXX_MANAGER_ACTION_QUEUE_STATUS_H

#include <astxx/manager/basic_action.h>
#include <astxx/manager/message.h>
#include <astxx/manager/error.h>

namespace astxx {
   namespace manager {
      namespace action {
         /** Ask Queue Status
          */
         class queue_status : public basic_action {
            public:
               /** Construct a QueueStatus action.
                * @param queue          - concretize queue [optional]
                * @param member         - select member [optional]
                * @param action_id      - add optional action Id to all responses [optional]
                */
               queue_status(const std::string& member    = "",
                            const std::string& queue     = "", 
                            const std::string& action_id = "") :
                                member(member),
                                queue(queue),
                                action_id(action_id) {}

               /** Format this action as a message::action.
                * @return this message as a message::action
                */
               message::action action() const {
                  message::action action("QueueStatus");

                  if (!queue.empty())
                  {
                    action["Queue"] = queue;
                  }

                  if (!member.empty())
                  {
                    action["Member"] = member;
                  }

                  if (!action_id.empty())
                  {
                    action["ActionID"] = action_id;
                  }

                  return action;
               }

            private:
               std::string queue;
               std::string member;
               std::string action_id;
         };
      }
   }
}

#endif // QUEUESTATUS_H

Пример

Пример основан на event-test.cc, что идёт вместе с astxx.

#include <astxx/manager.h>
#include <vector>
#include <string>
#include <iostream>

#include "queue_status.h"
#include "queue_pause.h"

void print_event(astxx::manager::message::event e) 
{
   //std::cout << e.format();
   std::cout << e["Paused"] << "<br/>n";
}

int main(int argc, char** argv) 
{
   std::vector<std::string> args(argv, argv + argc);

   if (args.size() != 4) 
   {
      std::cerr << "Usage: " << args[0] << " [host] [username] [secret]<br/>n";
      return 1;
   }

   try 
   {
      namespace manager = astxx::manager;
      namespace action = astxx::manager::action;

      manager::connection connection(args[1]);
      std::cout << "Connected to " << connection.name() << " v" << connection.version() << std::endl;

      boost::signals::scoped_connection c1 = connection.register_event("QueueMember", print_event);
      //boost::signals::scoped_connection c2 = connection.register_event("Queue", print_event);

      action::login login(args[2], args[3]);
      login(connection);

      action::queue_pause qpause("SIP/3008", true);
      manager::message::response resp = qpause(connection);
      std::cout << resp.format();

      action::queue_status qstatus("SIP/3008");
      resp = qstatus(connection);
      std::cout << resp.format();

      action::queue_pause qpause2("SIP/3008", false);
      resp = qpause2(connection);
      std::cout << resp.format();

      action::queue_status qstatus2("SIP/3008");
      resp = qstatus2(connection);
      std::cout << resp.format();


      for (;;) 
      {
         connection.wait_event();
         connection.pump_messages();
         connection.process_events();
      }
      return 0;
   }
   catch (const std::exception& e) 
   {
      std::cerr << "Exception: " << e.what() << std::endl;
   }
   return 1;
}

Собственно что тут происходит:

  • создаём подключение
  • регистрируем обработчик для эвента QueueMember (смотрим токи на AMI)
  • логинимся
  • ставим мембера в паузу и спрашиваем его состояние
  • убираем паузу и опять спрашиваем его состояние
  • в цикле обрабатываем приходящие эвенты