#include "Server.h" void Server::start(){ std::cout << "Initialising TCP sever" << std::endl; server.setup(port); osc_sender.setup(OSC_HOST, OSC_PORT); http.setup(http_ip, http_port, http_page); is_active = true; previous_embedding = embedding; last_change_time = std::chrono::steady_clock::now(); } void Server::update(){ for ( int i = 0; i < server.getLastID(); i++){ if (server.isClientConnected(i)) { const int buffer_size = 8; char buffer[buffer_size]; int bytes_recieved = server.receiveRawBytes(i, buffer, buffer_size); if (bytes_recieved == buffer_size){ float value; int id; memcpy(&value, buffer, sizeof(float)); memcpy(&id, buffer + sizeof(float), sizeof(int)); std::string ip_address = server.getClientIP(i); addOrUpdateClient(id, value, ip_address); } } } updateEmbedding(); checkActivity(); sendOSCMessage(); if(debug){ printClients(); } } void Server::addOrUpdateClient(int client_id, float value, const std::string& ip_address){ ClientInfo client; client.ip_address = ip_address; client.value = value; clients[client_id] = client; } void Server::updateEmbedding(){ for(const auto& c : clients){ const ClientInfo& info = c.second; float val = std::round(info.value * 100.0f) / 100.0f; switch(c.first){ case 0: embedding.angry = val; break; case 1: embedding.disgust = val; break; case 2: embedding.fear = val; break; case 3: embedding.happy = val; break; case 4: embedding.sad = val; break; case 5: embedding.surprise = val; break; case 6: embedding.neutral = val; embedding.fear = ofRandom(0.1, 0.6); embedding.angry = ofRandom(0.01, 0.99); embedding.happy = ofRandom(0.01, 0.99); break; } } } void Server::printClients(){ for( const auto& c : clients){ int id = c.first; const ClientInfo& info = c.second; std::cout << "id: " << id << ", value: " << info.value << ", IP: " << info.ip_address << std::endl; } std::cout << is_active << std::endl; } /* check if the controllers are in use */ void Server::checkActivity(){ if (previous_embedding.neutral != embedding.neutral) { // Check if embedding has changed last_change_time = std::chrono::steady_clock::now(); // Reset the timer if there is a change previous_embedding = embedding; // Update the previous embedding to the current one is_active = true; sendHttpRequest(); } else { // Calculate the time since the last change auto now = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast(now - last_change_time).count(); if (duration >= 2) { is_active = false; } } } /* send osc msg, check if audio file exists and it is different to the past audiofile */ void Server::sendOSCMessage(){ std::vector messages; ofxOscMessage me_0; ofxOscMessage me_1; ofxOscMessage me_2; ofxOscMessage me_3; ofxOscMessage me_file; std::string audio_file = vp_resp.folder; // Check if file exists in a given dir ofFile file("/home/cailean/Desktop/rave/all_wav_files/" + audio_file + ".wav"); if(!is_active && (audio_file != past_audio_file) && file.exists()){ me_file.setAddress("/emote/filename"); me_file.addStringArg(audio_file + ".wav"); messages.push_back(me_file); past_audio_file = audio_file; } me_0.setAddress("/emote/0"); me_0.addFloatArg(embedding.neutral); messages.push_back(me_0); me_1.setAddress("/emote/1"); me_1.addFloatArg(embedding.neutral); messages.push_back(me_1); me_2.setAddress("/emote/2"); me_2.addFloatArg(embedding.neutral); messages.push_back(me_2); me_3.setAddress("/emote/3"); me_3.addFloatArg(embedding.neutral); messages.push_back(me_3); for (auto& msg : messages){ osc_sender.sendMessage(msg, false); } } /* sends request to http server if is_active = true */ void Server::sendHttpRequest(){ vp_resp = http.query(embedding); }