-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceResponseSimulator.groovy
180 lines (155 loc) · 6 KB
/
VoiceResponseSimulator.groovy
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package dist
import groovyx.gpars.actor.DefaultActor
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.apache.http.protocol.HttpContext
import org.apache.log4j.Logger
import org.simpleframework.http.Request
import java.util.concurrent.ConcurrentLinkedQueue
enum JobState {
INIT(Constants.HOST_URL+"pressAnyDigit", [waitAfterPrompt: "5", promptN : 1]),
STARTED(Constants.HOST_URL+"reportHumanDetected"),
DETECTED(Constants.HOST_URL+"content", [isVoicemail: "true", ttsRate: "1s", isPlayMsgOnly: "true"]),
DELIVERED(Constants.HOST_URL+"respond", [choice: "2"]),
FAILED(Constants.HOST_URL+"errorExit"),
RESPONDED(Constants.HOST_URL+"callSuccess"),
SUCCEEDED(Constants.HOST_URL+"goodbye"),
ENDED(null);
JobState(String baseUrl, Map parameters) {
logger.debug("baseUrl ${baseUrl}; parameters=${parameters}")
this.baseUrl = baseUrl;
this.parameters = parameters;
}
JobState(String baseUrl) {
this(baseUrl, [:])
}
String getNextUrl(def eventId) {
String rv = "${baseUrl}?" + parameters.collect {k, v -> "${k}=${v}"}.join("&")
return parameters.isEmpty() ? rv + "eventId=${eventId}" : rv + "&eventId=${eventId}"
}
Logger logger = Logger.getLogger(JobState.class)
String baseUrl;
Map parameters;
}
enum JobStatus {
FAILED, SUCCESS
}
class Constants {
static String HOST_URL = "http://localhost:8888/apiservice/voxeoapi/"
static HttpClient client
static HttpClient createClient() {
if (client) {
return client
}
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
client = HttpClientBuilder.create().setConnectionManager(cm).build()
}
}
class Poster extends DefaultActor {
private static final Logger logger = Logger.getLogger(Poster.class)
private final HttpClient client
Poster() {
client = Constants.createClient()
}
void act() {
loop {
react { String url ->
HttpGet method = new HttpGet(url)
try {
HttpResponse response = client.execute(method, (HttpContext)null)
int code = response.statusLine.statusCode
logger.info("GetMethod ${url} return code = [${code}]; content = [${response?.entity?.content?.toString()}].")
reply code >= 200 && code < 300 ? JobStatus.SUCCESS : JobStatus.FAILED
} catch (Exception e) {
logger.error("GetMethod failed ${url}.", e)
reply JobStatus.FAILED
} finally {
method.releaseConnection()
}
}
}
}
}
class RequestHandler extends DefaultActor {
private static final Logger logger = Logger.getLogger(RequestHandler.class)
final Poster server = new Poster()
final Queue<String> jobs = [] as ConcurrentLinkedQueue<String>
private JobState state = JobState.INIT
Request request
def eventId
RequestHandler(Request request) {
this.request = request
eventId = request.form.find { key, value -> key.toLowerCase() == "event_id" }?.value
logger.info("Request eventId: ${eventId}")
logger.info("Request received for: ${request.form.collect { key, value -> "[${key} : ${value}]" }.join(" ")}")
server.start();
start();
}
Map handleRequest() {
if (eventId) {
jobs.add(state.getNextUrl(eventId))
}
return [HttpStatus: 200]
}
void act() {
loop {
def url = jobs.poll()
if (url) {
logger.info("Next Request: ${url}")
server.send url
react { rv ->
switch (rv) {
case JobStatus.FAILED:
if (state == JobState.FAILED) {
logger.info("Done ${eventId}")
terminate();
} else {
moveToFailed(url)
}
break;
case JobStatus.SUCCESS:
if (state == JobState.FAILED) {
logger.info("Done ${eventId}")
terminate();
} else {
moveToNextState()
logger.info("State = ${state}")
if ([JobState.ENDED].contains(state)) {
logger.info("Done ${eventId}")
terminate();
} else {
logger.info("Request succeeded, will send ${state.getNextUrl(eventId)}")
jobs.add(state.getNextUrl(eventId))
}
}
break
}
}
}
}
}
void moveToNextState() {
List chain = Arrays.asList(JobState.INIT, JobState.STARTED, JobState.DETECTED, JobState.DELIVERED, JobState.RESPONDED, JobState.SUCCEEDED, JobState.ENDED);
int pos = chain.indexOf(state);
if (pos != chain.last()) {
state = chain.get(pos + 1)
}
}
void moveToFailed(String url) {
state = JobState.FAILED
logger.error("Request failed (${url}), will send ${state.getNextUrl(eventId)}")
jobs.add(state.getNextUrl(eventId))
}
}
/**
* return a map, including an entry for HttpStatus
*/
Map handleRequest(Request request) {
return new RequestHandler(request).handleRequest()
}