/** * Create a new DesignatedControllerConnection. * This listens for connections from the controllers, and will ensure that if * any controllers try to connect, at least one will be picked as the * designated controller. */ DesignatedControllerConnection::DesignatedControllerConnection( ola::io::SelectServerInterface *ss, const IPV4Address &ip_address, ola::e133::MessageBuilder *message_builder, TCPConnectionStats *tcp_stats, unsigned int max_queue_size) : m_ip_address(ip_address), m_max_queue_size(max_queue_size), m_ss(ss), m_message_builder(message_builder), m_tcp_stats(tcp_stats), m_tcp_socket(NULL), m_health_checked_connection(NULL), m_message_queue(NULL), m_incoming_tcp_transport(NULL), m_tcp_socket_factory( NewCallback(this, &DesignatedControllerConnection::NewTCPConnection)), m_listening_tcp_socket(&m_tcp_socket_factory), m_root_inflator( NewCallback(this, &DesignatedControllerConnection::RLPDataReceived)), m_unsent_messages(false) { m_root_inflator.AddInflator(&m_e133_inflator); m_e133_inflator.AddInflator(&m_e133_status_inflator); m_e133_status_inflator.SetStatusHandler( NewCallback(this, &DesignatedControllerConnection::HandleStatusMessage)); }
/* * Check that RemoveWriteDescriptor is reentrant. * Similar to the case above, but this removes & deletes the descriptor from * within the OnRead callback of the same descriptor. */ void SelectServerTest::testRemoveWriteWhenReadable() { // Ownership is transferred to the SelectServer. LoopbackDescriptor *loopback = new LoopbackDescriptor(); loopback->Init(); loopback->SetOnData(NewCallback( this, &SelectServerTest::ReadDataAndRemove, static_cast<ConnectedDescriptor*>(loopback))); loopback->SetOnWritable(NewCallback(this, &SelectServerTest::NullHandler)); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(loopback)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(loopback)); OLA_ASSERT_EQ(2, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(1, write_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); // Send some data to make this descriptor readable. uint8_t data[] = {'a'}; loopback->Send(data, arraysize(data)); m_ss->Run(); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }
/* * Check that we don't invalid iterators by removing descriptors during an * on_Write callback. */ void SelectServerTest::testRemoveOthersWhenWriteable() { Descriptors read_set, write_set, delete_set; LoopbackDescriptor loopback1, loopback2, loopback3; loopback1.Init(); loopback2.Init(); loopback3.Init(); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback1)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback2)); OLA_ASSERT_TRUE(m_ss->AddWriteDescriptor(&loopback3)); write_set.insert(&loopback1); write_set.insert(&loopback2); write_set.insert(&loopback3); loopback1.SetOnWritable(NewCallback( this, &SelectServerTest::NullHandler)); loopback2.SetOnWritable(NewCallback( this, &SelectServerTest::RemoveAndDeleteDescriptors, read_set, write_set, delete_set)); loopback3.SetOnWritable(NewCallback( this, &SelectServerTest::NullHandler)); OLA_ASSERT_EQ(3, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); m_ss->Run(); OLA_ASSERT_EQ(0, write_descriptor_count->Get()); OLA_ASSERT_EQ(1, connected_read_descriptor_count->Get()); OLA_ASSERT_EQ(0, read_descriptor_count->Get()); }
/* * Test the single argument function closures */ void CallbackTest::testFunctionCallbacks1() { // single arg, void return closures BaseCallback1<void, unsigned int> *c1 = NewSingleCallback(&Function1); c1->Run(TEST_INT_VALUE); BaseCallback1<void, unsigned int> *c2 = NewCallback(&Function1); c2->Run(TEST_INT_VALUE); c2->Run(TEST_INT_VALUE); delete c2; // test a function that returns bool BaseCallback1<bool, unsigned int> *c3 = NewSingleCallback(&BoolFunction1); OLA_ASSERT_TRUE(c3->Run(TEST_INT_VALUE)); BaseCallback1<bool, unsigned int> *c4 = NewCallback(&BoolFunction1); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE)); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE)); delete c4; // single arg, void return closures BaseCallback1<void, int> *c6 = NewSingleCallback( &Function2, TEST_INT_VALUE); c6->Run(TEST_INT_VALUE2); BaseCallback1<void, int> *c7 = NewCallback( &Function2, TEST_INT_VALUE); c7->Run(TEST_INT_VALUE2); c7->Run(TEST_INT_VALUE2); delete c7; }
/* * Test the Method Callbacks */ void CallbackTest::testMethodCallbacks4() { // test 2 arg callbacks that return unsigned ints BaseCallback4<void, unsigned int, int, char, const string&> *c1 = NewSingleCallback( this, &CallbackTest::Method4); c1->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE); BaseCallback4<void, unsigned int, int, char, const string&> *c2 = NewCallback( this, &CallbackTest::Method4); c2->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE); c2->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE); delete c2; // test 2 arg callbacks that return bools BaseCallback4<bool, unsigned int, int, char, const string&> *c3 = NewSingleCallback( this, &CallbackTest::BoolMethod4); OLA_ASSERT_TRUE(c3->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE)); BaseCallback4<bool, unsigned int, int, char, const string&> *c4 = NewCallback( this, &CallbackTest::BoolMethod4); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE)); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE, TEST_INT_VALUE2, TEST_CHAR_VALUE, TEST_STRING_VALUE)); delete c4; }
/** * Create a new E133Receiver. * @param socket the UDP socket to read from * @param status_callback the callback to run when status messages are * received. * @param rdm_callback the callback to run when RDM messages are received. */ E133Receiver::E133Receiver(ola::network::UDPSocket *socket, StatusCallback *status_callback, RDMCallback *rdm_callback) : m_udp_socket(socket), m_status_callback(status_callback), m_rdm_callback(rdm_callback), m_root_inflator(new plugin::e131::RootInflator()), m_e133_inflator(new plugin::e131::E133Inflator()), m_rdm_inflator(new plugin::e131::RDMInflator()), m_e133_status_inflator(new plugin::e131::E133StatusInflator()), m_incoming_udp_transport( new plugin::e131::IncomingUDPTransport( m_udp_socket, m_root_inflator.get())) { m_root_inflator->AddInflator(m_e133_inflator.get()); m_e133_inflator->AddInflator(m_rdm_inflator.get()); m_e133_inflator->AddInflator(m_e133_status_inflator.get()); m_rdm_inflator->SetRDMHandler( NewCallback(this, &E133Receiver::HandlePacket)); m_e133_status_inflator->SetStatusHandler( NewCallback(this, &E133Receiver::HandleStatusMessage)); m_udp_socket->SetOnData( NewCallback(m_incoming_udp_transport.get(), &ola::plugin::e131::IncomingUDPTransport::Receive)); }
void ArtNetInputPort::PostSetUniverse(Universe *old_universe, Universe *new_universe) { if (new_universe) m_node->SetOutputPortUniverse(PortId(), new_universe->UniverseId() % 0xf); else m_node->DisableOutputPort(PortId()); if (new_universe && !old_universe) { m_node->SetDMXHandler( PortId(), &m_buffer, NewCallback(static_cast<ola::BasicInputPort*>(this), &ArtNetInputPort::DmxChanged)); m_node->SetOutputPortRDMHandlers( PortId(), NewCallback( this, &ArtNetInputPort::RespondWithTod), NewCallback( this, &ArtNetInputPort::TriggerDiscovery), ola::NewCallback( static_cast<ola::BasicInputPort*>(this), &ArtNetInputPort::HandleRDMRequest)); } else if (!new_universe) { m_node->SetDMXHandler(PortId(), NULL, NULL); m_node->SetOutputPortRDMHandlers(PortId(), NULL, NULL, NULL); } if (new_universe) TriggerRDMDiscovery( NewSingleCallback(this, &ArtNetInputPort::SendTODWithUIDs)); }
/* * Test the Method Callbacks */ void CallbackTest::testMethodCallbacks2() { // test 2 arg callbacks that return void BaseCallback2<void, unsigned int, int> *c1 = NewSingleCallback( this, &CallbackTest::Method2); c1->Run(TEST_INT_VALUE, TEST_INT_VALUE2); BaseCallback2<void, unsigned int, int> *c2 = NewCallback( this, &CallbackTest::Method2); c2->Run(TEST_INT_VALUE, TEST_INT_VALUE2); c2->Run(TEST_INT_VALUE, TEST_INT_VALUE2); delete c2; // test 2 arg callbacks that return bools BaseCallback2<bool, unsigned int, int> *c3 = NewSingleCallback( this, &CallbackTest::BoolMethod2); OLA_ASSERT_TRUE(c3->Run(TEST_INT_VALUE, TEST_INT_VALUE2)); BaseCallback2<bool, unsigned int, int> *c4 = NewCallback( this, &CallbackTest::BoolMethod2); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE, TEST_INT_VALUE2)); OLA_ASSERT_TRUE(c4->Run(TEST_INT_VALUE, TEST_INT_VALUE2)); delete c4; // test 1 create time, 2 run time arg callbacks that return void BaseCallback2<void, int, char> *c5 = NewSingleCallback( this, &CallbackTest::Method3, TEST_INT_VALUE); c5->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE); BaseCallback2<void, int, char> *c6 = NewCallback( this, &CallbackTest::Method3, TEST_INT_VALUE); c6->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE); c6->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE); delete c6; // test 1 create time, 2 run time arg callbacks that return bools BaseCallback2<bool, int, char> *c7 = NewSingleCallback( this, &CallbackTest::BoolMethod3, TEST_INT_VALUE); OLA_ASSERT_TRUE(c7->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE)); BaseCallback2<bool, int, char> *c8 = NewCallback( this, &CallbackTest::BoolMethod3, TEST_INT_VALUE); OLA_ASSERT_TRUE(c8->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE)); OLA_ASSERT_TRUE(c8->Run(TEST_INT_VALUE2, TEST_CHAR_VALUE)); delete c8; }
SimpleE133Device::SimpleE133Device(const Options &options) : m_controller(options.controller), m_message_builder(ola::acn::CID::Generate(), "E1.33 Device"), m_tcp_socket_factory(NewCallback(this, &SimpleE133Device::OnTCPConnect)), m_connector(&m_ss, &m_tcp_socket_factory, TimeInterval(FLAGS_tcp_connect_timeout_ms / 1000, (FLAGS_tcp_connect_timeout_ms % 1000) * 1000)), m_backoff_policy(TimeInterval( FLAGS_tcp_retry_interval_ms / 1000, (FLAGS_tcp_retry_interval_ms % 1000) * 1000)), m_root_inflator(NewCallback(this, &SimpleE133Device::RLPDataReceived)) { m_connector.AddEndpoint(options.controller, &m_backoff_policy); }
/** * Check that we send OSC messages correctly. */ void OSCNodeTest::testSendBlob() { // First up create a UDP socket to receive the messages on. // Port 0 means 'ANY' IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0); // Bind the socket, set the callback, and register with the select server. OLA_ASSERT_TRUE(m_udp_socket.Bind(socket_address)); m_udp_socket.SetOnData(NewCallback(this, &OSCNodeTest::UDPSocketReady)); OLA_ASSERT_TRUE(m_ss.AddReadDescriptor(&m_udp_socket)); // Store the local address of the UDP socket so we know where to tell the // OSCNode to send to. OLA_ASSERT_TRUE(m_udp_socket.GetSocketAddress(&socket_address)); // Setup the OSCTarget pointing to the local socket address OSCTarget target(socket_address, TEST_OSC_ADDRESS); // Add the target to the node. m_osc_node->AddTarget(TEST_GROUP, target); // Send the data OLA_ASSERT_TRUE(m_osc_node->SendData(TEST_GROUP, OSCNode::FORMAT_BLOB, m_dmx_data)); // Run the SelectServer this will return either when UDPSocketReady // completes, or the abort timeout triggers. m_ss.Run(); // Remove target OLA_ASSERT_TRUE(m_osc_node->RemoveTarget(TEST_GROUP, target)); // Try to remove it a second time OLA_ASSERT_FALSE(m_osc_node->RemoveTarget(TEST_GROUP, target)); // Try to remove the target from a group that doesn't exist OLA_ASSERT_FALSE(m_osc_node->RemoveTarget(TEST_GROUP + 1, target)); }
/* * Main. */ int main(int argc, char **argv) { ola::AppInit(&argc, argv, "[ options ]", "Ja Rule Admin Tool"); auto_ptr<UID> controller_uid(UID::FromString(FLAGS_controller_uid)); if (!controller_uid.get()) { OLA_WARN << "Invalid Controller UID: '" << FLAGS_controller_uid << "'"; exit(ola::EXIT_USAGE); } if (controller_uid->IsBroadcast()) { OLA_WARN << "The controller UID should not be a broadcast UID"; exit(ola::EXIT_USAGE); } SelectServer ss; WidgetManager widget_manager(*controller_uid); InputHandler input_handler(&ss, *controller_uid, &widget_manager); USBDeviceManager manager( &ss, NewCallback(&widget_manager, &WidgetManager::WidgetEvent)); if (!manager.Start()) { exit(ola::EXIT_UNAVAILABLE); } ss.Run(); return ola::EXIT_OK; }
/* * Check returning false from a repeating timeout cancels the timeout. */ void TimeoutManagerTest::testAbortedRepeatingTimeouts() { MockClock clock; TimeoutManager timeout_manager(&m_map, &clock); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); TimeInterval timeout_interval(1, 0); timeout_id id1 = timeout_manager.RegisterRepeatingTimeout( timeout_interval, NewCallback(this, &TimeoutManagerTest::HandleAbortedEvent, 1u)); OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT); TimeStamp last_checked_time; clock.AdvanceTime(0, 1); // Small offset to work around timer precision clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(1u, GetEventCounter(1)); clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(2u, GetEventCounter(1)); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); }
/** * Check pausing doesn't mark the channel as bad. */ void HealthCheckedConnectionTest::testPauseAndResume() { MockHealthCheckedConnection connection(&socket, &m_ss, heartbeat_interval, options, &m_clock); socket.SetOnData( NewCallback(&connection, &MockHealthCheckedConnection::ReadData)); connection.Setup(); m_ss.AddReadDescriptor(&socket); connection.Setup(); m_ss.RegisterSingleTimeout( TimeInterval(1, 0), NewSingleCallback(this, &HealthCheckedConnectionTest::PauseReading, &connection)); m_ss.RegisterSingleTimeout( TimeInterval(3, 0), NewSingleCallback(this, &HealthCheckedConnectionTest::ResumeReading, &connection)); m_ss.Run(); OLA_ASSERT_TRUE(connection.ChannelOk()); }
void AvahiDiscoveryAgent::AddMaster(AvahiIfIndex interface, AvahiProtocol protocol, const std::string &name, const std::string &type, const std::string &domain) { OLA_INFO << "(Browser) NEW: service " << name << " of type " << type << " in domain " << domain << ", iface" << interface << ", proto " << protocol; MutexLocker lock(&m_masters_mu); auto_ptr<MasterResolver> master(new MasterResolver( NewCallback(this, &AvahiDiscoveryAgent::MasterChanged), m_client.get(), interface, protocol, name, type, domain)); // We get the callback multiple times for the same instance MasterResolverList::iterator iter = m_masters.begin(); for (; iter != m_masters.end(); ++iter) { if ((**iter) == *master) { return; } } if (master->StartResolution()) { MasterEntry entry; master->GetMasterEntry(&entry); m_masters.push_back(master.release()); m_master_callback->Run(MASTER_ADDED, entry); } }
/* * Startup the server. */ int main(int argc, char *argv[]) { std::ostringstream help_msg; help_msg << "The OLA SLP client.\n" "\n" "Examples:\n" " " << argv[0] << " register service:myserv.x://myhost.com\n" " " << argv[0] << " findsrvs service:myserv.x\n" " " << argv[0] << " findsrvs service:myserv.x"; ola::AppInit(&argc, argv, "[options] command-and-arguments", help_msg.str()); vector<string> args; for (int i = 1; i < argc; i++) { args.push_back(argv[i]); } auto_ptr<Command> command(CreateCommand(args)); if (!command.get()) { ola::DisplayUsage(); exit(ola::EXIT_OK); } ola::slp::SLPClientWrapper client_wrapper; if (!client_wrapper.Setup()) exit(1); SelectServer *ss = client_wrapper.GetSelectServer(); command->SetTermination(NewCallback(ss, &SelectServer::Terminate)); if (!command->Execute(client_wrapper.GetClient())) exit(1); ss->Run(); }
/** * Init the device. */ bool E133Device::Init() { OLA_INFO << "Attempting to start E1.33 device at " << m_ip_address; // setup the TCP socket bool listen_ok = m_tcp_socket.Listen(m_ip_address, ola::plugin::e131::E133_PORT); if (!listen_ok) { m_tcp_socket.Close(); return false; } // setup the UDP socket if (!m_udp_socket.Init()) { m_tcp_socket.Close(); return false; } if (!m_udp_socket.Bind(ola::plugin::e131::E133_PORT)) { m_tcp_socket.Close(); return false; } m_udp_socket.SetOnData( NewCallback(&m_incoming_udp_transport, &ola::plugin::e131::IncomingUDPTransport::Receive)); // add both to the Select Server m_ss->AddReadDescriptor(&m_udp_socket); m_ss->AddReadDescriptor(&m_tcp_socket); return true; }
/** * Init the device. */ bool E133Device::Init() { if (m_controller_connection.get()) { OLA_WARN << "Init already performed"; return false; } OLA_INFO << "Attempting to start E1.33 device at " << m_ip_address; m_controller_connection.reset(new DesignatedControllerConnection( m_ss, m_ip_address, &m_message_builder, &m_tcp_stats)); if (!m_controller_connection->Init()) { m_controller_connection.reset(); return false; } // setup the UDP socket if (!m_udp_socket.Init()) { m_controller_connection.reset(); return false; } if (!m_udp_socket.Bind(IPV4SocketAddress(IPV4Address::WildCard(), ola::acn::E133_PORT))) { m_controller_connection.reset(); return false; } m_udp_socket.SetOnData( NewCallback(&m_incoming_udp_transport, &ola::plugin::e131::IncomingUDPTransport::Receive)); m_ss->AddReadDescriptor(&m_udp_socket); return true; }
void SimpleE133Device::OnTCPConnect(TCPSocket *socket) { OLA_INFO << "Opened new TCP connection: " << socket; m_socket.reset(socket); m_in_transport.reset(new IncomingTCPTransport(&m_root_inflator, socket)); m_message_queue.reset( new MessageQueue(m_socket.get(), &m_ss, m_message_builder.pool())); m_health_checked_connection.reset(new E133HealthCheckedConnection( &m_message_builder, m_message_queue.get(), NewSingleCallback(this, &SimpleE133Device::SocketClosed), &m_ss)); socket->SetOnData(NewCallback(this, &SimpleE133Device::ReceiveTCPData)); socket->SetOnClose(NewSingleCallback(this, &SimpleE133Device::SocketClosed)); m_ss.AddReadDescriptor(socket); if (!m_health_checked_connection->Setup()) { OLA_WARN << "Failed to setup heartbeat controller for " << m_controller; SocketClosed(); return; } }
/** * Setup a new Monitor */ SimpleE133Monitor::SimpleE133Monitor(PidStoreHelper *pid_helper, bool enable_slp) : m_command_printer(&cout, pid_helper), m_stdin_handler(&m_ss, ola::NewCallback(this, &SimpleE133Monitor::Input)), m_message_builder(ola::acn::CID::Generate(), "OLA Monitor"), m_device_manager(&m_ss, &m_message_builder) { if (enable_slp) { m_slp_thread.reset(ola::e133::SLPThreadFactory::NewSLPThread(&m_ss)); m_slp_thread->SetNewDeviceCallback( NewCallback(this, &SimpleE133Monitor::DiscoveryCallback)); } m_device_manager.SetRDMMessageCallback( NewCallback(this, &SimpleE133Monitor::EndpointRequest)); // TODO(simon): add a controller discovery callback here as well. }
void OnTCPConnect(TCPSocket *socket) { OLA_INFO << "New connection: " << socket; socket->SetOnData( NewCallback(this, &Server::ReceiveTCPData, socket)); socket->SetOnClose( NewSingleCallback(this, &Server::SocketClosed, socket)); m_ss.AddReadDescriptor(socket); m_sockets.push_back(socket); }
/** * Setup a new Monitor */ SimpleE133Monitor::SimpleE133Monitor(PidStoreHelper *pid_helper) : m_command_printer(&cout, pid_helper), m_stdin_handler(&m_ss, ola::NewCallback(this, &SimpleE133Monitor::Input)), m_message_builder(ola::acn::CID::Generate(), "OLA Monitor"), m_device_manager(&m_ss, &m_message_builder) { m_device_manager.SetRDMMessageCallback( NewCallback(this, &SimpleE133Monitor::EndpointRequest)); }
/** * Set the Root Endpoint, ownership is not transferred */ void E133Device::SetRootEndpoint(E133EndpointInterface *endpoint) { m_root_endpoint = endpoint; // register the root enpoint m_rdm_inflator.SetRDMHandler( 0, NewCallback(this, &E133Device::EndpointRequest, static_cast<uint16_t>(0))); }
/** * Setup our simple controller */ SimpleE133Controller::SimpleE133Controller( const Options &options, PidStoreHelper *pid_helper) : m_controller_ip(options.controller_ip), m_message_builder(ola::acn::CID::Generate(), "E1.33 Controller"), m_e133_receiver( &m_udp_socket, NewCallback(this, &SimpleE133Controller::HandleStatusMessage), NewCallback(this, &SimpleE133Controller::HandlePacket)), m_src_uid(OPEN_LIGHTING_ESTA_CODE, 0xabcdabcd), m_pid_helper(pid_helper), m_command_printer(&cout, m_pid_helper), m_uid_list_updated(false) { if (options.use_slp) { m_slp_thread.reset(ola::e133::SLPThreadFactory::NewSLPThread(&m_ss)); m_slp_thread->SetNewDeviceCallback( ola::NewCallback(this, &SimpleE133Controller::DiscoveryCallback)); } }
explicit Server(const IPV4Address &listen_ip) : m_listen_ip(listen_ip), m_tcp_socket_factory( ola::NewCallback(this, &Server::OnTCPConnect)), m_listen_socket(&m_tcp_socket_factory), m_is_master(false), m_update_timeout(ola::thread::INVALID_TIMEOUT) { m_update_timeout = m_ss.RegisterRepeatingTimeout( 1000, NewCallback(this, &Server::UpdateClients)); }
void SLPDaemon::Run() { #ifdef HAVE_LIBMICROHTTPD if (m_http_server.get()) m_http_server->Start(); #endif ola::thread::timeout_id cleanup_timeout = m_ss.RegisterRepeatingTimeout( 2000, NewCallback(this, &SLPDaemon::CleanOldClients)); m_ss.Run(); m_ss.RemoveTimeout(cleanup_timeout); CleanOldClients(); }
E133Device::E133Device(ola::io::SelectServerInterface *ss, const ola::network::IPV4Address &ip_address, EndpointManager *endpoint_manager, TCPConnectionStats *tcp_stats) : m_endpoint_manager(endpoint_manager), m_register_endpoint_callback(NULL), m_unregister_endpoint_callback(NULL), m_root_endpoint(NULL), m_tcp_stats(tcp_stats), m_cid(ola::plugin::e131::CID::Generate()), m_tcp_descriptor(NULL), m_outgoing_tcp_transport(NULL), m_health_checked_connection(NULL), m_ss(ss), m_ip_address(ip_address), m_tcp_socket_factory(NewCallback(this, &E133Device::NewTCPConnection)), m_tcp_socket(&m_tcp_socket_factory), m_root_inflator(NewCallback(this, &E133Device::RLPDataReceived)), m_incoming_udp_transport(&m_udp_socket, &m_root_inflator), m_outgoing_udp_transport(&m_udp_socket), m_incoming_tcp_transport(NULL), m_root_sender(m_cid), m_e133_sender(&m_root_sender, string("OLA Device")) { m_root_inflator.AddInflator(&m_e133_inflator); m_e133_inflator.AddInflator(&m_rdm_inflator); m_register_endpoint_callback.reset(NewCallback( this, &E133Device::RegisterEndpoint)); m_unregister_endpoint_callback.reset(NewCallback( this, &E133Device::UnRegisterEndpoint)); m_endpoint_manager->RegisterNotification( EndpointManager::ADD, m_register_endpoint_callback.get()); m_endpoint_manager->RegisterNotification( EndpointManager::REMOVE, m_unregister_endpoint_callback.get()); }
/** * Setup a new SLP server. * @param socket the UDP Socket to use for SLP messages. * @param options the SLP Server options. */ SLPDaemon::SLPDaemon(ola::network::UDPSocket *udp_socket, ola::network::TCPAcceptingSocket *tcp_socket, const SLPDaemonOptions &options, ola::ExportMap *export_map) : m_ss(export_map, &m_clock), m_slp_server(&m_ss, udp_socket, tcp_socket, export_map, options), m_stdin_handler(&m_ss, NewCallback(this, &SLPDaemon::Input)), m_rpc_port(options.rpc_port), m_rpc_socket_factory(NewCallback(this, &SLPDaemon::NewTCPConnection)), m_rpc_accept_socket(&m_rpc_socket_factory), m_service_impl(new SLPServiceImpl(&m_slp_server)), m_export_map(export_map) { #ifdef HAVE_LIBMICROHTTPD if (options.enable_http) { ola::http::HTTPServer::HTTPServerOptions http_options; http_options.port = options.http_port; m_http_server.reset( new ola::http::OlaHTTPServer(http_options, m_export_map)); } #endif }
/* * Test the Function Callbacks class */ void CallbackTest::testFunctionCallbacks() { // no arg, void return closures SingleUseCallback0<void> *c1 = NewSingleCallback(&Function0); c1->Run(); Callback0<void> *c2 = NewCallback(&Function0); c2->Run(); c2->Run(); delete c2; // no arg, bool closures SingleUseCallback0<bool> *c3 = NewSingleCallback(&BoolFunction0); OLA_ASSERT_TRUE(c3->Run()); Callback0<bool> *c4 = NewCallback(&BoolFunction0); OLA_ASSERT_TRUE(c4->Run()); OLA_ASSERT_TRUE(c4->Run()); delete c4; // one arg, void return SingleUseCallback0<void> *c5 = NewSingleCallback( &Function1, TEST_INT_VALUE); c5->Run(); Callback0<void> *c6 = NewCallback(&Function1, TEST_INT_VALUE); c6->Run(); c6->Run(); delete c6; // one arg, bool closures SingleUseCallback0<bool> *c7 = NewSingleCallback( &BoolFunction1, TEST_INT_VALUE); OLA_ASSERT_TRUE(c7->Run()); Callback0<bool> *c8 = NewCallback(&BoolFunction1, TEST_INT_VALUE); OLA_ASSERT_TRUE(c8->Run()); OLA_ASSERT_TRUE(c8->Run()); delete c8; }
/* * Check RegisterRepeatingTimeout works. */ void TimeoutManagerTest::testRepeatingTimeouts() { MockClock clock; TimeoutManager timeout_manager(&m_map, &clock); OLA_ASSERT_FALSE(timeout_manager.EventsPending()); TimeInterval timeout_interval(1, 0); timeout_id id1 = timeout_manager.RegisterRepeatingTimeout( timeout_interval, NewCallback(this, &TimeoutManagerTest::HandleRepeatingEvent, 1u)); OLA_ASSERT_NE(id1, ola::thread::INVALID_TIMEOUT); TimeStamp last_checked_time; clock.AdvanceTime(0, 1); // Small offset to work around timer precision clock.CurrentTime(&last_checked_time); TimeInterval next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, timeout_interval); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_EQ(0u, GetEventCounter(1)); OLA_ASSERT_LT(next, TimeInterval(0, 500000)); clock.AdvanceTime(0, 500000); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_LTE(next, timeout_interval); OLA_ASSERT_EQ(1u, GetEventCounter(1)); OLA_ASSERT_TRUE(timeout_manager.EventsPending()); // fire the event again clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_LTE(next, timeout_interval); OLA_ASSERT_EQ(2u, GetEventCounter(1)); // cancel the event timeout_manager.CancelTimeout(id1); clock.AdvanceTime(1, 0); clock.CurrentTime(&last_checked_time); next = timeout_manager.ExecuteTimeouts(&last_checked_time); OLA_ASSERT_TRUE(next.IsZero()); OLA_ASSERT_EQ(2u, GetEventCounter(1)); }
/** * Check the channel works when every 2nd heartbeat is lost */ void HealthCheckedConnectionTest::testChannelWithPacketLoss() { options.send_every = 2; MockHealthCheckedConnection connection(&socket, &m_ss, heartbeat_interval, options, &m_clock); socket.SetOnData( NewCallback(&connection, &MockHealthCheckedConnection::ReadData)); connection.Setup(); m_ss.AddReadDescriptor(&socket); connection.Setup(); m_ss.Run(); OLA_ASSERT_TRUE(connection.ChannelOk()); }