forked from augcampos/asterisk-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReader.cpp
More file actions
136 lines (117 loc) · 4.07 KB
/
Reader.cpp
File metadata and controls
136 lines (117 loc) · 4.07 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
133
134
135
/*
* Reader.cpp
*
* Created on: Jul 5, 2011
* Author: augcampos
*/
#include "asteriskcpp/manager/Reader.h"
#include <stdlib.h>
#include <boost/algorithm/string/predicate.hpp>
#include "asteriskcpp/exceptions/Exception.h"
#include "asteriskcpp/exceptions/IOException.h"
#include "asteriskcpp/utils/LogHandler.h"
#include "asteriskcpp/utils/StringUtils.h"
#include "asteriskcpp/manager/AsteriskVersion.h"
#include "asteriskcpp/manager/responses/ManagerResponse.h"
#include "asteriskcpp/manager/events/ManagerEvent.h"
#define SOCKET_WAIT 0
const std::string SEP("\r\n\r\n");
const std::string SEPLN("\r\n");
const unsigned int RCVBUFSIZE = 65536;
namespace asteriskcpp {
void Reader::start(TCPSocket* s, Dispatcher* d) {
connectionSocket = s;
dispatcher = d;
Thread::start();
}
void Reader::stop() {
Thread::stop();
dispatcher = NULL;
connectionSocket = NULL;
}
void Reader::run() {
try {
char buffer[(RCVBUFSIZE + 1)] = "\0";
if (connectionSocket != NULL && connectionSocket->check4readData(SOCKET_WAIT)) {
int bytesRead = connectionSocket->readData(buffer, RCVBUFSIZE);
if (bytesRead > 0) {
std::string rsv(buffer, bytesRead);
processIncomming(rsv);
rsv.clear();
}
}
} catch (SocketException& e) {
stop();
std::cout << "___CATCH SocketException" << std::endl;
LOG_ERROR_STR(e.getMessage());
} catch (Exception& e) {
stop();
std::cout << "___CATCH Exception" << std::endl;
LOG_ERROR_STR(e.getMessage());
}
}
void Reader::processIncomming(const std::string& newStr) {
size_t cutAt, endAt;
//LOG_TRACE_STR(str2Log(newStr));
std::string str;
str.append(unprocessedStr);
str.append(newStr);
unprocessedStr.clear();
while ((cutAt = str.find(SEPLN)) != str.npos) {
if (cutAt > 0) {
int type(0);
std::string endDelim;
if (boost::istarts_with(str, "Asterisk")) {
type = 1;
endDelim = SEPLN;
} else if (boost::istarts_with(str, "Response:")) {
if (boost::istarts_with(str, "Response: Follows")) {
type = 2;
endDelim = "\n--END COMMAND--\r\n\r\n";
} else {
type = 3;
endDelim = SEP;
}
} else if (boost::istarts_with(str, "Event:")) {
type = 4;
endDelim = SEP;
}
if ((endAt = str.find(endDelim)) == str.npos) {
break;
}
std::string nstr = str.substr(0, endAt);
LOG_TRACE_DATA("[DISPATCH: " << type << "::::" << str2Log(nstr) << ":DISPATCH]");
switch (type) {
case 1:
{
AsteriskVersion ver;
ver.setManagerValues(nstr);
dispatcher->dispatchAsteriskVersion(&ver);
}
break;
case 2:
case 3:
{
dispatcher->dispatchResponse(nstr);
}
break;
case 4:
{
dispatcher->dispatchEvent(nstr);
}
break;
default:
{
LOG_WARN_STR("INVALID Type Received" + nstr);
}
break;
}
str = str.substr(endAt + endDelim.length());
} else
str = str.substr(SEPLN.length());
}
if (str.length() > 0) {
unprocessedStr.append(str); // saves data not processed
}
}
}