File: SandNetDevice.cpp

package info (click to toggle)
ola 0.10.9.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,440 kB
  • sloc: cpp: 132,294; python: 14,445; javascript: 7,109; sh: 4,713; ansic: 2,189; java: 518; xml: 253; makefile: 175
file content (157 lines) | stat: -rw-r--r-- 4,208 bytes parent folder | download | duplicates (3)
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
151
152
153
154
155
156
157
/*
 * 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 2 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 Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * SandNetDevice.cpp
 * SandNet device
 * Copyright (C) 2005 Simon Newton
 */

#include <sstream>
#include <string>
#include <vector>

#include "ola/Logging.h"
#include "ola/network/NetworkUtils.h"
#include "olad/Plugin.h"
#include "olad/PluginAdaptor.h"
#include "olad/Preferences.h"
#include "plugins/sandnet/SandNetDevice.h"
#include "plugins/sandnet/SandNetPlugin.h"
#include "plugins/sandnet/SandNetPort.h"

namespace ola {
namespace plugin {
namespace sandnet {

using std::ostringstream;
using std::string;
using std::vector;

const char SandNetDevice::IP_KEY[] = "ip";
const char SandNetDevice::NAME_KEY[] = "name";
const char SandNetDevice::SANDNET_DEVICE_NAME[] = "SandNet";

/*
 * Create a new device
 */
SandNetDevice::SandNetDevice(SandNetPlugin *owner,
                             Preferences *prefs,
                             PluginAdaptor *plugin_adaptor):
  Device(owner, SANDNET_DEVICE_NAME),
  m_preferences(prefs),
  m_plugin_adaptor(plugin_adaptor),
  m_node(NULL),
  m_timeout_id(ola::thread::INVALID_TIMEOUT) {
}


/*
 * Start this device
 */
bool SandNetDevice::StartHook() {
  vector<ola::network::UDPSocket*> sockets;
  vector<ola::network::UDPSocket*>::iterator iter;

  m_node = new SandNetNode(m_preferences->GetValue(IP_KEY));
  m_node->SetName(m_preferences->GetValue(NAME_KEY));

  // setup the output ports (ie INTO sandnet)
  for (int i = 0; i < SANDNET_MAX_PORTS; i++) {
    bool ret = m_node->SetPortParameters(i,
                                         SandNetNode::SANDNET_PORT_MODE_IN,
                                         0,
                                         i);
    if (!ret) {
      OLA_WARN << "SetPortParameters failed";
      DeleteAllPorts();
      delete m_node;
      return false;
    }
  }

  if (!m_node->Start()) {
    DeleteAllPorts();
    delete m_node;
    return false;
  }

  ostringstream str;
  str << SANDNET_DEVICE_NAME << " [" << m_node->GetInterface().ip_address <<
    "]";
  SetName(str.str());


  for (unsigned int i = 0; i < INPUT_PORTS; i++) {
    SandNetInputPort *port = new SandNetInputPort(
        this,
        i,
        m_plugin_adaptor,
        m_node);
    AddPort(port);
  }
  for (unsigned int i = 0; i < SANDNET_MAX_PORTS ; i++) {
    SandNetOutputPort *port = new SandNetOutputPort(this, i, m_node);
    AddPort(port);
  }

  sockets = m_node->GetSockets();
  for (iter = sockets.begin(); iter != sockets.end(); ++iter)
    m_plugin_adaptor->AddReadDescriptor(*iter);

  m_timeout_id = m_plugin_adaptor->RegisterRepeatingTimeout(
      ADVERTISEMENT_PERIOD_MS,
      NewCallback(this, &SandNetDevice::SendAdvertisement));

  return true;
}


/*
 * Stop this device
 */
void SandNetDevice::PrePortStop() {
  vector<ola::network::UDPSocket*> sockets = m_node->GetSockets();
  vector<ola::network::UDPSocket*>::iterator iter;
  for (iter = sockets.begin(); iter != sockets.end(); ++iter)
    m_plugin_adaptor->RemoveReadDescriptor(*iter);

  if (m_timeout_id != ola::thread::INVALID_TIMEOUT) {
    m_plugin_adaptor->RemoveTimeout(m_timeout_id);
    m_timeout_id = ola::thread::INVALID_TIMEOUT;
  }
}


/*
 * Stop this device
 */
void SandNetDevice::PostPortStop() {
  m_node->Stop();
  delete m_node;
  m_node = NULL;
}


/*
 * Called periodically to send advertisements.
 */
bool SandNetDevice::SendAdvertisement() {
  OLA_DEBUG << "Sending Sandnet advertisement";
  m_node->SendAdvertisement();
  return true;
}
}  // namespace sandnet
}  // namespace plugin
}  // namespace ola