-
Notifications
You must be signed in to change notification settings - Fork 16
/
controller-state.cc
150 lines (131 loc) · 5.08 KB
/
controller-state.cc
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// This file is part of UPnP LCD Display
//
// Copyright (C) 2013 Henner Zeller <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "controller-state.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "observer.h"
#include "renderer-state.h"
#include <upnptools.h>
// Prefix, as they can be followed by version number.
static const char kMediaRendererDevicePrefix[] =
"urn:schemas-upnp-org:device:MediaRenderer:";
ControllerState::ControllerState(const char *interface_name,
ControllerObserver *observer,
Printer *printer, FILE *logstream)
: observer_(observer), logstream_(logstream) {
assert(observer != NULL); // without, it wouldn't make much sense.
pthread_mutex_init(&mutex_, NULL);
char buffer[40];
snprintf(buffer, sizeof(buffer), "Interface: %s",
interface_name ? interface_name : "any");
printer->Print(0, "Network connect.");
printer->Print(1, buffer);
int rc = UpnpInit2(interface_name, 0);
int retries_left = 60;
static const int kRetryTimeMs = 1000;
// If network is not up yet, UpnpInit2() fails. Retry.
// This can happen if system just booted and DHCP is not settled yet.
while (rc != UPNP_E_SUCCESS && retries_left--) {
usleep(kRetryTimeMs * 1000);
snprintf(buffer, sizeof(buffer), "Network...%d", retries_left);
printer->Print(0, buffer);
fprintf(logstream, "UpnpInit2() Error: %s (%d). Retrying...(%ds)",
UpnpGetErrorMessage(rc), rc, retries_left);
rc = UpnpInit2(interface_name, 0);
}
if (rc != UPNP_E_SUCCESS) {
fprintf(logstream, "UpnpInit2() Error: %s (%d).", UpnpGetErrorMessage(rc), rc);
} else {
snprintf(buffer, sizeof(buffer), "%s:%d",
UpnpGetServerIpAddress(),
UpnpGetServerPort());
printer->Print(0, buffer);
if (UpnpGetServerIp6Address() && *UpnpGetServerIp6Address()) {
snprintf(buffer, sizeof(buffer), "%s:%d",
UpnpGetServerIp6Address(),
UpnpGetServerPort6());
printer->Print(1, buffer);
}
}
UpnpRegisterClient(&UpnpEventHandler, this, &device_);
}
static bool prefixMatch(const char *str, const char *prefix) {
return strncmp(str, prefix, strlen(prefix)) == 0;
}
void ControllerState::Register(const UpnpDiscovery *discovery) {
if (!prefixMatch(UpnpDiscovery_get_DeviceType_cstr(discovery),
kMediaRendererDevicePrefix)) {
return;
}
const std::string uuid = UpnpDiscovery_get_DeviceID_cstr(discovery);
RendererState *renderer = NULL;
pthread_mutex_lock(&mutex_);
renderer = uuid2render_[uuid];
if (renderer == NULL) {
renderer = new RendererState(UpnpDiscovery_get_Location_cstr(discovery),
logstream_);
uuid2render_[uuid] = renderer;
if (renderer->InitDescription(UpnpDiscovery_get_Location_cstr(discovery))) {
renderer->SubscribeTo(device_, &subscription2render_);
}
observer_->AddRenderer(uuid, renderer);
}
pthread_mutex_unlock(&mutex_);
}
void ControllerState::Unregister(const UpnpDiscovery *discovery) {
const std::string uuid = UpnpDiscovery_get_DeviceID_cstr(discovery);
pthread_mutex_lock(&mutex_);
RenderMap::iterator found = uuid2render_.find(uuid);
if (found != uuid2render_.end()) {
observer_->RemoveRenderer(uuid);
delete found->second;
uuid2render_.erase(uuid);
}
pthread_mutex_unlock(&mutex_);
}
void ControllerState::ReceiveEvent(const UpnpEvent *data) {
const std::string sid = UpnpEvent_get_SID_cstr(data);
pthread_mutex_lock(&mutex_);
RenderMap::iterator found = subscription2render_.find(sid);
if (found != subscription2render_.end()) {
found->second->ReceiveEvent(data);
}
pthread_mutex_unlock(&mutex_);
}
int ControllerState::UpnpEventHandler(Upnp_EventType_e event,
const void *event_data,
void *userdata) {
ControllerState *state = static_cast<ControllerState*>(userdata);
switch (event) {
case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
state->Register(static_cast<const UpnpDiscovery*>(event_data));
break;
case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
case UPNP_EVENT_AUTORENEWAL_FAILED:
state->Unregister(static_cast<const UpnpDiscovery*>(event_data));
break;
case UPNP_EVENT_RECEIVED:
state->ReceiveEvent(static_cast<const UpnpEvent*>(event_data));
break;
default:
// don't care.
;
}
return UPNP_E_SUCCESS;
}