From 7ae1f870faebb65e9ee3a064fc43ef2fc6955a84 Mon Sep 17 00:00:00 2001 From: Comment Date: Thu, 29 Nov 2012 19:13:19 +0000 Subject: work on asterisk plugin --- ofAsterisk/src/Asterisk.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++ ofAsterisk/src/Asterisk.h | 64 +++++++++++++++++++++++++++++++++ ofAsterisk/src/main.cpp | 16 +++++++++ ofAsterisk/src/testApp.cpp | 69 ++++++++++++++++++++++++++++++++++++ ofAsterisk/src/testApp.h | 25 +++++++++++++ 5 files changed, 260 insertions(+) create mode 100755 ofAsterisk/src/Asterisk.cpp create mode 100755 ofAsterisk/src/Asterisk.h create mode 100755 ofAsterisk/src/main.cpp create mode 100755 ofAsterisk/src/testApp.cpp create mode 100755 ofAsterisk/src/testApp.h (limited to 'ofAsterisk/src') diff --git a/ofAsterisk/src/Asterisk.cpp b/ofAsterisk/src/Asterisk.cpp new file mode 100755 index 0000000..ae85269 --- /dev/null +++ b/ofAsterisk/src/Asterisk.cpp @@ -0,0 +1,86 @@ +#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 +} diff --git a/ofAsterisk/src/Asterisk.h b/ofAsterisk/src/Asterisk.h new file mode 100755 index 0000000..c9ce864 --- /dev/null +++ b/ofAsterisk/src/Asterisk.h @@ -0,0 +1,64 @@ +#ifndef ASTERISK_H +#define ASTERISK_H + +#include "ofMain.h" +#include "ofxNetwork.h" + +#define IDLE 0 +#define WAITING 1 +#define STARTING 2 +#define PLAYING 3 + +// http://stackoverflow.com/questions/1735781/non-blocking-pipe-using-popen + +/* + +Commands for game to control PBX + +Game ready + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "devstate change Custom:GAME NOT_INUSE"' + +change daily access 4 digit code + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "database put GAME passcode 1234"' + + + +Set status code for current game + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "database put GAME statuscode 1234"' + +Gameover Hangup Call + +ssh 10.10.10.1 'sudo /usr/sbin/asterisk -rx "hangup request Local/9999@default-00000039;2" + +Local/9999@default-00000039;2 is a channel identifier and will be send to game when AGI script connects to game controller. The channel ID is unique for each call + + +can we have a message that someone has joined the queue? +can we have a message if someone hangs up? +*/ + +class Asterisk +{ + public: + Asterisk(); + virtual ~Asterisk(); + void startGame(); + void endGame(string score); + int update(); + void cmd(string s); + + int state; + + protected: + private: + int startTime; + FILE *file; + int filenum; + ofxUDPManager udpConnection; + string playerCode; +}; + +#endif // ASTERISK_H diff --git a/ofAsterisk/src/main.cpp b/ofAsterisk/src/main.cpp new file mode 100755 index 0000000..6042c32 --- /dev/null +++ b/ofAsterisk/src/main.cpp @@ -0,0 +1,16 @@ +#include "ofMain.h" +#include "testApp.h" +#include "ofAppGlutWindow.h" + +//======================================================================== +int main( ){ + + ofAppGlutWindow window; + ofSetupOpenGL(&window, 100,100, OF_WINDOW); // <-------- setup the GL context + + // this kicks off the running of my app + // can be OF_WINDOW or OF_FULLSCREEN + // pass in width and height too: + ofRunApp( new testApp()); + +} diff --git a/ofAsterisk/src/testApp.cpp b/ofAsterisk/src/testApp.cpp new file mode 100755 index 0000000..a69a084 --- /dev/null +++ b/ofAsterisk/src/testApp.cpp @@ -0,0 +1,69 @@ +#include "testApp.h" + +//-------------------------------------------------------------- +void testApp::setup(){ + +} + +//-------------------------------------------------------------- +void testApp::update(){ + int ret=game.update(); +} + +//-------------------------------------------------------------- +void testApp::draw(){ + +} + +//-------------------------------------------------------------- +void testApp::keyPressed(int key){ + switch (key) { + case 's': + game.startGame(); + break; + case 'e': + game.endGame("GOOD"); + break; + } + +} + +//-------------------------------------------------------------- +void testApp::keyReleased(int key){ + +} + +//-------------------------------------------------------------- +void testApp::mouseMoved(int x, int y ){ + +} + +//-------------------------------------------------------------- +void testApp::mouseDragged(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mousePressed(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::mouseReleased(int x, int y, int button){ + +} + +//-------------------------------------------------------------- +void testApp::windowResized(int w, int h){ + +} + +//-------------------------------------------------------------- +void testApp::gotMessage(ofMessage msg){ + +} + +//-------------------------------------------------------------- +void testApp::dragEvent(ofDragInfo dragInfo){ + +} \ No newline at end of file diff --git a/ofAsterisk/src/testApp.h b/ofAsterisk/src/testApp.h new file mode 100755 index 0000000..84c984c --- /dev/null +++ b/ofAsterisk/src/testApp.h @@ -0,0 +1,25 @@ +#pragma once + +#include "ofMain.h" +#include "Asterisk.h" + +class testApp : public ofBaseApp{ + + public: + void setup(); + void update(); + void draw(); + + void keyPressed (int key); + void keyReleased(int key); + void mouseMoved(int x, int y ); + void mouseDragged(int x, int y, int button); + void mousePressed(int x, int y, int button); + void mouseReleased(int x, int y, int button); + void windowResized(int w, int h); + void dragEvent(ofDragInfo dragInfo); + void gotMessage(ofMessage msg); + + Asterisk game; + +}; -- cgit v1.2.3