#include "Asterisk.h" //there is no notification that there is someone in the queue //but there is a status message you can check periodically... Asterisk::Asterisk() { udpConnection.Create(); udpConnection.Bind(5000); udpConnection.SetNonBlocking(true); cmd("ssh 80.93.22.22 'sudo /usr/sbin/asterisk -rx \"database put GAME passcode 1234\"'"); state=WAITING; printf("Asterisk: created socket and connected to server\n"); } void Asterisk::startGame(){ if (state!=PLAYING) { 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; } } void Asterisk::endGame(string score){ if (state==PLAYING) { 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; } } 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 } 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); //if (r == -1 && errno == EAGAIN) //no data yet //else if (r > 0) //received data printf("%s\n",buf); pclose(file); state=IDLE; ///else //pipe closed } */ //check messages and pipes, returns new keypresses char udpMessage[10000]; udpConnection.Receive(udpMessage,10000); string msg=udpMessage; if(msg!=""){ 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") { state=PLAYING; playerCode=msg.substr(0,msg.length()-1); printf("Asterisk: game started: code %s\n",playerCode.c_str()); return 1000; } return 0; } else { return ofToInt(msg); } } else return 0; } Asterisk::~Asterisk() { pclose(file); //dtor }