Created
July 11, 2012 11:52
-
-
Save joernroeder/3089887 to your computer and use it in GitHub Desktop.
AppConnector.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* AppConnector.cpp | |
* appConnector | |
* | |
* Created by Jonathan Pirnay & Jörn Röder. | |
* Copyright 2012 jj. All rights reserved. | |
* | |
*/ | |
#include "AppConnector.h" | |
void AppConnector::init(string appKey) { | |
handler.init(appKey); | |
_publishDescriptions = json_object(); | |
} | |
void AppConnector::setup() { | |
json_t* data = handler.setup(); | |
checkCurrentAppVersion(JSONParser.getValueS(data, "ofVersion")); | |
if (_isDebug == true) { | |
loaded(); | |
} | |
else { | |
interval = ofToInt(JSONParser.getValueS(data, "interval", "0")); | |
ofSleepMillis(getDelay(JSONParser.getValueI(data, "timestamp", 0))); | |
loaded(); | |
} | |
json_decref(data); | |
} | |
bool AppConnector::checkCurrentAppVersion(string version) { | |
float v = ofToFloat(version); | |
if (v > APPCONNECTOR_VERSION) { | |
cout << "\n\n------------------------------------------------\n" << endl; | |
cout << "You're using an old version of the AppConnector!" << endl; | |
cout << "Go to http://appconnector.joernroeder.de/download to get the latest version." << endl; | |
cout << "\n------------------------------------------------\n\n" << endl; | |
} | |
} | |
void AppConnector::start() { | |
_running = true; | |
printf("Starting AppConnector Sync (will execute every %i milliseconds.)", interval); | |
startThread(true, false); | |
} | |
void AppConnector::setDebugResponse(bool val) { | |
handler.setDebugResponse(val); | |
} | |
bool AppConnector::getDebugResponse() { | |
return handler.getDebugResponse(); | |
} | |
void AppConnector::setDebug(bool val) { | |
_isDebug = val; | |
} | |
bool AppConnector::isDebug() { | |
return _isDebug; | |
} | |
void AppConnector::exit() { | |
_running = false; | |
handler.exit(); | |
stopThread(); | |
} | |
bool AppConnector::available() { | |
return _available; | |
} | |
bool AppConnector::isRunning() { | |
return _running; | |
} | |
void AppConnector::publish(string name, string desc, getter g) { | |
_publishMethods.insert(make_pair<string, getter>(name, g)); | |
json_object_set_new(_publishDescriptions, name.c_str(), json_string(desc.c_str())); | |
} | |
void AppConnector::sync() { | |
if (!_syncMethods.empty()) { | |
json_t* data = json_loads(handler.get(getSyncMethodsToJSON()).c_str(), JSON_DECODE_ANY, NULL); | |
setMethods(data); | |
json_decref(data); | |
} | |
} | |
void AppConnector::sync(string appName, string name, setter s) { | |
MapStringMapStringSetter::const_iterator got = _syncMethods.find(appName); | |
if (got == _syncMethods.end()) { | |
MapStringSetter appMap; | |
_syncMethods.insert(make_pair<string, MapStringSetter>(appName, appMap)); | |
} | |
MapStringSetter appMap = _syncMethods.find(appName)->second; | |
appMap.insert(make_pair<string, setter>(name, s)); | |
_syncMethods[appName] = appMap; | |
} | |
/* | |
* @begin helper functions | |
*/ | |
json_t* AppConnector::publishMethods() { | |
json_t* methods = json_object(); | |
for (MapStringGetter::iterator it = _publishMethods.begin(); it != _publishMethods.end(); ++it) { | |
string m = it->first; | |
json_t* mo = publishMethod(m, it->second); | |
json_object_set(methods, m.c_str(), json_object_get(mo, m.c_str())); | |
json_decref(mo); | |
} | |
return methods; | |
} | |
json_t* AppConnector::publishMethod(string name, getter g) { | |
json_t* o = json_object(); | |
// call method | |
json_object_set_new(o, name.c_str(), json_string((((connectedApp*)ofGetAppPtr())->*g)().c_str())); | |
return o; | |
} | |
void AppConnector::setMethods(json_t* data) { | |
const char* appName; | |
json_t* appList; | |
json_object_foreach(data, appName, appList) { | |
const char* name; | |
json_t* value; | |
json_object_foreach(appList, name, value) { | |
// find in _syncMethods | |
setter s = getMethodForSyncKey(appName, name); | |
if (s != NULL) { | |
(((connectedApp*)ofGetAppPtr())->*s)(json_string_value(value)); | |
} | |
} | |
} | |
} | |
setter AppConnector::getMethodForSyncKey(const char* appName, const char* name) { | |
MapStringMapStringSetter::const_iterator foundApp = _syncMethods.find(appName); | |
if (foundApp != _syncMethods.end()) { | |
MapStringSetter appMap = foundApp->second; | |
MapStringSetter::const_iterator foundName = appMap.find(name); | |
if (foundName != appMap.end()) { | |
return foundName->second; | |
} | |
} | |
return NULL; | |
} | |
json_t* AppConnector::getSyncMethodsToJSON() { | |
json_t* j = json_object(); | |
for (MapStringMapStringSetter::iterator it = _syncMethods.begin(); it != _syncMethods.end(); ++it) { | |
string appName = it->first; | |
json_t* appList = json_incref(json_object()); | |
for (MapStringSetter::iterator appIt = it->second.begin(); appIt != it->second.end(); ++appIt) { | |
string name = appIt->first; | |
json_object_set_new(appList, name.c_str(), json_string(name.c_str())); | |
} | |
json_object_set_new(j, appName.c_str(), appList); | |
json_decref(appList); | |
} | |
return j; | |
} | |
void AppConnector::loaded() { | |
sync(); | |
handler.setDescription(_publishDescriptions); | |
_available = true; | |
start(); | |
} | |
long AppConnector::getDelay(int t) { | |
long cTime = time(NULL); | |
printf("start in: %i sec \n", int(t - cTime)); | |
return (t - cTime) * 1000; | |
} | |
void AppConnector::threadedFunction() { | |
while (isThreadRunning() && _running) { | |
if (_publishMethods.size() > 0) { | |
handler.post(publishMethods()); | |
} | |
_available = true; | |
ofSleepMillis(interval / 2); | |
sync(); | |
int iv = handler.getInterval(); | |
ofSleepMillis(interval / 2); | |
interval = iv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment