|
| 2 |
|
| 3 |
import org.apache.mina.common.IoSession |
| 4 |
import java.util.concurrent.BlockingQueue |
| 5 |
import java.util.concurrent.Executors |
| 6 |
import java.util.concurrent.ExecutorService |
| 7 |
import java.util.concurrent.Future |
| 8 |
import java.util.concurrent.Callable |
| 9 |
import java.util.concurrent.ExecutionException |
| 10 |
import java.util.concurrent.TimeoutException |
| 11 |
import java.util.concurrent.TimeUnit |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class Session { |
| 16 |
private IoSession session |
| 17 |
private FSEventHandler handler |
| 18 |
private BlockingQueue<String> msgQ |
| 19 |
private final ExecutorService executor = Executors.newSingleThreadExecutor() |
| 20 |
private final static long DEFAULT_TIMEOUT = 5000 |
| 21 |
|
| 22 |
def data |
| 23 |
|
| 24 |
def Session(IoSession s, BlockingQueue q) { |
| 25 |
session = s |
| 26 |
msgQ = q |
| 27 |
} |
| 28 |
|
| 29 |
private def executeAndWait(Closure task, long timeout=0) { |
| 30 |
Future <CommandResult> f = executor.submit(task as Callable) |
| 31 |
def result |
| 32 |
def boolean success = false |
| 33 |
try { |
| 34 |
if (timeout != 0) { |
| 35 |
result = f.get(timeout, TimeUnit.MILLISECONDS) |
| 36 |
} else { |
| 37 |
result = f.get() |
| 38 |
} |
| 39 |
if (result.code == CommandResult.OK) data = result.data |
| 40 |
} catch (ExecutionException e) { |
| 41 |
// Should log here |
| 42 |
} catch (TimeoutException e) { |
| 43 |
f.cancel(true) |
| 44 |
} |
| 45 |
|
| 46 |
return result |
| 47 |
} |
| 48 |
|
| 49 |
def answer() { |
| 50 |
def task = { |
| 51 |
def done = false |
| 52 |
def r = new CommandResult() |
| 53 |
sendMessage("answer") |
| 54 |
while (! done) { |
| 55 |
def m = msgQ.take() |
| 56 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") && (m?.Application == "answer")) { |
| 57 |
done = true |
| 58 |
r.code = CommandResult.OK |
| 59 |
r.data = m |
| 60 |
} |
| 61 |
} |
| 62 |
return r |
| 63 |
} |
| 64 |
executeAndWait(task, DEFAULT_TIMEOUT) |
| 65 |
} |
| 66 |
|
| 67 |
def unset(var) { |
| 68 |
def task = { |
| 69 |
def done = false |
| 70 |
def r = new CommandResult() |
| 71 |
sendMessage("unset", var) |
| 72 |
while (! done) { |
| 73 |
def m = msgQ.take() |
| 74 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 75 |
&& (m?.Application == "unset") |
| 76 |
&& (m?.ApplicationData == var)) { |
| 77 |
done = true |
| 78 |
r.code = CommandResult.OK |
| 79 |
r.data = m |
| 80 |
} |
| 81 |
} |
| 82 |
return r |
| 83 |
} |
| 84 |
executeAndWait(task, DEFAULT_TIMEOUT) |
| 85 |
} |
| 86 |
|
| 87 |
def queueDtmf(dtmfs) { |
| 88 |
def task = { |
| 89 |
def done = false |
| 90 |
def r = new CommandResult() |
| 91 |
sendMessage("queue_dtmf", dtmfs) |
| 92 |
while (! done) { |
| 93 |
def m = msgQ.take() |
| 94 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 95 |
&& (m?.Application == "queue_dtmf") |
| 96 |
&& (m?.ApplicationData == dtmfs)) { |
| 97 |
done = true |
| 98 |
r.code = CommandResult.OK |
| 99 |
r.data = m |
| 100 |
} |
| 101 |
} |
| 102 |
return r |
| 103 |
} |
| 104 |
executeAndWait(task, DEFAULT_TIMEOUT) |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
def hangup() { |
| 109 |
def task = { |
| 110 |
def done = false |
| 111 |
def r = new CommandResult() |
| 112 |
sendMessage("hangup") |
| 113 |
while (! done) { |
| 114 |
def m = msgQ.take() |
| 115 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 116 |
&& (m?.Application == "queue_dtmf") |
| 117 |
&& (m?.ApplicationData == dtmfs)) { |
| 118 |
done = true |
| 119 |
r.code = CommandResult.OK |
| 120 |
r.data = m |
| 121 |
} |
| 122 |
} |
| 123 |
return r |
| 124 |
} |
| 125 |
executeAndWait(task, DEFAULT_TIMEOUT) |
| 126 |
} |
| 127 |
*/ |
| 128 |
|
| 129 |
def setVariable(String var, String value) { |
| 130 |
def task = { |
| 131 |
def done = false |
| 132 |
def r = new CommandResult() |
| 133 |
sendMessage("set", "${var}=${value}") |
| 134 |
while (! done) { |
| 135 |
def m = msgQ.take() |
| 136 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 137 |
&& (m?.Application == "set") |
| 138 |
&& (m?.ApplicationData == "${var}=${value}")) { |
| 139 |
done = true |
| 140 |
r.code = CommandResult.OK |
| 141 |
r.data = m |
| 142 |
} |
| 143 |
} |
| 144 |
return r |
| 145 |
} |
| 146 |
executeAndWait(task, DEFAULT_TIMEOUT) |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
def export(String var) { |
| 151 |
set("export_vars", var) |
| 152 |
} |
| 153 |
|
| 154 |
def bridge(String number) { |
| 155 |
def task = { |
| 156 |
def done = false |
| 157 |
def r = new CommandResult() |
| 158 |
sendMessage("bridge", number) |
| 159 |
while (! done) { |
| 160 |
def m = msgQ.take() |
| 161 |
// println m |
| 162 |
if (((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 163 |
&& (m?.Application == "bridge") |
| 164 |
&& (m?.ApplicationData == number)) || |
| 165 |
((m?.event?.Name == "CHANNEL_UNBRIDGE") |
| 166 |
&& (m?.variable.bridge_channel == number)) || (m?.SESSIONCLOSED == "true")) { |
| 167 |
done = true |
| 168 |
r.code = CommandResult.OK |
| 169 |
r.data = m |
| 170 |
} |
| 171 |
} |
| 172 |
return r |
| 173 |
} |
| 174 |
executeAndWait(task) |
| 175 |
} |
| 176 |
|
| 177 |
def promptForDigits(int min, int max, String soundFile, String variableName, long timeout, String terminator) { |
| 178 |
def task = { |
| 179 |
def done = false |
| 180 |
def r = new CommandResult() |
| 181 |
def appData = "${min} ${max} ${soundFile} ${variableName} ${timeout} ${terminator}" |
| 182 |
sendMessage("read", appData, true) |
| 183 |
while (! done) { |
| 184 |
def m = msgQ.take() |
| 185 |
if ((m?.event?.Name == "CHANNEL_EXECUTE_COMPLETE") |
| 186 |
&& (m?.Application == "read") |
| 187 |
&& (m?.ApplicationData == appData)) { |
| 188 |
done = true |
| 189 |
r.code = CommandResult.OK |
| 190 |
r.data = m |
| 191 |
} |
| 192 |
} |
| 193 |
return r |
| 194 |
} |
| 195 |
executeAndWait(task, timeout) |
| 196 |
} |
| 197 |
|
| 198 |
def originate(String url) { |
| 199 |
sendMessage("originate", url, true) |
| 200 |
} |
| 201 |
|
| 202 |
def sleep(int sec) { |
| 203 |
sendMessage("sleep", new Integer(sec*1000).toString()) |
| 204 |
} |
| 205 |
|
| 206 |
def say(String phrase) { |
| 207 |
sendMessage("phrase", "spell,$phrase") |
| 208 |
} |
| 209 |
|
| 210 |
def script() { |
| 211 |
return "jeprox" |
| 212 |
} |
| 213 |
|
| 214 |
private def sendMessage(String app, String arg=null, boolean event_lock=false) { |
| 215 |
String msg = "sendmsg\ncall-command: execute\nexecute-app-name: ${app}" |
| 216 |
if (arg) { |
| 217 |
msg += "\nexecute-app-arg: ${arg}" |
| 218 |
} |
| 219 |
if (event_lock) { |
| 220 |
msg += "\nevent-lock: ${event_lock}" |
| 221 |
} |
| 222 |
System.out.println(msg) |
| 223 |
session.write("$msg\n\n") |
| 224 |
} |
| 225 |
} |