From 8fe8572729f186269b02ac2bb149fe9a25671cae Mon Sep 17 00:00:00 2001 From: Comment Date: Wed, 5 Dec 2012 22:10:04 +0000 Subject: asterisk with thread --- vfg/src/Asterisk.cpp | 86 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 34 deletions(-) (limited to 'vfg/src/Asterisk.cpp') diff --git a/vfg/src/Asterisk.cpp b/vfg/src/Asterisk.cpp index 12b3d92..b78678e 100755 --- a/vfg/src/Asterisk.cpp +++ b/vfg/src/Asterisk.cpp @@ -14,17 +14,58 @@ std::vector &split(const std::string &s, char delim, std::vector lines; + split(msg,'\n',lines); + queued=0; + for (int i=4;i0) startThread(true, false); // blocking, verbose } void Asterisk::startGame(){ if (!isPlaying&&queued) { @@ -45,57 +86,39 @@ void Asterisk::endGame(string score){ } } void Asterisk::cmd(string s) { + //non blocking command for anync messages 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; + fcntl(filenum, F_SETFL, O_NONBLOCK); } int Asterisk::update(){ //capture stdin response from popen - if (state==WAITING||state==STARTING||state==IDLE) { - char buf[1000]; + if (state==WAITING||state==STARTING) { + char buf[100]; ssize_t r = read(filenum, buf, 1000); //if (r == -1 && errno == EAGAIN) //no data yet - //else + //else if (r > 0) { pclose(file); string msg=string(buf); state=IDLE; //received data is always a command ACKNOWLEDGEMENT + //can never issue a new one until last one returns? if (msg.compare(0,8,"Changing")==0) { printf("player dequeued\n"); + state==PLAYING; isPlaying=true; } - else if (msg.compare(0,5,"gameQ")==0) { - vector lines; - split(msg,'\n',lines); - queued=0; - for (int i=4;i