originate.h

Go to the documentation of this file.
00001 /* vim: set et sw=3 tw=0 fo=croqlaw cino=t0:
00002  *
00003  * Astxx, the Asterisk C++ API and Utility Library.
00004  * Copyright (C) 2005-2007  Matthew A. Nicholson
00005  * Copyright (C) 2005-2007  Digium, Inc.
00006  * 
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00027 #ifndef ASTXX_MANAGER_ACTION_ORIGINATE_H
00028 #define ASTXX_MANAGER_ACTION_ORIGINATE_H
00029 
00030 #include <astxx/manager/basic_action.h>
00031 #include <astxx/manager/message.h>
00032 #include <string>
00033 #include <map>
00034 #include <boost/lexical_cast.hpp>
00035 #include <boost/date_time/posix_time/posix_time_types.hpp>
00036 
00037 namespace astxx {
00038    namespace manager {
00039       namespace action {
00040          using boost::lexical_cast;
00041          using boost::posix_time::time_duration;
00042          using boost::posix_time::pos_infin;
00043 
00045          class originate : public basic_action {
00046             public:
00053                originate(const std::string& channel, const std::string& context, const std::string& exten, const std::string& priority)
00054                   : channel(channel), context(context), exten(exten), priority(priority), m_async(false) {
00055                }
00056          
00063                originate(const std::string& channel, const std::string& context, unsigned int exten, const std::string& priority)
00064                   : channel(channel), context(context), exten(lexical_cast<std::string>(exten)), priority(priority), m_async(false) {
00065                }
00066          
00073                originate(const std::string& channel, const std::string& context, const std::string& exten, unsigned int priority)
00074                   : channel(channel), context(context), exten(exten), priority(lexical_cast<std::string>(priority)), m_async(false) {
00075                }
00076          
00083                originate(const std::string& channel, const std::string& context, unsigned int exten, unsigned int priority)
00084                   : channel(channel), context(context), exten(lexical_cast<std::string>(exten)), priority(lexical_cast<std::string>(priority)), m_async(false) {
00085                }
00086          
00093                originate(const std::string& channel, const std::string& application, const std::string& data)
00094                   : channel(channel), application(application), data(data), m_async(false) {
00095                }
00096 
00102                action::originate& timeout(time_duration to) {
00103                   m_timeout = to;
00104                   return *this;
00105                }
00106 
00113                action::originate& caller_id(const std::string& cid) {
00114                   m_caller_id = cid;
00115                   return *this;
00116                }
00117 
00122                action::originate& account(const std::string& account) {
00123                   m_account = account;
00124                   return * this;
00125                }
00126 
00131                action::originate& async(bool state) {
00132                   m_async = state;
00133                   return *this;
00134                }
00135 
00141                action::originate& variables(const std::map<std::string, std::string>& vars) {
00142                   m_variables = vars;
00143                   return *this;
00144                }
00145 
00146                message::action action() const {
00147                   message::action action("Originate");
00148                   action["Channel"] = channel;
00149 
00150                   if (not context.empty()) {
00151                      action["Context"] = context;
00152                      action["Exten"] = exten;
00153                      action["Priority"] = priority;
00154                   }
00155                   else {
00156                      action["Application"] = application;
00157                      action["Data"] = data;
00158                   }
00159 
00160                   if (not m_timeout.is_special() and m_timeout != time_duration()) {
00161                      action["Timeout"] = lexical_cast<std::string>(m_timeout.total_milliseconds());
00162                   }
00163                   else if (m_timeout == pos_infin) {
00164                      action["Timeout"] = "-1"; // Asterisk uses -1 for infinite
00165                   }
00166 
00167                   if (not m_caller_id.empty())
00168                      action["CallerID"] = m_caller_id;
00169 
00170                   if (not m_account.empty())
00171                      action["Account"] = m_account;
00172 
00173                   if (m_async)
00174                      action["Async"] = "true";
00175 
00176                   // format "Variable:" headers
00177                   for (std::map<std::string, std::string>::const_iterator i = m_variables.begin(); i != m_variables.end(); ++i) {
00178                      action.insert(std::make_pair("Variable", i->first + "=" + i->second));
00179                   }
00180 
00181                   return action;
00182                }
00183 
00184             private:
00185                std::string channel;
00186 
00187                std::string context;
00188                std::string exten;
00189                std::string priority;
00190 
00191                std::string application;
00192                std::string data;
00193 
00194                time_duration m_timeout;
00195                std::string m_caller_id;
00196                std::string m_account;
00197                bool m_async;
00198                std::map<std::string, std::string> m_variables;
00199          };
00200       }
00201    }
00202 }
00203 
00204 #endif

Generated on Thu Jul 3 01:32:42 2008 for Astxx by  doxygen 1.5.6