6 changed files with 248 additions and 14 deletions
			
			
		@ -0,0 +1,31 @@ | 
				
			|||||
 | 
					#include "Request.h" | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/* setup http server */ | 
				
			||||
 | 
					void Request::setup(std::string ip, int port, std::string page){ | 
				
			||||
 | 
					    req.method = ofHttpRequest::POST; | 
				
			||||
 | 
					    req.url = "http://" + ip + ":" + ofToString(port) + "/" + page; | 
				
			||||
 | 
					    req.headers["Content-Type"] = "application/json"; | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/* send a request to vp_server & return frame/video/folder */ | 
				
			||||
 | 
					VPResp Request::query(Vector7D in){ | 
				
			||||
 | 
					    req.body = "{\"vector\": [" + | 
				
			||||
 | 
					            ofToString(in.angry) + "," + | 
				
			||||
 | 
					            ofToString(in.disgust) + "," + | 
				
			||||
 | 
					            ofToString(in.fear) + "," + | 
				
			||||
 | 
					            ofToString(in.happy) + "," + | 
				
			||||
 | 
					            ofToString(in.sad) + "," + | 
				
			||||
 | 
					            ofToString(in.surprise) + "," + | 
				
			||||
 | 
					            ofToString(in.neutral) + "]}"; | 
				
			||||
 | 
					             | 
				
			||||
 | 
					    auto resp = http.handleRequest(req); | 
				
			||||
 | 
					    json_resp = ofJson::parse(resp.data.getText()); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    VPResp vp_resp; | 
				
			||||
 | 
					    vp_resp.folder = json_resp["folder"]; | 
				
			||||
 | 
					    vp_resp.image = json_resp["image"]; | 
				
			||||
 | 
					    vp_resp.video = json_resp["video"]; | 
				
			||||
 | 
					    vp_resp.frame = json_resp["frame"]; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    return vp_resp; | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,44 @@ | 
				
			|||||
 | 
					#ifndef _REQUEST | 
				
			||||
 | 
					#define _REQUEST | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#include "ofMain.h" | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/* emotional embedding */ | 
				
			||||
 | 
					struct Vector7D { | 
				
			||||
 | 
					    float angry; | 
				
			||||
 | 
					    float disgust; | 
				
			||||
 | 
					    float fear; | 
				
			||||
 | 
					    float happy; | 
				
			||||
 | 
					    float sad; | 
				
			||||
 | 
					    float surprise; | 
				
			||||
 | 
					    float neutral; | 
				
			||||
 | 
					     | 
				
			||||
 | 
					    bool operator!=(const Vector7D &other) const { | 
				
			||||
 | 
					        return angry != other.angry || | 
				
			||||
 | 
					               disgust != other.disgust || | 
				
			||||
 | 
					               fear != other.fear || | 
				
			||||
 | 
					               happy != other.happy || | 
				
			||||
 | 
					               sad != other.sad || | 
				
			||||
 | 
					               surprise != other.surprise || | 
				
			||||
 | 
					               neutral != other.neutral; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/* Vantage point query structure */ | 
				
			||||
 | 
					struct VPResp{ | 
				
			||||
 | 
					    std::string folder; | 
				
			||||
 | 
					    std::string video; | 
				
			||||
 | 
					    std::string image; | 
				
			||||
 | 
					    int frame; | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					class Request{ | 
				
			||||
 | 
					    public: | 
				
			||||
 | 
					    void setup(std::string ip, int port, std::string page); | 
				
			||||
 | 
					    VPResp query(Vector7D in); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    ofHttpRequest req; | 
				
			||||
 | 
					    ofURLFileLoader http; | 
				
			||||
 | 
					    ofJson json_resp; | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					#endif | 
				
			||||
@ -0,0 +1,107 @@ | 
				
			|||||
 | 
					#include "Server.h" | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					void Server::start(){ | 
				
			||||
 | 
					    server.setup(port); | 
				
			||||
 | 
					    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(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    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 * 1000.0f) / 1000.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; | 
				
			||||
 | 
					                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 != embedding) {  // 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; | 
				
			||||
 | 
					    } else { | 
				
			||||
 | 
					        // Calculate the time since the last change
 | 
				
			||||
 | 
					        auto now = std::chrono::steady_clock::now(); | 
				
			||||
 | 
					        auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - last_change_time).count(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        if (duration >= 2) { | 
				
			||||
 | 
					            is_active = false; | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    std::cout << is_active << std::endl; | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,40 @@ | 
				
			|||||
 | 
					#ifndef _SERVER | 
				
			||||
 | 
					#define _SERVER | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#include "ofMain.h" | 
				
			||||
 | 
					#include "ofxNetwork.h" | 
				
			||||
 | 
					#include "Request.h" | 
				
			||||
 | 
					#include <unordered_map> | 
				
			||||
 | 
					#include <chrono> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					struct ClientInfo { | 
				
			||||
 | 
					    float value; | 
				
			||||
 | 
					    std::string ip_address; | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					class Server{ | 
				
			||||
 | 
					    public: | 
				
			||||
 | 
					    Server(int port, Vector7D& _embedding, bool debug)  | 
				
			||||
 | 
					        : port(port), embedding(_embedding), debug(debug) {} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    void start(); | 
				
			||||
 | 
					    void update(); | 
				
			||||
 | 
					    void addOrUpdateClient(int client_id, float value, const std::string& ip_address); | 
				
			||||
 | 
					    void printClients(); | 
				
			||||
 | 
					    void updateEmbedding(); | 
				
			||||
 | 
					    void checkActivity(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    int port; | 
				
			||||
 | 
					    ofxTCPServer server; | 
				
			||||
 | 
					    std::unordered_map<int, ClientInfo> clients; | 
				
			||||
 | 
					    bool debug; | 
				
			||||
 | 
					    bool is_active; | 
				
			||||
 | 
					    Vector7D& embedding; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private: | 
				
			||||
 | 
					    Vector7D previous_embedding; | 
				
			||||
 | 
					    std::chrono::time_point<std::chrono::steady_clock> last_change_time; | 
				
			||||
 | 
					    bool hasChanged(); | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#endif | 
				
			||||
					Loading…
					
					
				
		Reference in new issue