summaryrefslogtreecommitdiff
path: root/vfg/src/Asterisk.cpp
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2012-12-04 14:53:44 +0000
committerComment <tim@gray.(none)>2012-12-04 14:53:44 +0000
commit7e99d4291172f4dadb59b81cf525817dd8a210e5 (patch)
tree75fb43c04330250a25f745228e59a30902d783d0 /vfg/src/Asterisk.cpp
parentf7bee21ada14851da8626a3784821ded25333fe6 (diff)
dress rehearsal version
Diffstat (limited to 'vfg/src/Asterisk.cpp')
-rwxr-xr-xvfg/src/Asterisk.cpp92
1 files changed, 66 insertions, 26 deletions
diff --git a/vfg/src/Asterisk.cpp b/vfg/src/Asterisk.cpp
index 3f845f0..12b3d92 100755
--- a/vfg/src/Asterisk.cpp
+++ b/vfg/src/Asterisk.cpp
@@ -4,64 +4,104 @@
//there is no notification that there is someone in the queue
//but there is a status message you can check periodically...
+std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
+ std::stringstream ss(s);
+ std::string item;
+ while(std::getline(ss, item, delim)) {
+ elems.push_back(item);
+ }
+ return elems;
+}
+
+
Asterisk::Asterisk()
{
+}
+
+void Asterisk::setup(string passcode){
udpConnection.Create();
udpConnection.Bind(5000);
udpConnection.SetNonBlocking(true);
- cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME passcode 1122\"'");
- state=WAITING;
- printf("Asterisk: created socket and connected to server\n");
+ cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME passcode "+passcode+"\"'");
+ state=WAITING; //for acknowledge
+ isPlaying=false;
}
-
void Asterisk::startGame(){
- if (state!=PLAYING) {
+ if (!isPlaying&&queued) {
cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"devstate change Custom:GAME NOT_INUSE\"'");
printf("Asterisk: attempting to dequeue\n");
- state=STARTING;
+ state=WAITING;
}
}
void Asterisk::endGame(string score){
- if (state==PLAYING) {
+ printf("Asterisk: request hangup %s\n",playerCode.c_str());
+ if (isPlaying) {
cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME statuscode "+score+"\"'");
string emsg="ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"hangup request "+playerCode+"\"'";
- printf("%s\n",emsg.c_str());
cmd(emsg);
printf("Asterisk: hanging up %s\n",playerCode.c_str());
state=WAITING;
+ isPlaying=false;
}
}
void Asterisk::cmd(string s) {
- file = popen(s.c_str(), "r");
- int filenum=fileno(file);
- //fcntl(filenum, F_SETFL, O_NONBLOCK); //doesn't seem to work, still pauses
- //pclose(file); //discard the pipe THIS IS BLOCKING
+ file = popen(s.c_str(), "re");
+ filenum=fileno(file);
+ fcntl(filenum, F_SETFL, O_NONBLOCK);
}
+/*
+gameQ has 1 calls (max unlimited) in 'rrmemory' strategy (8s holdtime, 50s talktime), W:0, C:38, A:26, SL:36.8% within 0s
+ Members:
+ Game AGI (Local/9999@default) with penalty 1 (In use) has taken 31 calls (last was 5333 secs ago)
+ Callers:
+ 1. SIP/sip_proxy-00000045 (wait: 0:06, prio: 0)
+*/
+
+void Asterisk::requestStatus(){
+ cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"queue show gameQ\"'");
+ state=WAITING;
+}
+
+
int Asterisk::update(){
- //how to capture stdin response from popen
- /*
- if (state==WAITING||state==STARTING) {
- char buf[100];
- ssize_t r = read(filenum, buf, 100);
+ //capture stdin response from popen
+ if (state==WAITING||state==STARTING||state==IDLE) {
+ char buf[1000];
+ ssize_t r = read(filenum, buf, 1000);
//if (r == -1 && errno == EAGAIN)
//no data yet
- //else
- if (r > 0)
- //received data
- printf("%s\n",buf);
+ //else
+ if (r > 0) {
pclose(file);
+ string msg=string(buf);
state=IDLE;
- ///else
- //pipe closed
+ //received data is always a command ACKNOWLEDGEMENT
+ if (msg.compare(0,8,"Changing")==0) {
+ printf("player dequeued\n");
+ isPlaying=true;
+ }
+ else if (msg.compare(0,5,"gameQ")==0) {
+ vector<std::string> lines;
+ split(msg,'\n',lines);
+ queued=0;
+ for (int i=4;i<lines.size();i++) {
+ if (lines[i].compare(0,6," ")==0&&lines[i].compare(7,1,".")==0) queued++;
+ }
+ }
+ else {
+ printf("stdin says: %s\n",buf);
+
+ }
+ }
}
- */
- //check messages and pipes, returns new keypresses
+
+ //check udp messages, return new keypresses
char udpMessage[10000];
udpConnection.Receive(udpMessage,10000);
string msg=udpMessage;
if(msg!=""){
- printf("Asterisk: %s\n",msg.c_str());
+ //printf("Asterisk: %s\n",msg.c_str());
if (msg.length()>3) {
//printf("status msg: %s\n",msg.c_str());
if (msg.substr(0,5)=="Local") {