Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SiMSessionManager.cpp
- // simd
- //
- // Created by Carlos Eduardo Batista
- // Copyright (c) 2014 GT-SiM. All rights reserved.
- //
- #include "../include/SiMSessionManager.h"
- #include <chrono>
- #include <sstream>
- using std::stringstream;
- #define TOP_DIR "../pages/"
- #define DEFAULT_PORT 9990
- SiMSessionManager *SiMSessionManager::_instance = 0;
- SiMSessionManager::SiMSessionManager(SiMLogger *log, SiMLogger *debug, SiMConfig* c) {
- this->log = log;
- this->debug = debug;
- this->isHostingSession = false;
- this->config = c;
- }
- SiMSessionManager::~SiMSessionManager() {
- //TODO: [604] kill session manager thread and clean stuff
- delete server;
- _instance = 0;
- }
- SiMSessionManager *SiMSessionManager::instance(SiMLogger *log, SiMLogger *debug, SiMConfig* c) {
- if (_instance == 0) {
- _instance = new SiMSessionManager (log, debug, c);
- }
- return _instance;
- }
- void SiMSessionManager::setMutex (std::mutex* m) {
- myMutex = m;
- }
- void SiMSessionManager::setConditionalVariable (std::condition_variable* c, char *reference) {
- myCondVariable = c;
- isOrchestratorSleeping = reference;
- }
- //TODO: [300] use session_id_encode to generate from address data structure
- string SiMSessionManager::createNewSessionId() {
- return "0";
- }
- bool SiMSessionManager::loadHTTPServer(int port) {
- bool result = false;
- server = new SiMSessionManagerHTTPServer;
- result = server->load(DEFAULT_PORT,(char*)TOP_DIR, (SiMSessionManagerHTTPRequestListener*)this);
- //TODO: [301] use exceptions to handle possible errors
- return result;
- }
- void SiMSessionManager::setOrchestrator(SiMOrchestrator* o) {
- orchestator = o;
- }
- void SiMSessionManager::setConfig(SiMConfig* c) {
- config = c;
- }
- void SiMSessionManager::setMetronomeInterface(SiMMetronomeInterface* m) {
- metronomeInterface = m;
- }
- void SiMSessionManager::setPTPdInterface(SiMPTPdInterface* p) {
- ptpInterface = p;
- }
- void SiMSessionManager::setThread(std::thread* t) {
- myThread = t;
- }
- void SiMSessionManager::run() {
- //TODO: [302] session manager server thread, session manager stuff
- //TODO: [303] test communication with orchestrator
- *debug << "SiMSessionManager::run()" << std::endl;
- if (loadHTTPServer(DEFAULT_PORT)) {
- *debug << "SiMSessionManager::loadHTTPServer()" << std::endl;
- } else {
- fprintf(stderr,"SiMSessionManager::loadHTTPServer() - error");
- *debug << "SiMSessionManager::loadHTTPServer() - error" << std::endl;
- }
- std::this_thread::sleep_for(std::chrono::seconds(200));
- *debug << "SiMSessionManager slept for 200 secs." << std::endl;
- std::this_thread::sleep_for(std::chrono::seconds(1));
- *debug << "SiMSessionManager slept for 1 sec." << std::endl;
- }
- string SiMSessionManager::ptp(bool master, std::string ip, std::string type) {
- stringstream ss;
- int res = 0;
- if (master) {
- res = ptpInterface->createMaster(ip, config->getValue ("networkInterface"));
- //TODO: [505] read interface from config file
- } else {
- res = ptpInterface->createSlave(ip,config->getValue ("networkInterface"));
- }
- if (type == "HTML_RESPONSE") {
- if (res > -1) {
- ss << "SiMD - PTPD SYNC OK with " << ip;
- ss << " MASTER = " << master << std::endl;
- } else {
- ss << "SiMD - PTPD SYNC ERROR" << std::endl;
- }
- } else {
- string title = "GT-SiM - PTP SYNC";
- ss << "<?XML version=1.0?>" << std::endl;
- ss << "<title>" << title << "</title>" << std::endl;
- ss << std::endl;
- if (res > -1) {
- string status ="SiMD - PTPD SYNC OK";
- ss << "<status>" << status << "</status>" << std::endl;
- ss << "<ip>" << ip << "</ip>" << std::endl;
- ss << "<master>" << master << "</master>" << std::endl;
- } else {
- string error ="SiMD - PTPD SYNC ERROR";
- ss << "<error>" << error << "</error>" << std::endl;
- }
- }
- return ss.str();
- }
- string SiMSessionManager::connection(SiMAudioConnectionParams params, int audioService, int id, std::string connection_type, int buffersize, int samplerate, SiMAudioConnectionHosts* host_a, SiMAudioConnectionHosts* host_b, int channels,
- int Approach, int delay, string title, string type) {
- stringstream ss;
- string res = orchestator->createConnection(params);
- if (type == "HTML_RESPONSE") {
- if (atoi(res.c_str()) > 0) {
- ss << res << std::endl;
- ss << "SiMD - CREATED CONNECTION " << res << " <br/>" << std::endl;
- ss << params.toString() << std::endl;
- } else {
- ss << "SiMD - ERROR CREATING CONNECTION <br/>" << std::endl;
- }
- } else {
- if (atoi(res.c_str()) > 0) {
- ss << "<?XML version=1.0?>" << std::endl;
- ss << "<title>" << title << "</title>" << std::endl;
- ss << std::endl;
- ss << "<res>" << res << "</res>" << std::endl;
- string str = "SiMD - CREATED CONNECTION";
- ss << "<str>" << str << "</str>" << std::endl;
- ss << std::endl;
- ss << "<conparams>" << std::endl;
- ss << std::endl;
- ss << " <audioService>" << audioService << "</audioService>" << std::endl;
- ss << " <connection_type>" << connection_type << "</connection_type>" << std::endl;
- ss << " <buffersize>" << buffersize << "</buffersize>" << std::endl;
- ss << " <samplerate>" << samplerate << "</samplerate>" << std::endl;
- ss << " <host_a>" << host_a << "</host_a>" << std::endl;
- ss << " <host_b>" << host_b << "</host_b>" << std::endl;
- ss << " <channels>" << channels << "</channels>" << std::endl;
- ss << " <delay>" << delay << "</delay>" << std::endl;
- ss << " <approach>" << Approach << "</approach>" << std::endl;
- ss << std::endl;
- ss << "</conparams>" <<std::endl;
- } else {
- string errorStr = "SiMD - ERROR CREATING CONNECTION ";
- ss << "<errorStr>" << errorStr << "</errorStr>" << std::endl;
- }
- }
- return ss.str();
- }
- string SiMSessionManager::removeConnection(std::string id, string type) {
- stringstream ss;
- if (type == "HTML_RESPONSE") {
- std::cout << "removeConnection " << id << std::endl;
- if (orchestator->removeConnection(id)) {
- ss << "SiMD - REMOVED CONNECTION " << id << " <br/>" << std::endl;
- } else {
- ss << "SiMD - ERROR REMOVING CONNECTION " << id << "<br/>" << std::endl;
- }
- } else {
- string title = "GT-SiM - REMOVE CONNECTION";
- string rem = "SiMD - REMOVED CONNECTION";
- string error = "SiMD - ERROR REMOVING CONNECTION ";
- ss << "<?XML version=1.0?>" << std::endl;
- ss << "<title>" << title << "</title>" << std::endl;
- ss << std::endl;
- ss << "<id>" << id << "</id>" << std::endl;
- if (orchestator->removeConnection(id)) {
- ss << "<rem>" << rem << "</rem>" << std::endl;
- } else {
- ss << "<error>" << error << "</error>" << std::endl;
- }
- }
- return ss.str();
- }
- string SiMSessionManager::metronome(int bpm, string timestamp, int duration, bool now, string title, string type) {
- //TODO: [507] Implement proper metronome handler
- stringstream ss;
- if (type == "HTML_RESPONSE") {
- ss << "Called metronome with params:" << std::endl;
- ss << "bpm="<< bpm << " timestamp=" << timestamp << " duration=" << duration;
- ss << " now = " << now << "<br/>" << std::endl;
- ss << "METRONOME CALL RESULT = ";
- if (now) {
- ss << metronomeInterface->startBeating(duration,bpm);
- //TODO: [506] Schedule beat end
- } else {
- ss << metronomeInterface->sendStr("a "+timestamp);
- }
- } else {
- ss << "<?XML version=1.0?>" << std::endl;
- ss << "<title>" << title << "</title>" << std::endl;
- ss << std::endl;
- string str = "Called metronome with params:";
- ss << "<str>" << str << "</str>" << std::endl;
- ss << std::endl;
- ss <<"<metroparams>" << std::endl;
- ss << std::endl;
- ss << " <bpm>" << bpm << "</bpm>" << std::endl;
- ss << " <timestamp>" << timestamp << "</timestamp>" << std::endl;
- ss << " <duration>" << duration << "</duration>" << std::endl;
- ss << " <now>" << now<< "</now>" << std::endl;
- ss << std::endl;
- ss <<"</metroparams>" << std::endl;
- }
- return ss.str();
- }
- void SiMSessionManager::session_id_encode(unsigned char* input, unsigned char* output) {
- output[7] = input[5] & 0x3F;
- unsigned char to6_l4 = input[4] & 0x0F;
- unsigned char sto6_l4_2 = to6_l4 << 2;
- unsigned char to6_f2 = input[5] & 0xC0;
- unsigned char sto6_f2_6 = to6_f2 >> 6;
- output[6] = sto6_l4_2 + sto6_f2_6;
- unsigned char to5_f4 = input[4] & 0xF0;
- unsigned char sto5_f4_4 = to5_f4 >> 4;
- unsigned char to5_l2 = input[3] & 0x03;
- unsigned char sto5_l2_4 = to5_l2 << 4;
- output[5] = sto5_l2_4 + sto5_f4_4;
- unsigned char to4_f6 = input[3] & 0xFC;
- unsigned char sto4_f6 = to4_f6 >> 2;
- output[4] = sto4_f6;
- output[3] = input[2] & 0x3F;
- unsigned char to2_l4 = input[1] & 0x0F;
- unsigned char sto2_l4_2 = to2_l4 << 2;
- unsigned char to2_f2 = input[2] & 0xC0;
- unsigned char sto2_f2_6 = to2_f2 >> 6;
- output[2] = sto2_l4_2 + sto2_f2_6;
- unsigned char to1_f4 = input[1] & 0xF0;
- unsigned char sto1_f4_4 = to1_f4 >> 4;
- unsigned char to1_l2 = input[0] & 0x03;
- unsigned char sto1_l2_4 = to1_l2 << 4;
- output[1] = sto1_f4_4 + sto1_l2_4;
- unsigned char to0_f6 = input[0] & 0xFC;
- unsigned char sto0_f6 = to0_f6 >> 2;
- output[0] = sto0_f6;
- }
- void SiMSessionManager::session_id_decode(unsigned char* input, unsigned char* output) {
- unsigned char to0_l6 = input[0] & 0x3F;
- unsigned char sto_l6_2 = to0_l6 << 2;
- unsigned char to0_m2 = input[1] & 0x30;
- unsigned char sto0_m2_4 = to0_m2 >> 4;
- output[0] = sto_l6_2 + sto0_m2_4;
- unsigned char to1_l4 = input[1] & 0x0F;
- unsigned char sto1_l4_4 = to1_l4 << 4;
- unsigned char to1_m4 = input[2] & 0x3C;
- unsigned char sto1_m4_2 = to1_m4 >> 2;
- output[1] = sto1_l4_4 + sto1_m4_2;
- unsigned char to2_l2 = input[2] & 0x03;
- unsigned char sto2_l2_4 = to2_l2 << 4;
- unsigned char to2_l6 = input[3] & 0x3F;
- output[2] = sto2_l2_4 + to2_l6;
- unsigned char to3_l6 = input[4] & 0x3F;
- unsigned char sto3_l6 = to3_l6 << 2;
- unsigned char to3_m2 = input[5] & 0x30;
- unsigned char sto3_m2 = to3_m2 >> 4;
- output[3] = sto3_l6 + sto3_m2;
- unsigned char to4_l4 = input[5] & 0x0F;
- unsigned char sto4_l4_4 = to4_l4 << 4;
- unsigned char to4_m4 = input[6] & 0x3C;
- unsigned char sto4_m4_2 = to4_m4 >> 2;
- output[4] = sto4_l4_4 + sto4_m4_2;
- unsigned char to5_l2 = input[6] & 0x03;
- unsigned char sto5_l2_6 = to5_l2 << 6;
- unsigned char to5_l6 = input[7] & 0x3F;
- output[5] = sto5_l2_6 + to5_l6;
- }
- SiMSessionManagerHTTPResponse SiMSessionManager::receiveHTTPRequest(SiMSessionManagerHTTPRequest* r) {
- *debug << "SiMSessionManager::receiveHTTPRequest" << std::endl;
- stringstream ss;
- if (r->getUrl() == "/metronome") {
- string mres = metronome(atoi(r->getParamValue("bpm").c_str()),r->getParamValue("timestamp"),
- atoi(r->getParamValue("duration").c_str()),(r->getParamValue("now")=="yes"),"GT-SiM - METRONOME",
- r->getResponseType());
- if (r->getResponseType() == "HTML_RESPONSE") {
- ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - METRONOME");
- ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
- } else {
- ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
- }
- } if (r->getUrl() == "/remove_connection") {
- string mres = removeConnection(r->getParamValue("id"), r->getResponseType());
- if (r->getResponseType() == "HTML_RESPONSE") {
- ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - REMOVE CONNECTION");
- ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
- } else {
- ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
- }
- } else if (r->getUrl() == "/connection") {
- //TODO: [200] implement support for other audio services (such as sound jack)
- //TODO: [200] create session and connections properly. store hosts contact info.
- // SiMAudioConnectionParams(int audioService, int id, std::string connection_type, int buffersize, int samplerate,
- // SiMAudioConnectionHosts* host_a, SiMAudioConnectionHosts* host_b, int channels, int Approach, int delay);
- //SiMAudioConnectionHosts(std::string id_h, std::string ip_host, int porta, std::string n_responsavel,
- //std::string email_responsavel, int input_channel, int output_channel);
- //JACKTRIP = 9991
- //0 - UNIQUE SESSION ID
- SiMAudioConnectionHosts* host_a = new SiMAudioConnectionHosts("0",(string)r->getParamValue("host_a_ip"),(int)atoi(r->getParamValue("host_a_port").c_str()),"hostA","hostA",
- (int)atoi(r->getParamValue("input_channel_a").c_str()),(int)atoi(r->getParamValue("output_channel_a").c_str()));
- SiMAudioConnectionHosts* host_b = new SiMAudioConnectionHosts("0",(string)r->getParamValue("host_b_ip"),(int)atoi(r->getParamValue("host_b_port").c_str()),"hostB","hostB",
- (int)atoi(r->getParamValue("input_channel_b").c_str()),(int)atoi(r->getParamValue("output_channel_b").c_str()));
- SiMAudioConnectionParams params(9991,0,
- (string)r->getParamValue("type"),
- (int)atoi(r->getParamValue("buffersize").c_str()),
- (int)atoi(r->getParamValue("samplerate").c_str()),
- host_a,host_b,
- (int)atoi(r->getParamValue("channels").c_str()),(int)atoi(r->getParamValue("delay").c_str()),
- (int)atoi(r->getParamValue("approach").c_str())
- );
- string mres = connection(params, 9991,0, (string)r->getParamValue("type"), (int)atoi(r->getParamValue("buffersize").c_str()),
- (int)atoi(r->getParamValue("samplerate").c_str()), host_a,host_b,
- (int)atoi(r->getParamValue("channels").c_str()),(int)atoi(r->getParamValue("delay").c_str()),
- (int)atoi(r->getParamValue("approach").c_str()), "GT-SiM - AUDIO SERVICE CONNECTION",
- r->getResponseType());
- if (r->getResponseType() == "HTML_RESPONSE") {
- ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - AUDIO SERVICE CONNECTION");
- ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
- } else {
- ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
- }
- } else if (r->getUrl() == "/orchestrator") {
- //TODO: [201] handle request and update server state accordingly !!!!
- } else if (r->getUrl() == "/newsession") {
- //TODO: [201] handle request and update server state accordingly !!!!
- } else if (r->getUrl() == "/joinsession") {
- //TODO: [209] join session
- if (!isHostingSession) {
- //RETURN ERROR
- } else {
- //handles...
- }
- } else if (r->getUrl() == "/session") {
- //TODO: [201] handle request and update server state accordingly !!!!
- if (!isHostingSession) {
- //ERROR
- } else {
- //handles
- }
- } else if (r->getUrl() == "/ptp") {
- string mres = ptp((r->getParamValue("master")=="yes"),r->getParamValue("ip"), r->getResponseType());
- if (r->getResponseType() == "HTML_RESPONSE") {
- ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - PTP SYNC");
- ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
- } else {
- ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
- }
- }
- SiMSessionManagerHTTPResponse res(ss.str());
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement