From 8fe8572729f186269b02ac2bb149fe9a25671cae Mon Sep 17 00:00:00 2001 From: Comment Date: Wed, 5 Dec 2012 22:10:04 +0000 Subject: asterisk with thread --- ofAsterisk/src/Asterisk.cpp | 103 +++++++++++++++++++++++++++----------------- ofAsterisk/src/Asterisk.h | 22 ++++++++-- ofAsterisk/src/testApp.cpp | 10 ++--- vfg/src/Asterisk.cpp | 86 +++++++++++++++++++++--------------- vfg/src/Asterisk.h | 22 +++++++--- vfg/src/music.cpp | 9 ++-- vfg/src/testApp.cpp | 35 +++++++-------- vfg/src/testApp.h | 24 +++++------ 8 files changed, 186 insertions(+), 125 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 &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 (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 lines; - split(msg,'\n',lines); - queued=0; - for (int i=4;i3) { //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 +} 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;ifirst]=new flake(notes.begin()->second->num,notes.begin()->second->velocity,notes.begin()->second->duration); flakes[notes.begin()->first]->puppet=snowflakes[notemap(lastNote->num)]; for (iter = notes.begin(); iter != notes.end(); iter++) { - float songPos=((float)iter->first)/songDuration; - if ((levels->nextLevelTime(iter->first)>2000)&&((notemap(iter->second->num)!=notemap(lastNote->num))||(iter->first-lastTime>((songPos*threshEnd)+((1.0f-songPos)*threshStart))))) { + float songPos=((float)iter->first)/songDuration; + //((notemap(iter->second->num)!=notemap(lastNote->num))|| + if ((levels->nextLevelTime(iter->first)>2000)&&(iter->first-lastTime>((songPos*threshEnd)+((1.0f-songPos)*threshStart)))) { flakes[iter->first]=new flake(iter->second->num,iter->second->velocity,iter->second->duration); flakes[iter->first]->puppet=snowflakes[notemap(iter->second->num)]; lastNote=iter->second; @@ -235,7 +236,7 @@ void musicscore::drawFlakes(float hOffs,levelscore *levels,float scale) { missedLast=!iter->second->activated; } //at this point missed points to the latest unactivated flake in the level if there is one or else flakes.end() - + if ((missed!=flakes.end())&&(missedFlake!=missed)) { missedFlake=missed; missedNote=notemap(missed->second->num); diff --git a/vfg/src/testApp.cpp b/vfg/src/testApp.cpp index 7c596d0..986673c 100755 --- a/vfg/src/testApp.cpp +++ b/vfg/src/testApp.cpp @@ -32,9 +32,8 @@ void guiWindow::windowMoved(int x,int y){ //---------------------------------------------------------------------- void testApp::setup(){ - - game.setup("1122"); - gamepoll=0; + + game.setup("1122",2000); //Asterisk.cpp thread polls server every 2000ms songs.push_back(new song("WeWishYouAMerryChistmas_v.2.1/VODA_MUS_WEWISHU_Backing_v.2.1.mp3","WeWishYouAMerryChistmas_v.2.1/VODA_MUS_WEWISHU_Lead_v.2.1.mp3","VODA_MUS_WEWISHU_Midi_v.2.2.xml","Lyrics_WeWishYou.2.1.xml","Levels_WeWishYou.2.1.xml")); songs.push_back(new song("DeckTheHalls_v.4.0/VODA_MUS_DeckTheHalls-Backing_v.4.0.mp3","DeckTheHalls_v.4.0/VODA_MUS_DeckTheHalls-Lead_v.4.0.mp3","VODA_MIDI_DeckTheHalls-v.4.0.xml","Lyrics_DeckTheHalls.4.0.xml","Levels_DeckTheHalls.4.0.xml")); @@ -52,7 +51,7 @@ void testApp::setup(){ penguins[1].load("Penguin-Purple.xml"); penguins[2].load("Penguin-Green.xml"); - + // temp save time for (int i=0;i<3;i++) raccoons.push_back(Animal()); raccoons[0].load("Raccoon-Blue.xml"); @@ -65,7 +64,7 @@ void testApp::setup(){ tags.push_back(new Tag("Tags/TAG_Level-2.png",3000,0.5,0)); tags.push_back(new Tag("Tags/TAG_Level-3.png",3000,0.5,0)); tags.push_back(new Tag("Tags/TAG_Level-4.png",3000,0.5,0)); - tags.push_back(new Tag("Tags/PreGame-Practice.png",3000,0.5,0)); + tags.push_back(new Tag("Tags/PreGame-Practice.png",6000,0.5,0.16667)); tags.push_back(new Tag("Tags/Game-Over.png",3000,0.5,0)); tags.push_back(new Tag("Tags/Thanks-NEW.png",3000,0.5,0)); @@ -79,7 +78,7 @@ void testApp::setup(){ banner.setAnchorPercent(0.5,0.0); banner.setFrameRate(50); banner.play(); - + wirebirds.load("Birds-with-Wire/Birds-with-Wire_%05i.png",125); wirebirds.setAnchorPercent(0.5,0.5); wirebirds.setFrameRate(50); @@ -94,7 +93,7 @@ void testApp::setup(){ //release: commented out: 436m 256m //508fr 1.4s load in use: 836m 443m - + //cd /home/tim/workspace/VFxmas/vfg/bin/data //mount -t tmpfs -o size=64M tmpfs /RAM //cp *.m* RAM @@ -110,7 +109,7 @@ void testApp::setup(){ background.loadMovie("RAM/Background_v3.mp4"); background.setLoopState(OF_LOOP_NORMAL); background.play(); - + backgroundmusic.loadSound("VODA_MUS_Pre-Game-Music_v.1.1.mp3"); backgroundmusic.setLoop(true); backgroundmusic.play(); @@ -139,6 +138,7 @@ void testApp::setup(){ gui.add(ts.setup("timescale",timescale,0,10000,255)); keyThresh=1500; gui.add(kT.setup("keythresh",keyThresh,0,4000,255)); + gui.add(showWire.setup("birdwire",true)); ofxFenster* win=ofxFensterManager::get()->createFenster(0, 0, 200, 400, OF_WINDOW); win->setWindowTitle("config"); @@ -176,10 +176,6 @@ void testApp::exit(){ //-------------------------------------------------------------- void testApp::update(){ int ret=game.update(); - if (ofGetElapsedTimeMillis()-gamepoll>5000) { - game.requestStatus(); - gamepoll=ofGetElapsedTimeMillis(); - } if (ret==1000) { activatePractice(); } @@ -262,7 +258,7 @@ void testApp::draw(){ float scale=ofGetHeight()/1080.0f; float bannerscale,aspect,wOffs; //this old chestnut float segamt=((float)(ofGetElapsedTimeMillis()-segmentStartTime)*2.0f)/fadelength; //same variable used in all segments - + switch(gamestate) { case GAME_STARTINGINTRO: case GAME_INTRO: @@ -335,11 +331,11 @@ void testApp::draw(){ wOffs=(ofGetHeight()-(ofGetWidth()*aspect))*0.5; advert.draw(0,wOffs,ofGetWidth(),ofGetWidth()*aspect); ofDisableAlphaBlending(); //may have been turned on during intro - + break; - + } - + //draw game action switch(gamestate) { case GAME_STARTINGADVERT: @@ -376,7 +372,7 @@ void testApp::draw(){ if (missed>-1) (*playanimal)[missed].playNow("Shudder"); int hit=practiceSong.hitNote(); if (hit>-1) (*playanimal)[hit].play("Catch"); - + } for (int i=0;i<3;i++) (*playanimal)[i].draw(hOffs+(gridX[i+1]*ofGetHeight()),gridY[0]*ofGetHeight(),scale); if (songs[currentsong]->isPlaying) { @@ -423,7 +419,7 @@ void testApp::draw(){ case GAME_PRACTICE: ofEnableAlphaBlending(); bannerscale=scale*(1.0f-(max(0.0,min(((float)(ofGetElapsedTimeMillis()-segmentStartTime))*.002,1.0))*0.325)); - wirebirds.draw(ofGetWidth()/2,ofGetHeight()*0.65,scale); + wirebirds.draw(ofGetWidth()/2,ofGetHeight()*0.6,scale); segamt=1.0; ofEnableAlphaBlending(); banner.draw(ofGetWidth()*0.5,0,bannerscale); @@ -446,6 +442,7 @@ void testApp::draw(){ break; case GAME_STARTPLAYING: case GAME_PLAYING: + if (showWire) wirebirds.draw(ofGetWidth()/2,ofGetHeight()*0.6,scale); bannerscale=scale*0.675; ofEnableAlphaBlending(); banner.draw(ofGetWidth()*0.5,0,bannerscale); @@ -530,7 +527,7 @@ void testApp::activatePractice(){ practiceSong.setTimeframe(timescale); practiceSong.setFlakeThresh(threshStart,threshEnd); practiceSong.setKeyThresh(keyThresh); - practiceSong.play(); + practiceSong.preRoll(5000); tags[5]->play(); } void testApp::switchAnimals(){ diff --git a/vfg/src/testApp.h b/vfg/src/testApp.h index 4852073..d4a9627 100755 --- a/vfg/src/testApp.h +++ b/vfg/src/testApp.h @@ -71,12 +71,12 @@ class testApp : public ofxFensterListener{ void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); - + void drawBackgroundLayers(); void activateGame(); void activatePractice(); void switchAnimals(); - + ofVideoPlayer intro; ofVideoPlayer advert; ofSoundPlayer backgroundmusic; @@ -87,38 +87,37 @@ class testApp : public ofxFensterListener{ vector songs; song practiceSong; - + vector penguins; vector raccoons; - + //vector > animals; - + //vector playanimals; vector* playanimal; vector tags; - + vector logos; - + puppetSprite banner; puppetSprite wirebirds; - + Billboard* lyricspanel; Puppet testpenguin; int currentlevel; Asterisk game; - int gamepoll; bool showFPS; bool showVis; bool fullscreenoutput; - + int currentsong,nextsong,gamestate,segmentStartTime; - + float fadelength; - + guiWindow *guiWin; ofxPanel gui; ofxIntSlider tS; @@ -129,6 +128,7 @@ class testApp : public ofxFensterListener{ ofxParameter timescale; ofxIntSlider kT; ofxParameter keyThresh; + ofxToggle showWire; }; -- cgit v1.2.3