summaryrefslogtreecommitdiff
path: root/rotord/rotord.cpp
blob: e794b7d0a77341a399e7791e7feb18902c55da5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "rotord.h"

RotorRequestHandler::RotorRequestHandler(const std::string& format): _format(format){
}

void RotorRequestHandler::handleRequest(HTTPServerRequest& request,HTTPServerResponse& response) {
        
        Timestamp now;
        std::string dt(DateTimeFormatter::format(now, _format));

        response.setChunkedTransferEncoding(true);
        response.setContentType("text/html");

        std::ostream& ostr = response.send();
        ostr << "<html><head><title>RotorServer powered by "
                "POCO C++ Libraries</title>";
        ostr << "</head>";
        ostr << "<body><p style=\"text-align: center; "
                "font-size: 48px;\">";
        ostr << dt;
        ostr << "</p></body></html>";
}

RotorRequestHandlerFactory::RotorRequestHandlerFactory(const std::string& format):_format(format){
}

AudioAnalyserHandler::AudioAnalyserHandler(const vampHost::Settings& _settings): settings(_settings){
}

void AudioAnalyserHandler::handleRequest(HTTPServerRequest& request,HTTPServerResponse& response) {

        response.setChunkedTransferEncoding(true);
        response.setContentType("text/html");
	
	string audioData=vampHost::

        std::ostream& ostr = response.send();
        ostr << "<html><head><title>RotorServer powered by "
                "POCO C++ Libraries</title>";
        ostr << "</head>";
        ostr << "<body><p style=\"text-align: center; "
                "font-size: 48px;\">";
        ostr << dt;
        ostr << "</p></body></html>";
}

AudioAnalyserFactory::AudioAnalyserFactory(const vampHost::Settings& _settings): settings(_settings){
}

HTTPRequestHandler* RotorRequestHandlerFactory::createRequestHandler(const HTTPServerRequest& request){
	Application& app = Application::instance();
	
	Poco::URI theuri=Poco::URI(request.getURI());
	std::vector <std::string> segments;
	theuri.getPathSegments(segments);
	
	std::ostringstream ostr; 
	ostr << segments.size();
	
	std::string out;
	for (uint i=0;i<segments.size();i++) {
		out+=segments[i];
		out+=" ";
	}
	
	app.logger().information("Request from "+ request.clientAddress().toString()+" "+ostr.str()+" segments: "+out);
	
	if (segments.size() == 0) {
	    return new RotorRequestHandler(_format);
	}
	else if (segments[0]=="vamp"&&segments.size()>3) {
			// vamp/plugin/filter/filename
			// how do deal with error condition?
		return new RotorRequestHandler(_format);
		//string audioData=runPlugin(string myname, string soname, string id,
		//      string output, int outputNo, string wavname,
		//      string outfilename, bool useFrames);
		return 0;
	}
	else {
		return 0;
	}
}


RotorServer::RotorServer(): _helpRequested(false)
{
}

RotorServer::~RotorServer()
{
}

void RotorServer::initialize(Application& self){
	loadConfiguration();
	ServerApplication::initialize(self);
}

void RotorServer::uninitialize(){
	ServerApplication::uninitialize();
}

void RotorServer::defineOptions(OptionSet& options) {
	ServerApplication::defineOptions(options);
	options.addOption(
		Option("help", "h", "display argument help information")
		    .required(false)
		    .repeatable(false)
		    .callback(OptionCallback<RotorServer>(this, &RotorServer::handleHelp)
		)
	);
}

void RotorServer::handleHelp(const std::string& name, const std::string& value){
	HelpFormatter helpFormatter(options());
	helpFormatter.setCommand(commandName());
	helpFormatter.setUsage("OPTIONS");
	helpFormatter.setHeader(
	    "Rotor");
	helpFormatter.format(std::cout);
	stopOptionsProcessing();
	_helpRequested = true;
}

int RotorServer::main(const std::vector<std::string>& args){
	if (!_helpRequested) {
		unsigned short port = (unsigned short) config().getInt("port", 9980);
		std::string format(config().getString("format", DateTimeFormat::SORTABLE_FORMAT));
		ServerSocket svs(port);
		HTTPServer srv(new RotorRequestHandlerFactory(format), svs, new HTTPServerParams);
		srv.start();
		waitForTerminationRequest();
		srv.stop();
	}
	return Application::EXIT_OK;
}