Newer
Older
/*
* Open Pixel Control server for Fadecandy
*
* Copyright (c) 2013 Micah Elizabeth Scott
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <netdb.h>
#include <ctype.h>
FCServer::FCServer(rapidjson::Document &config)
: mListen(config["listen"]),
mColor(config["color"]),
mDevices(config["devices"]),
mOPCSink(opcCallback, this, mVerbose),
mUSB(0)
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
/*
* Parse the listen [host, port] list.
*/
if (mListen.IsArray() && mListen.Size() == 2) {
const Value &host = mListen[0u];
const Value &port = mListen[1];
const char *hostStr = 0;
std::ostringstream portStr;
if (host.IsString()) {
hostStr = host.GetString();
} else if (!host.IsNull()) {
mError << "Hostname in 'listen' must be null (any) or a hostname string.\n";
}
if (port.IsUint()) {
portStr << port.GetUint();
} else {
mError << "The 'listen' port must be an integer.\n";
}
struct addrinfo hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(hostStr, portStr.str().c_str(), &hints, &mListenAddr) || !mListenAddr) {
mError << "Failed to resolve hostname '" << host.GetString() << "'\n";
}
} else {
mError << "The required 'listen' configuration key must be a [host, port] list.\n";
}
}
FCServer::~FCServer()
{
if (mListenAddr) {
freeaddrinfo(mListenAddr);
}
void FCServer::start(struct ev_loop *loop)
mOPCSink.start(loop, mListenAddr);
startUSB(loop);
}
void FCServer::startUSB(struct ev_loop *loop)
{
if (libusb_init(&mUSB)) {
std::clog << "Error initializing USB library!\n";
return;
}
mUSBEvent.init(mUSB, loop);
void FCServer::opcCallback(OPCSink::Message &msg, void *context)
{
FCServer *self = static_cast<FCServer*>(context);
printf("Msg %d bytes\n", msg.length());
}