forked from yohanson/astercti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasterisk.cpp
More file actions
133 lines (119 loc) · 3.24 KB
/
asterisk.cpp
File metadata and controls
133 lines (119 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <map>
#include <list>
#include <wx/wx.h>
#include <wx/socket.h>
#include <sstream>
#include "observer.h"
#include "asterisk.h"
void Asterisk::Notify(AmiMessage &message)
{
for (auto iter : _observers)
{
iter->handleEvent(message);
}
}
void Asterisk::OnSocketEvent(wxSocketEvent &event)
{
switch ( event.GetSocketEvent() )
{
case wxSOCKET_INPUT:
//std::cout << "Input available on the socket" << std::endl;
OnInputAvailable();
break;
case wxSOCKET_LOST:
//std::cout << "Socket connection was unexpectedly lost." << std::endl;
break;
case wxSOCKET_CONNECTION:
std::cout << "... socket is now connected." << std::endl;
break;
default:
//std::cout << "Unknown socket event!!!" << std::endl;
break;
}
}
void Asterisk::OnInputAvailable()
{
m_socket->Read(m_recv_buff, RECV_BUFF);
int read_bytes = m_socket->LastReadCount();
m_recv_buff[read_bytes] = 0;
std::string raw_messages = m_recv_buff;
std::string delim = "\r\n";
std::string line, key, value;
size_t start = 0, colon;
size_t end = raw_messages.find(delim);
AmiMessage am;
while (end != std::string::npos)
{
line = raw_messages.substr(start, end-start);
//std::cout << line << std::endl;
if ( ! line.length() )
{
//std::cout << "===================================\n" << message << std::endl;
Notify(am);
am.clear();
}
else
{
colon = line.find(':');
key = line.substr(0, colon);
if (line.length() == colon+1)
value = "";
else
value = line.substr(colon+2);
am[key] = value;
if (key == "Channel")
{
am["ChannelID"] = value.substr(0, value.find_last_of('-'));
}
}
start = end + delim.length();
end = raw_messages.find(delim, start);
}
}
void Asterisk::add(IObserver& observer)
{
_observers.push_back(&observer);
}
Asterisk::Asterisk(std::string host, int port, std::string username, std::string secret)
{
Bind(wxEVT_SOCKET, &Asterisk::OnSocketEvent, this);
m_socket = new wxSocketClient;
m_socket->SetEventHandler(*this);
m_socket->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_CONNECTION_FLAG | wxSOCKET_LOST_FLAG);
m_socket->Notify(true);
wxIPV4address addr;
addr.Hostname(host);
addr.Service(port);
m_socket->Connect(addr);
std::string login = "Action: login\nUsername: "+username+"\nSecret: "+secret+"\n\n";
m_socket->Write(login.c_str(), login.length());
}
Asterisk::~Asterisk()
{
m_socket->Close();
}
void Asterisk::Originate(std::string mychan, std::string context, std::string exten, std::string myexten, int priority)
{
std::ostringstream actionstream;
char id[32];
time_t tmp_time = time(0);
strftime(id, 32, "%H%M%S", localtime(&tmp_time));
actionstream <<
"Action: originate"
<< "\nChannel: " << mychan
<< "\nContext: " << context
<< "\nExten: " << exten
<< "\nPriority: " << priority
<< "\nAsync: yes"
<< "\nCallerID: \"" << exten << "\" <" << myexten << ">"
<< "\nActionID: " << mychan << "-" << id
<< "\n\n";
std::string action = actionstream.str();
std::cout << "Going to originate with this: '" << action << "'" << std::endl;
m_socket->Write(action.c_str(), action.length());
}
void Asterisk::HangupChannel(std::string &channel)
{
std::string action = "Action: hangup\nChannel: "+channel+"\n\n";
m_socket->Write(action.c_str(), action.length());
}