Advertisement
Draegon

Manager.cpp

Jul 28th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.80 KB | None | 0 0
  1. //
  2. //  SiMSessionManager.cpp
  3. //  simd
  4. //
  5. //  Created by Carlos Eduardo Batista
  6. //  Copyright (c) 2014 GT-SiM. All rights reserved.
  7. //
  8.  
  9. #include "../include/SiMSessionManager.h"
  10.  
  11. #include <chrono>
  12. #include <sstream>
  13.  
  14. using std::stringstream;
  15.  
  16. #define TOP_DIR "../pages/"
  17. #define DEFAULT_PORT 9990
  18.  
  19. SiMSessionManager *SiMSessionManager::_instance = 0;
  20.  
  21. SiMSessionManager::SiMSessionManager(SiMLogger *log, SiMLogger *debug, SiMConfig* c) {
  22.     this->log = log;
  23.     this->debug = debug;
  24.     this->isHostingSession = false;
  25.     this->config = c;
  26. }
  27.  
  28. SiMSessionManager::~SiMSessionManager() {
  29.     //TODO: [604] kill session manager thread and clean stuff
  30.  
  31.     delete server;
  32.  
  33.     _instance = 0;
  34. }
  35.  
  36. SiMSessionManager *SiMSessionManager::instance(SiMLogger *log, SiMLogger *debug, SiMConfig* c) {
  37.     if (_instance == 0) {
  38.         _instance = new SiMSessionManager (log, debug, c);
  39.     }
  40.     return _instance;
  41. }
  42.  
  43. void SiMSessionManager::setMutex (std::mutex* m) {
  44.     myMutex = m;
  45. }
  46.  
  47. void SiMSessionManager::setConditionalVariable (std::condition_variable* c, char *reference) {
  48.     myCondVariable = c;
  49.     isOrchestratorSleeping = reference;
  50. }
  51.  
  52. //TODO: [300] use session_id_encode to generate from address data structure
  53. string SiMSessionManager::createNewSessionId() {
  54.  
  55.     return "0";
  56. }
  57.  
  58. bool SiMSessionManager::loadHTTPServer(int port) {
  59.     bool result = false;
  60.     server = new SiMSessionManagerHTTPServer;
  61.    
  62.     result = server->load(DEFAULT_PORT,(char*)TOP_DIR, (SiMSessionManagerHTTPRequestListener*)this);
  63.     //TODO: [301] use exceptions to handle possible errors
  64.     return result;
  65. }
  66.  
  67. void SiMSessionManager::setOrchestrator(SiMOrchestrator* o) {
  68.     orchestator = o;
  69. }
  70.  
  71. void SiMSessionManager::setConfig(SiMConfig* c) {
  72.     config = c;
  73. }
  74.  
  75. void SiMSessionManager::setMetronomeInterface(SiMMetronomeInterface* m) {
  76.     metronomeInterface = m;
  77. }
  78.  
  79. void SiMSessionManager::setPTPdInterface(SiMPTPdInterface* p) {
  80.     ptpInterface = p;
  81.  
  82. }
  83.  
  84. void SiMSessionManager::setThread(std::thread* t) {
  85.     myThread = t;
  86. }
  87.  
  88. void SiMSessionManager::run() {
  89.     //TODO: [302] session manager server thread, session manager stuff
  90.     //TODO: [303] test communication with orchestrator
  91.     *debug << "SiMSessionManager::run()" << std::endl;
  92.     if (loadHTTPServer(DEFAULT_PORT)) {
  93.         *debug << "SiMSessionManager::loadHTTPServer()" << std::endl;
  94.     } else {
  95.         fprintf(stderr,"SiMSessionManager::loadHTTPServer() - error");
  96.         *debug << "SiMSessionManager::loadHTTPServer() - error" << std::endl;
  97.     }
  98.  
  99.     std::this_thread::sleep_for(std::chrono::seconds(200));
  100.     *debug << "SiMSessionManager slept for 200 secs." << std::endl;
  101.     std::this_thread::sleep_for(std::chrono::seconds(1));
  102.     *debug << "SiMSessionManager slept for 1 sec." << std::endl;
  103. }
  104.  
  105. string SiMSessionManager::ptp(bool master, std::string ip, std::string type) {
  106.     stringstream ss;
  107.     int res = 0;
  108.  
  109.     if (master) {
  110.         res = ptpInterface->createMaster(ip, config->getValue ("networkInterface"));
  111.         //TODO: [505] read interface from config file
  112.     } else {
  113.         res = ptpInterface->createSlave(ip,config->getValue ("networkInterface"));
  114.     }
  115.  
  116.     if (type == "HTML_RESPONSE") {
  117.  
  118.         if (res > -1) {
  119.             ss << "SiMD - PTPD SYNC OK with " << ip;
  120.             ss << " MASTER = " << master << std::endl;
  121.         } else {
  122.             ss << "SiMD - PTPD SYNC ERROR" << std::endl;
  123.         }
  124.    
  125.     } else {
  126.  
  127.         string title = "GT-SiM - PTP SYNC";
  128.  
  129.         ss << "<?XML version=1.0?>" << std::endl;
  130.         ss << "<title>" << title << "</title>" << std::endl;
  131.         ss << std::endl;
  132.  
  133.         if (res > -1) {
  134.             string status ="SiMD - PTPD SYNC OK";
  135.  
  136.             ss << "<status>" << status << "</status>" << std::endl;
  137.             ss << "<ip>" << ip << "</ip>" << std::endl;
  138.             ss << "<master>" << master << "</master>" << std::endl;
  139.         } else {
  140.             string error ="SiMD - PTPD SYNC ERROR";
  141.             ss << "<error>" << error << "</error>" << std::endl;
  142.         }
  143.  
  144.         }
  145.     return ss.str();
  146. }
  147.  
  148. 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,
  149.                      int Approach, int delay, string title, string type) {
  150.     stringstream ss;
  151.  
  152.     string res = orchestator->createConnection(params);
  153.  
  154.     if (type == "HTML_RESPONSE") {
  155.         if (atoi(res.c_str()) > 0) {
  156.             ss << res << std::endl;
  157.             ss << "SiMD - CREATED CONNECTION " << res << " <br/>" << std::endl;
  158.             ss << params.toString() << std::endl;
  159.         } else {
  160.             ss << "SiMD - ERROR CREATING CONNECTION <br/>" << std::endl;
  161.         }
  162.     }   else {
  163.         if (atoi(res.c_str()) > 0) {
  164.             ss << "<?XML version=1.0?>" << std::endl;
  165.             ss << "<title>" << title << "</title>" << std::endl;
  166.             ss << std::endl;
  167.  
  168.             ss << "<res>" << res << "</res>" << std::endl;
  169.  
  170.             string str = "SiMD - CREATED CONNECTION";
  171.             ss << "<str>" << str << "</str>" << std::endl;
  172.             ss << std::endl;
  173.  
  174.             ss << "<conparams>" << std::endl;
  175.             ss << std::endl;
  176.  
  177.             ss << "     <audioService>" << audioService << "</audioService>" << std::endl;
  178.             ss << "     <connection_type>" << connection_type << "</connection_type>" << std::endl;
  179.             ss << "     <buffersize>" << buffersize << "</buffersize>" << std::endl;
  180.             ss << "     <samplerate>" << samplerate << "</samplerate>" << std::endl;
  181.             ss << "     <host_a>" << host_a << "</host_a>" << std::endl;
  182.             ss << "     <host_b>" << host_b << "</host_b>" << std::endl;   
  183.             ss << "     <channels>" << channels << "</channels>" << std::endl;
  184.             ss << "     <delay>" << delay << "</delay>" << std::endl;
  185.             ss << "     <approach>" << Approach << "</approach>" << std::endl;
  186.             ss << std::endl;
  187.  
  188.             ss << "</conparams>" <<std::endl;
  189.  
  190.         } else {
  191.             string errorStr = "SiMD - ERROR CREATING CONNECTION ";
  192.             ss << "<errorStr>" << errorStr << "</errorStr>" << std::endl;
  193.         }
  194.        }   
  195.  
  196.  
  197.     return ss.str();
  198. }
  199.  
  200. string SiMSessionManager::removeConnection(std::string id, string type) {
  201.     stringstream ss;
  202.  
  203.     if (type == "HTML_RESPONSE") {
  204.         std::cout << "removeConnection " << id << std::endl;
  205.  
  206.         if (orchestator->removeConnection(id)) {
  207.             ss << "SiMD - REMOVED CONNECTION " <<  id << " <br/>" << std::endl;
  208.         } else {
  209.             ss << "SiMD - ERROR REMOVING CONNECTION " <<  id << "<br/>"  << std::endl;
  210.           }
  211.  
  212.     }   else {
  213.         string title = "GT-SiM - REMOVE CONNECTION";
  214.         string rem = "SiMD - REMOVED CONNECTION";
  215.         string error = "SiMD - ERROR REMOVING CONNECTION ";
  216.  
  217.         ss << "<?XML version=1.0?>" << std::endl;
  218.         ss << "<title>" << title << "</title>" << std::endl;
  219.         ss << std::endl;
  220.  
  221.         ss << "<id>" << id << "</id>" << std::endl;
  222.  
  223.         if (orchestator->removeConnection(id)) {
  224.             ss << "<rem>" << rem << "</rem>" << std::endl;
  225.         } else {
  226.             ss << "<error>" << error << "</error>" << std::endl;
  227.           }
  228.        }
  229.     return ss.str();
  230.  
  231. }
  232.  
  233. string SiMSessionManager::metronome(int bpm, string timestamp, int duration, bool now, string title, string type) {
  234.     //TODO: [507] Implement proper metronome handler
  235.     stringstream ss;
  236.  
  237.     if (type == "HTML_RESPONSE") {
  238.         ss << "Called metronome with params:" << std::endl;
  239.         ss << "bpm="<< bpm << " timestamp=" << timestamp << " duration=" << duration;
  240.         ss << " now = " << now << "<br/>" << std::endl;
  241.         ss << "METRONOME CALL RESULT = ";
  242.         if (now) {
  243.             ss << metronomeInterface->startBeating(duration,bpm);
  244.             //TODO: [506] Schedule beat end
  245.         } else {
  246.             ss << metronomeInterface->sendStr("a "+timestamp);
  247.           }
  248.  
  249.     }   else {
  250.         ss << "<?XML version=1.0?>" << std::endl;
  251.         ss << "<title>" << title << "</title>" << std::endl;
  252.         ss << std::endl;
  253.  
  254.  
  255.         string str = "Called metronome with params:";
  256.         ss << "<str>" << str << "</str>" << std::endl;
  257.         ss << std::endl;
  258.  
  259.         ss <<"<metroparams>" << std::endl;
  260.         ss << std::endl;
  261.  
  262.         ss << "     <bpm>" << bpm << "</bpm>" << std::endl;
  263.         ss << "     <timestamp>" << timestamp << "</timestamp>" << std::endl;
  264.         ss << "     <duration>" << duration << "</duration>" << std::endl;
  265.         ss << "     <now>" << now<< "</now>" << std::endl; 
  266.         ss << std::endl;
  267.  
  268.         ss <<"</metroparams>" << std::endl;
  269.          }
  270.    
  271.     return ss.str();
  272. }
  273.  
  274. void SiMSessionManager::session_id_encode(unsigned char* input, unsigned char* output) {
  275.     output[7] = input[5] & 0x3F;
  276.  
  277.     unsigned char to6_l4 = input[4] & 0x0F;
  278.     unsigned char sto6_l4_2 = to6_l4 << 2;
  279.     unsigned char to6_f2 = input[5] & 0xC0;
  280.     unsigned char sto6_f2_6 = to6_f2 >> 6;
  281.     output[6] = sto6_l4_2 + sto6_f2_6;
  282.  
  283.     unsigned char to5_f4 = input[4] & 0xF0;
  284.     unsigned char sto5_f4_4 = to5_f4 >> 4;
  285.     unsigned char to5_l2 = input[3] & 0x03;
  286.     unsigned char sto5_l2_4 = to5_l2 << 4;
  287.     output[5] = sto5_l2_4 + sto5_f4_4;
  288.  
  289.     unsigned char to4_f6 = input[3] & 0xFC;
  290.     unsigned char sto4_f6 = to4_f6 >> 2;
  291.     output[4] = sto4_f6;
  292.  
  293.     output[3] = input[2] & 0x3F;
  294.  
  295.     unsigned char to2_l4 = input[1] & 0x0F;
  296.     unsigned char sto2_l4_2 = to2_l4 << 2;
  297.     unsigned char to2_f2 = input[2] & 0xC0;
  298.     unsigned char sto2_f2_6 = to2_f2 >> 6;
  299.     output[2] = sto2_l4_2 + sto2_f2_6;
  300.  
  301.     unsigned char to1_f4 = input[1] & 0xF0;
  302.     unsigned char sto1_f4_4 = to1_f4 >> 4;
  303.     unsigned char to1_l2 = input[0] & 0x03;
  304.     unsigned char sto1_l2_4 = to1_l2 << 4;
  305.     output[1] = sto1_f4_4 + sto1_l2_4;
  306.  
  307.     unsigned char to0_f6 = input[0] & 0xFC;
  308.     unsigned char sto0_f6 = to0_f6 >> 2;
  309.     output[0] = sto0_f6;
  310. }
  311.  
  312. void SiMSessionManager::session_id_decode(unsigned char* input, unsigned char* output) {
  313.     unsigned char to0_l6 = input[0] & 0x3F;
  314.     unsigned char sto_l6_2 = to0_l6 << 2;
  315.     unsigned char to0_m2 = input[1] & 0x30;
  316.     unsigned char sto0_m2_4 = to0_m2 >> 4;
  317.     output[0] = sto_l6_2 + sto0_m2_4;
  318.  
  319.     unsigned char to1_l4 = input[1] & 0x0F;
  320.     unsigned char sto1_l4_4 = to1_l4 << 4;
  321.     unsigned char to1_m4 = input[2] & 0x3C;
  322.     unsigned char sto1_m4_2 = to1_m4 >> 2;
  323.     output[1] = sto1_l4_4 + sto1_m4_2;
  324.  
  325.     unsigned char to2_l2 = input[2] & 0x03;
  326.     unsigned char sto2_l2_4 = to2_l2 << 4;
  327.     unsigned char to2_l6 = input[3] & 0x3F;
  328.     output[2] = sto2_l2_4 + to2_l6;
  329.  
  330.     unsigned char to3_l6 = input[4] & 0x3F;
  331.     unsigned char sto3_l6 = to3_l6 << 2;
  332.     unsigned char to3_m2 = input[5] & 0x30;
  333.     unsigned char sto3_m2 = to3_m2 >> 4;
  334.     output[3] = sto3_l6 + sto3_m2;
  335.  
  336.     unsigned char to4_l4 = input[5] & 0x0F;
  337.     unsigned char sto4_l4_4 = to4_l4 << 4;
  338.     unsigned char to4_m4 = input[6] & 0x3C;
  339.     unsigned char sto4_m4_2 = to4_m4 >> 2;
  340.     output[4] = sto4_l4_4 + sto4_m4_2;
  341.  
  342.     unsigned char to5_l2 = input[6] & 0x03;
  343.     unsigned char sto5_l2_6 = to5_l2 << 6;
  344.     unsigned char to5_l6 = input[7] & 0x3F;
  345.     output[5] = sto5_l2_6 + to5_l6;
  346. }
  347.  
  348. SiMSessionManagerHTTPResponse SiMSessionManager::receiveHTTPRequest(SiMSessionManagerHTTPRequest* r) {
  349.     *debug << "SiMSessionManager::receiveHTTPRequest" << std::endl;
  350.     stringstream ss;
  351.  
  352.     if (r->getUrl() == "/metronome") {
  353.  
  354.             string mres = metronome(atoi(r->getParamValue("bpm").c_str()),r->getParamValue("timestamp"),
  355.                         atoi(r->getParamValue("duration").c_str()),(r->getParamValue("now")=="yes"),"GT-SiM - METRONOME",
  356.                      r->getResponseType());
  357.  
  358.         if (r->getResponseType() == "HTML_RESPONSE") {
  359.  
  360.             ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - METRONOME");
  361.             ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
  362.  
  363.         }   else {
  364.  
  365.             ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
  366.             }
  367.            
  368.  
  369.     }   if (r->getUrl() == "/remove_connection") {
  370.  
  371.         string mres = removeConnection(r->getParamValue("id"), r->getResponseType());
  372.  
  373.         if (r->getResponseType() == "HTML_RESPONSE") {
  374.  
  375.             ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - REMOVE CONNECTION");
  376.             ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
  377.        
  378.         }   else {
  379.  
  380.             ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
  381.             }
  382.  
  383.  
  384.     } else if (r->getUrl() == "/connection") {
  385.  
  386.         //TODO: [200] implement support for other audio services (such as sound jack)
  387.         //TODO: [200] create session and connections properly. store hosts contact info.
  388.  
  389.         //  SiMAudioConnectionParams(int audioService, int id, std::string connection_type, int buffersize, int samplerate,
  390.         // SiMAudioConnectionHosts* host_a, SiMAudioConnectionHosts* host_b, int channels, int Approach, int delay);
  391.  
  392.  
  393.         //SiMAudioConnectionHosts(std::string id_h, std::string ip_host, int porta, std::string n_responsavel,
  394.         //std::string email_responsavel, int input_channel, int output_channel);
  395.  
  396.         //JACKTRIP = 9991
  397.         //0 - UNIQUE SESSION ID
  398.  
  399.         SiMAudioConnectionHosts* host_a = new SiMAudioConnectionHosts("0",(string)r->getParamValue("host_a_ip"),(int)atoi(r->getParamValue("host_a_port").c_str()),"hostA","hostA",
  400.                 (int)atoi(r->getParamValue("input_channel_a").c_str()),(int)atoi(r->getParamValue("output_channel_a").c_str()));
  401.         SiMAudioConnectionHosts* host_b = new SiMAudioConnectionHosts("0",(string)r->getParamValue("host_b_ip"),(int)atoi(r->getParamValue("host_b_port").c_str()),"hostB","hostB",
  402.                 (int)atoi(r->getParamValue("input_channel_b").c_str()),(int)atoi(r->getParamValue("output_channel_b").c_str()));
  403.  
  404.         SiMAudioConnectionParams params(9991,0,
  405.                 (string)r->getParamValue("type"),
  406.                 (int)atoi(r->getParamValue("buffersize").c_str()),
  407.                 (int)atoi(r->getParamValue("samplerate").c_str()),
  408.                 host_a,host_b,
  409.                 (int)atoi(r->getParamValue("channels").c_str()),(int)atoi(r->getParamValue("delay").c_str()),
  410.                 (int)atoi(r->getParamValue("approach").c_str())
  411.                 );
  412.  
  413.         string mres = connection(params, 9991,0, (string)r->getParamValue("type"), (int)atoi(r->getParamValue("buffersize").c_str()),
  414.                      (int)atoi(r->getParamValue("samplerate").c_str()), host_a,host_b,
  415.                      (int)atoi(r->getParamValue("channels").c_str()),(int)atoi(r->getParamValue("delay").c_str()),
  416.                      (int)atoi(r->getParamValue("approach").c_str()), "GT-SiM - AUDIO SERVICE CONNECTION",
  417.                       r->getResponseType());
  418.  
  419.         if (r->getResponseType() == "HTML_RESPONSE") {
  420.  
  421.             ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - AUDIO SERVICE CONNECTION");
  422.             ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
  423.  
  424.         } else {
  425.  
  426.             ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
  427.           }
  428.  
  429.     } else if (r->getUrl() == "/orchestrator") {
  430.         //TODO: [201] handle request and update server state accordingly !!!!
  431.  
  432.     } else if  (r->getUrl() == "/newsession") {
  433.         //TODO: [201] handle request and update server state accordingly !!!!
  434.  
  435.     } else if (r->getUrl() == "/joinsession") {
  436.         //TODO: [209] join session
  437.         if (!isHostingSession) {
  438.             //RETURN ERROR
  439.         } else {
  440.             //handles...
  441.         }
  442.  
  443.     } else if  (r->getUrl() == "/session") {
  444.         //TODO: [201] handle request and update server state accordingly !!!!
  445.         if (!isHostingSession) {
  446.             //ERROR
  447.         } else {
  448.             //handles
  449.         }
  450.  
  451.  
  452.     } else if  (r->getUrl() == "/ptp") {
  453.  
  454.         string mres = ptp((r->getParamValue("master")=="yes"),r->getParamValue("ip"), r->getResponseType());
  455.  
  456.         if (r->getResponseType() == "HTML_RESPONSE") {
  457.    
  458.             ss << SiMSessionManagerHTTPResponse::HTMLHeader("GT-SiM - PTP SYNC");
  459.             ss << SiMSessionManagerHTTPResponse::HTMLBody(mres);
  460.  
  461.         }   else {
  462.  
  463.             ss << SiMSessionManagerHTTPResponse::XMLResponse(mres);
  464.             }
  465.     }
  466.  
  467.     SiMSessionManagerHTTPResponse res(ss.str());
  468.  
  469.     return res;
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement