summaryrefslogtreecommitdiff
path: root/ofAsterisk/src
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2012-12-05 22:10:04 +0000
committerComment <tim@gray.(none)>2012-12-05 22:10:04 +0000
commit8fe8572729f186269b02ac2bb149fe9a25671cae (patch)
treefb30ccab57f4c3ed50c6fc76fd28db4c63255cc0 /ofAsterisk/src
parent7e99d4291172f4dadb59b81cf525817dd8a210e5 (diff)
asterisk with thread
Diffstat (limited to 'ofAsterisk/src')
-rwxr-xr-xofAsterisk/src/Asterisk.cpp103
-rwxr-xr-xofAsterisk/src/Asterisk.h22
-rwxr-xr-xofAsterisk/src/testApp.cpp10
3 files changed, 84 insertions, 51 deletions
diff --git a/ofAsterisk/src/Asterisk.cpp b/ofAsterisk/src/Asterisk.cpp
index 7c4f050..b78678e 100755
--- a/ofAsterisk/src/Asterisk.cpp
+++ b/ofAsterisk/src/Asterisk.cpp
@@ -14,89 +14,117 @@ std::vector<std::string> &split(const std::string &s, char delim, std::vector<st
}
-Asterisk::Asterisk(string passcode)
+Asterisk::~Asterisk()
{
+ stopThread();
+ pclose(file);
+}
+
+/*
+Use a thread to track status with a blocking popen()
+
+
+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::threadedFunction(){
+ while( isThreadRunning() != 0 ){
+ FILE *f = popen("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"queue show gameQ\"'", "re");
+ int fn=fileno(f);
+ char buf[1000];
+ ssize_t r = read(fn, buf, 1000);
+ pclose(f);
+ string msg=string(buf);
+ //received data is always a command ACKNOWLEDGEMENT
+ 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("status err: %s\n",buf);
+ }
+
+ ofSleepMillis(statusPollMillis);
+ }
+}
+
+void Asterisk::setup(string passcode,int millis){
udpConnection.Create();
udpConnection.Bind(5000);
udpConnection.SetNonBlocking(true);
cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME passcode "+passcode+"\"'");
state=WAITING; //for acknowledge
+ isPlaying=false;
+ statusPollMillis=millis;
+ if (statusPollMillis>0) startThread(true, false); // blocking, verbose
}
-
void Asterisk::startGame(){
- if (state==IDLE) {
+ 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) {
+ //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) {
- char buf[1000];
+ 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
- if (msg.compare("Changing GAME to NOT_INUSE")==0) {
+ //can never issue a new one until last one returns?
+ if (msg.compare(0,8,"Changing")==0) {
printf("player dequeued\n");
- state=PLAYING;
- }
- 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++;
- }
+ state==PLAYING;
+ isPlaying=true;
}
else {
printf("stdin says: %s\n",buf);
- state=IDLE;
+
}
}
}
-
+
//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") {
@@ -114,8 +142,3 @@ int Asterisk::update(){
else return 0;
}
-Asterisk::~Asterisk()
-{
- pclose(file);
- //dtor
-}
diff --git a/ofAsterisk/src/Asterisk.h b/ofAsterisk/src/Asterisk.h
index d3e3786..5033a5c 100755
--- a/ofAsterisk/src/Asterisk.h
+++ b/ofAsterisk/src/Asterisk.h
@@ -10,26 +10,40 @@
#define PLAYING 3
-class Asterisk
+class Asterisk: public ofThread
{
public:
- Asterisk(string passcode="1111");
+ Asterisk(){
+ }
+
+ void start(){
+
+ }
+ void stop(){
+ stopThread();
+ }
+
+ //--------------------------
+
+ void setup(string passcode="1111",int millis=1000);
virtual ~Asterisk();
void startGame();
void endGame(string score);
int update();
- void requestStatus();
int state;
+ bool isPlaying;
int queued;
protected:
- void cmd(string s);
+ void cmd(string s);
+ void threadedFunction();
private:
int startTime;
FILE *file;
int filenum;
ofxUDPManager udpConnection;
string playerCode;
+ int statusPollMillis;
};
diff --git a/ofAsterisk/src/testApp.cpp b/ofAsterisk/src/testApp.cpp
index 1a172ee..772a9b6 100755
--- a/ofAsterisk/src/testApp.cpp
+++ b/ofAsterisk/src/testApp.cpp
@@ -2,16 +2,12 @@
//--------------------------------------------------------------
void testApp::setup(){
- queuecheck=0;
+ game.setup("1122",2000);
}
//--------------------------------------------------------------
void testApp::update(){
int ret=game.update();
- if (ofGetElapsedTimeMillis()-queuecheck>5000) {
- game.requestStatus();
- queuecheck=ofGetElapsedTimeMillis();
- }
}
//--------------------------------------------------------------
@@ -70,6 +66,6 @@ void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
-void testApp::dragEvent(ofDragInfo dragInfo){
+void testApp::dragEvent(ofDragInfo dragInfo){
-} \ No newline at end of file
+}