1. 新建车牌识别程序。
parent
755cefd0fb
commit
f77169189b
@ -0,0 +1,198 @@
|
|||||||
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @file Publisher.cxx
|
||||||
|
* This file contains the implementation of the publisher functions.
|
||||||
|
*
|
||||||
|
* This file was generated by the tool fastddsgen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Publisher.hpp"
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <csignal>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
|
||||||
|
#include <fastdds/dds/log/Log.hpp>
|
||||||
|
#include <fastdds/dds/publisher/DataWriter.hpp>
|
||||||
|
#include <fastdds/dds/publisher/Publisher.hpp>
|
||||||
|
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
|
||||||
|
#include <fastdds/dds/publisher/qos/PublisherQos.hpp>
|
||||||
|
|
||||||
|
#include "SystemPubSubTypes.hpp"
|
||||||
|
#include "json.hpp"
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
using namespace eprosima::fastdds::dds;
|
||||||
|
|
||||||
|
PublisherApp::PublisherApp(
|
||||||
|
const int& domain_id)
|
||||||
|
: factory_(nullptr)
|
||||||
|
, participant_(nullptr)
|
||||||
|
, publisher_(nullptr)
|
||||||
|
, topic_(nullptr)
|
||||||
|
, writer_(nullptr)
|
||||||
|
, type_(new LicencePlateRspPubSubType())
|
||||||
|
, matched_(0)
|
||||||
|
, samples_sent_(0)
|
||||||
|
, stop_(false)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
|
||||||
|
// Create the participant
|
||||||
|
DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
|
||||||
|
pqos.name("HttpServer_pub_participant");
|
||||||
|
pqos.wire_protocol().builtin.discovery_config.leaseDuration = Duration_t(60, 0);
|
||||||
|
pqos.wire_protocol().builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(30, 0);
|
||||||
|
factory_ = DomainParticipantFactory::get_shared_instance();
|
||||||
|
participant_ = factory_->create_participant(domain_id, pqos, nullptr, StatusMask::none());
|
||||||
|
if (participant_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("HttpServerPub Participant initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the type
|
||||||
|
type_.register_type(participant_);
|
||||||
|
|
||||||
|
// Create the publisher
|
||||||
|
PublisherQos pub_qos = PUBLISHER_QOS_DEFAULT;
|
||||||
|
participant_->get_default_publisher_qos(pub_qos);
|
||||||
|
publisher_ = participant_->create_publisher(pub_qos, nullptr, StatusMask::none());
|
||||||
|
if (publisher_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateRsp Publisher initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the topic
|
||||||
|
TopicQos topic_qos = TOPIC_QOS_DEFAULT;
|
||||||
|
participant_->get_default_topic_qos(topic_qos);
|
||||||
|
topic_ = participant_->create_topic("LicencePlateRspTopic", type_.get_type_name(), topic_qos);
|
||||||
|
if (topic_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateRsp Topic initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the data writer
|
||||||
|
DataWriterQos writer_qos = DATAWRITER_QOS_DEFAULT;
|
||||||
|
publisher_->get_default_datawriter_qos(writer_qos);
|
||||||
|
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
|
||||||
|
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
|
||||||
|
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
|
||||||
|
writer_ = publisher_->create_datawriter(topic_, writer_qos, this, StatusMask::all());
|
||||||
|
if (writer_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateRsp DataWriter initialization failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PublisherApp::~PublisherApp()
|
||||||
|
{
|
||||||
|
if (nullptr != participant_)
|
||||||
|
{
|
||||||
|
// Delete DDS entities contained within the DomainParticipant
|
||||||
|
participant_->delete_contained_entities();
|
||||||
|
|
||||||
|
// Delete DomainParticipant
|
||||||
|
factory_->delete_participant(participant_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PublisherApp::on_publication_matched(
|
||||||
|
DataWriter* writer,
|
||||||
|
const PublicationMatchedStatus& info)
|
||||||
|
{
|
||||||
|
if (info.current_count_change == 1)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
matched_ = info.current_count;
|
||||||
|
}
|
||||||
|
std::cout << writer->get_topic()->get_name() << " Publisher matched." << std::endl;
|
||||||
|
cv_.notify_one();
|
||||||
|
}
|
||||||
|
else if (info.current_count_change == -1)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
matched_ = info.current_count;
|
||||||
|
}
|
||||||
|
std::cout << writer->get_topic()->get_name() << " Publisher unmatched." << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << info.current_count_change
|
||||||
|
<< " is not a valid value for PublicationMatchedStatus current count change" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PublisherApp::run(std::shared_ptr<HTTPServer> handler)
|
||||||
|
{
|
||||||
|
while (!is_stopped())
|
||||||
|
{
|
||||||
|
handler->run();
|
||||||
|
|
||||||
|
if (handler->PostMsg.empty() != true)
|
||||||
|
{
|
||||||
|
json j = json::parse(handler->PostMsg);
|
||||||
|
std::cout << "车牌: " << j["AlarmInfoPlate"]["result"]["PlateResult"]["license"] << std::endl;
|
||||||
|
|
||||||
|
LicencePlateRsp rsp;
|
||||||
|
rsp.licence() = j["AlarmInfoPlate"]["result"]["PlateResult"]["license"];
|
||||||
|
writer_->write(&rsp);
|
||||||
|
|
||||||
|
handler->PostMsg.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for period or stop event
|
||||||
|
std::unique_lock<std::mutex> period_lock(mutex_);
|
||||||
|
cv_.wait_for(period_lock, std::chrono::milliseconds(period_ms_), [this]()
|
||||||
|
{
|
||||||
|
return is_stopped();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PublisherApp::publish()
|
||||||
|
{
|
||||||
|
bool ret = false;
|
||||||
|
// Wait for the data endpoints discovery
|
||||||
|
std::unique_lock<std::mutex> matched_lock(mutex_);
|
||||||
|
cv_.wait(matched_lock, [&]()
|
||||||
|
{
|
||||||
|
// at least one has been discovered
|
||||||
|
return ((matched_ > 0) || is_stopped());
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!is_stopped())
|
||||||
|
{
|
||||||
|
/* Initialize your structure here */
|
||||||
|
WeighRsp sample_;
|
||||||
|
ret = (RETCODE_OK == writer_->write(&sample_));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PublisherApp::is_stopped()
|
||||||
|
{
|
||||||
|
return stop_.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PublisherApp::stop()
|
||||||
|
{
|
||||||
|
stop_.store(true);
|
||||||
|
cv_.notify_one();
|
||||||
|
}
|
||||||
@ -0,0 +1,76 @@
|
|||||||
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @file PublisherApp.hpp
|
||||||
|
* This header file contains the declaration of the publisher functions.
|
||||||
|
*
|
||||||
|
* This file was generated by the tool fastddsgen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FAST_DDS_GENERATED__PUBLISHERAPP_HPP
|
||||||
|
#define FAST_DDS_GENERATED__PUBLISHERAPP_HPP
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
#include <fastdds/dds/domain/DomainParticipant.hpp>
|
||||||
|
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
|
||||||
|
#include <fastdds/dds/publisher/DataWriterListener.hpp>
|
||||||
|
#include <fastdds/dds/topic/TypeSupport.hpp>
|
||||||
|
|
||||||
|
#include "httpserver.hpp"
|
||||||
|
|
||||||
|
class PublisherApp : public eprosima::fastdds::dds::DataWriterListener
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
PublisherApp(
|
||||||
|
const int& domain_id);
|
||||||
|
|
||||||
|
~PublisherApp();
|
||||||
|
|
||||||
|
//! Publisher matched method
|
||||||
|
void on_publication_matched(
|
||||||
|
eprosima::fastdds::dds::DataWriter* writer,
|
||||||
|
const eprosima::fastdds::dds::PublicationMatchedStatus& info) override;
|
||||||
|
|
||||||
|
//! Run publisher
|
||||||
|
void run(std::shared_ptr<HTTPServer> handler);
|
||||||
|
|
||||||
|
//! Trigger the end of execution
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
//! Return the current state of execution
|
||||||
|
bool is_stopped();
|
||||||
|
|
||||||
|
//! Publish a sample
|
||||||
|
bool publish();
|
||||||
|
|
||||||
|
std::shared_ptr<eprosima::fastdds::dds::DomainParticipantFactory> factory_;
|
||||||
|
eprosima::fastdds::dds::DomainParticipant* participant_;
|
||||||
|
eprosima::fastdds::dds::Publisher* publisher_;
|
||||||
|
eprosima::fastdds::dds::Topic* topic_;
|
||||||
|
eprosima::fastdds::dds::DataWriter* writer_;
|
||||||
|
eprosima::fastdds::dds::TypeSupport type_;
|
||||||
|
std::condition_variable cv_;
|
||||||
|
int32_t matched_;
|
||||||
|
std::mutex mutex_;
|
||||||
|
const uint32_t period_ms_ = 100; // in ms
|
||||||
|
uint16_t samples_sent_;
|
||||||
|
std::atomic<bool> stop_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // FAST_DDS_GENERATED__PUBLISHERAPP_HPP
|
||||||
@ -0,0 +1,159 @@
|
|||||||
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @file Subscriber.cxx
|
||||||
|
* This file contains the implementation of the subscriber functions.
|
||||||
|
*
|
||||||
|
* This file was generated by the tool fastddsgen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Subscriber.hpp"
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#include <fastdds/dds/core/status/SubscriptionMatchedStatus.hpp>
|
||||||
|
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/DataReader.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/qos/SubscriberQos.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/SampleInfo.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/Subscriber.hpp>
|
||||||
|
|
||||||
|
#include "SystemPubSubTypes.hpp"
|
||||||
|
|
||||||
|
using namespace eprosima::fastdds::dds;
|
||||||
|
|
||||||
|
SubscriberApp::SubscriberApp(
|
||||||
|
const int& domain_id)
|
||||||
|
: factory_(nullptr)
|
||||||
|
, participant_(nullptr)
|
||||||
|
, subscriber_(nullptr)
|
||||||
|
, topic_(nullptr)
|
||||||
|
, reader_(nullptr)
|
||||||
|
, type_(new LicencePlateReqPubSubType())
|
||||||
|
, samples_received_(0)
|
||||||
|
, stop_(false)
|
||||||
|
{
|
||||||
|
// Create the participant
|
||||||
|
DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
|
||||||
|
pqos.name("HttpServer_sub_participant");
|
||||||
|
pqos.wire_protocol().builtin.discovery_config.leaseDuration = Duration_t(60, 0);
|
||||||
|
pqos.wire_protocol().builtin.discovery_config.leaseDuration_announcementperiod = Duration_t(30, 0);
|
||||||
|
factory_ = DomainParticipantFactory::get_shared_instance();
|
||||||
|
participant_ = factory_->create_participant(domain_id, pqos, nullptr, StatusMask::none());
|
||||||
|
if (participant_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("HttpServerSub Participant initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register the type
|
||||||
|
type_.register_type(participant_);
|
||||||
|
|
||||||
|
// Create the subscriber
|
||||||
|
SubscriberQos sub_qos = SUBSCRIBER_QOS_DEFAULT;
|
||||||
|
participant_->get_default_subscriber_qos(sub_qos);
|
||||||
|
subscriber_ = participant_->create_subscriber(sub_qos, nullptr, StatusMask::none());
|
||||||
|
if (subscriber_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateReq Subscriber initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the topic
|
||||||
|
TopicQos topic_qos = TOPIC_QOS_DEFAULT;
|
||||||
|
participant_->get_default_topic_qos(topic_qos);
|
||||||
|
topic_ = participant_->create_topic("LicencePlateReqTopic", type_.get_type_name(), topic_qos);
|
||||||
|
if (topic_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateReq Topic initialization failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the reader
|
||||||
|
DataReaderQos reader_qos = DATAREADER_QOS_DEFAULT;
|
||||||
|
subscriber_->get_default_datareader_qos(reader_qos);
|
||||||
|
reader_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
|
||||||
|
reader_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
|
||||||
|
reader_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
|
||||||
|
reader_ = subscriber_->create_datareader(topic_, reader_qos, this, StatusMask::all());
|
||||||
|
if (reader_ == nullptr)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("LicencePlateReq DataReader initialization failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SubscriberApp::~SubscriberApp()
|
||||||
|
{
|
||||||
|
if (nullptr != participant_)
|
||||||
|
{
|
||||||
|
// Delete DDS entities contained within the DomainParticipant
|
||||||
|
participant_->delete_contained_entities();
|
||||||
|
|
||||||
|
// Delete DomainParticipant
|
||||||
|
factory_->delete_participant(participant_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubscriberApp::on_subscription_matched(
|
||||||
|
DataReader* reader,
|
||||||
|
const SubscriptionMatchedStatus& info)
|
||||||
|
{
|
||||||
|
if (info.current_count_change == 1)
|
||||||
|
{
|
||||||
|
std::cout << reader->get_topicdescription()->get_name() << " Subscriber matched." << std::endl;
|
||||||
|
}
|
||||||
|
else if (info.current_count_change == -1)
|
||||||
|
{
|
||||||
|
std::cout << reader->get_topicdescription()->get_name() << " Subscriber unmatched." << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << info.current_count_change
|
||||||
|
<< " is not a valid value for SubscriptionMatchedStatus current count change" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubscriberApp::on_data_available(
|
||||||
|
DataReader* reader)
|
||||||
|
{
|
||||||
|
LicencePlateReq sample_;
|
||||||
|
SampleInfo info;
|
||||||
|
while ((!is_stopped()) && (RETCODE_OK == reader->take_next_sample(&sample_, &info)))
|
||||||
|
{
|
||||||
|
if ((info.instance_state == ALIVE_INSTANCE_STATE) && info.valid_data)
|
||||||
|
{
|
||||||
|
std::cout << "Sample '" << std::to_string(++samples_received_) << "' RECEIVED" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubscriberApp::run()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lck(terminate_cv_mtx_);
|
||||||
|
terminate_cv_.wait(lck, [this]
|
||||||
|
{
|
||||||
|
return is_stopped();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SubscriberApp::is_stopped()
|
||||||
|
{
|
||||||
|
return stop_.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SubscriberApp::stop()
|
||||||
|
{
|
||||||
|
stop_.store(true);
|
||||||
|
terminate_cv_.notify_all();
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @file SubscriberApp.hpp
|
||||||
|
* This header file contains the declaration of the subscriber functions.
|
||||||
|
*
|
||||||
|
* This file was generated by the tool fastddsgen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FAST_DDS_GENERATED__SUBSCRIBERAPP_HPP
|
||||||
|
#define FAST_DDS_GENERATED__SUBSCRIBERAPP_HPP
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
#include <fastdds/dds/domain/DomainParticipant.hpp>
|
||||||
|
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
|
||||||
|
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
|
||||||
|
#include <fastdds/dds/topic/TypeSupport.hpp>
|
||||||
|
|
||||||
|
#include "System.hpp"
|
||||||
|
|
||||||
|
class SubscriberApp : public eprosima::fastdds::dds::DataReaderListener
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
SubscriberApp(
|
||||||
|
const int& domain_id);
|
||||||
|
|
||||||
|
virtual ~SubscriberApp();
|
||||||
|
|
||||||
|
//! Subscription callback
|
||||||
|
void on_data_available(
|
||||||
|
eprosima::fastdds::dds::DataReader* reader) override;
|
||||||
|
|
||||||
|
//! Subscriber matched method
|
||||||
|
void on_subscription_matched(
|
||||||
|
eprosima::fastdds::dds::DataReader* reader,
|
||||||
|
const eprosima::fastdds::dds::SubscriptionMatchedStatus& info) override;
|
||||||
|
|
||||||
|
//! Run subscriber
|
||||||
|
void run();
|
||||||
|
|
||||||
|
//! Trigger the end of execution
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
//! Return the current state of execution
|
||||||
|
bool is_stopped();
|
||||||
|
|
||||||
|
std::shared_ptr<eprosima::fastdds::dds::DomainParticipantFactory> factory_;
|
||||||
|
eprosima::fastdds::dds::DomainParticipant* participant_;
|
||||||
|
eprosima::fastdds::dds::Subscriber* subscriber_;
|
||||||
|
eprosima::fastdds::dds::Topic* topic_;
|
||||||
|
eprosima::fastdds::dds::DataReader* reader_;
|
||||||
|
eprosima::fastdds::dds::TypeSupport type_;
|
||||||
|
uint16_t samples_received_;
|
||||||
|
std::atomic<bool> stop_;
|
||||||
|
mutable std::mutex terminate_cv_mtx_;
|
||||||
|
std::condition_variable terminate_cv_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // FAST_DDS_GENERATED__SUBSCRIBERAPP_HPP
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
#ifndef HTTP_SERVER_H
|
||||||
|
#define HTTP_SERVER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
class HTTPServer {
|
||||||
|
public:
|
||||||
|
// 构造函数和析构函数
|
||||||
|
HTTPServer(int port = 8080);
|
||||||
|
~HTTPServer();
|
||||||
|
|
||||||
|
//消息
|
||||||
|
std::string PostMsg;
|
||||||
|
|
||||||
|
// 服务器控制方法
|
||||||
|
bool start();
|
||||||
|
void run();
|
||||||
|
void stop();
|
||||||
|
void SetPort(int port);
|
||||||
|
|
||||||
|
// 回调函数类型定义
|
||||||
|
using RequestHandler = std::function<void(int, const std::string&, const std::map<std::string, std::string>&, const std::string&)>;
|
||||||
|
|
||||||
|
// 设置自定义请求处理器
|
||||||
|
void setRequestHandler(const RequestHandler& handler);
|
||||||
|
|
||||||
|
// 静态工具方法
|
||||||
|
static std::string urlDecode(const std::string& encoded);
|
||||||
|
static std::map<std::string, std::string> parseQueryString(const std::string& query);
|
||||||
|
static std::map<std::string, std::string> parseHeaders(const std::string& headers);
|
||||||
|
static std::string getContentType(const std::map<std::string, std::string>& headers);
|
||||||
|
static int getContentLength(const std::map<std::string, std::string>& headers);
|
||||||
|
|
||||||
|
// 响应方法
|
||||||
|
static void sendResponse(int client_socket, const std::string& response,
|
||||||
|
const std::string& content_type = "text/html; charset=utf-8");
|
||||||
|
static void sendError(int client_socket, int code, const std::string& message);
|
||||||
|
static void sendJSONResponse(int client_socket, const std::string& json);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 私有方法
|
||||||
|
std::string readFullRequest(int client_socket, int timeout_ms = 5000);
|
||||||
|
bool isRequestComplete(const std::string& request);
|
||||||
|
void handleConnection(int client_socket, const std::string& client_ip);
|
||||||
|
void handleRequest(int client_socket, const std::string& request);
|
||||||
|
void handlePOST(int client_socket, const std::string& request,
|
||||||
|
const std::map<std::string, std::string>& headers);
|
||||||
|
void handleGET(int client_socket, const std::string& request_line);
|
||||||
|
void logRequest(const std::string& method, const std::string& client_ip,
|
||||||
|
const std::map<std::string, std::string>& headers);
|
||||||
|
|
||||||
|
// 私有成员变量
|
||||||
|
int server_fd;
|
||||||
|
int port;
|
||||||
|
RequestHandler custom_handler;
|
||||||
|
|
||||||
|
// 禁用复制和赋值
|
||||||
|
HTTPServer(const HTTPServer&) = delete;
|
||||||
|
HTTPServer& operator=(const HTTPServer&) = delete;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HTTP_SERVER_H
|
||||||
@ -0,0 +1,144 @@
|
|||||||
|
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* @file main.cxx
|
||||||
|
* This file acts as a main entry point to the application.
|
||||||
|
*
|
||||||
|
* This file was generated by the tool fastddsgen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <csignal>
|
||||||
|
#include <cstring>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <fastdds/dds/log/Log.hpp>
|
||||||
|
|
||||||
|
#include "Subscriber.hpp"
|
||||||
|
#include "Publisher.hpp"
|
||||||
|
|
||||||
|
#include "httpserver.hpp"
|
||||||
|
|
||||||
|
#define VERSION "v1.0"
|
||||||
|
|
||||||
|
using eprosima::fastdds::dds::Log;
|
||||||
|
|
||||||
|
std::function<void(int)> stop_handler;
|
||||||
|
void signal_handler(
|
||||||
|
int signum)
|
||||||
|
{
|
||||||
|
stop_handler(signum);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string parse_signal(
|
||||||
|
const int& signum)
|
||||||
|
{
|
||||||
|
switch (signum)
|
||||||
|
{
|
||||||
|
case SIGINT:
|
||||||
|
return "SIGINT";
|
||||||
|
case SIGTERM:
|
||||||
|
return "SIGTERM";
|
||||||
|
#ifndef _WIN32
|
||||||
|
case SIGQUIT:
|
||||||
|
return "SIGQUIT";
|
||||||
|
case SIGHUP:
|
||||||
|
return "SIGHUP";
|
||||||
|
#endif // _WIN32
|
||||||
|
default:
|
||||||
|
return "UNKNOWN SIGNAL";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
auto ret = EXIT_SUCCESS;
|
||||||
|
std::shared_ptr<SubscriberApp> sub;
|
||||||
|
std::shared_ptr<PublisherApp> pub;
|
||||||
|
std::shared_ptr<HTTPServer> dev;
|
||||||
|
|
||||||
|
int domain_id = 0;
|
||||||
|
int port = 8080;
|
||||||
|
|
||||||
|
for (int i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[i], "--domain") == 0 && i + 1 < argc)
|
||||||
|
{
|
||||||
|
domain_id = atoi(argv[++i]);
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc)
|
||||||
|
{
|
||||||
|
port = atoi(argv[++i]);
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "--version") == 0)
|
||||||
|
{
|
||||||
|
std::cout << "Vesrion: " << VERSION << "\n";
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "--help") == 0)
|
||||||
|
{
|
||||||
|
std::cout << "Usage: [options]\n"
|
||||||
|
<< "Options:\n"
|
||||||
|
<< " --domain Set domain ID\n"
|
||||||
|
<< " --port Set port name (e.g., 8080)\n"
|
||||||
|
<< " --version Show software Version\n"
|
||||||
|
<< " --help Show this help message\n";
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cerr << "Unknown option: " << argv[i] << "\n";
|
||||||
|
std::cout << "Usage: [options]\n"
|
||||||
|
<< "Options:\n"
|
||||||
|
<< " --domain Set domain ID\n"
|
||||||
|
<< " --port Set port name (e.g., 8080)\n"
|
||||||
|
<< " --version Show software Version\n"
|
||||||
|
<< " --help Show this help message\n";
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub = std::make_shared<SubscriberApp>(domain_id);
|
||||||
|
pub = std::make_shared<PublisherApp>(domain_id);
|
||||||
|
dev = std::make_shared<HTTPServer>();
|
||||||
|
|
||||||
|
dev->SetPort(port);
|
||||||
|
dev->start();
|
||||||
|
|
||||||
|
std::thread pub_thread(&PublisherApp::run, pub, dev);
|
||||||
|
|
||||||
|
std::cout << "Program is running. Please press Ctrl+C to stop at any time." << std::endl;
|
||||||
|
|
||||||
|
stop_handler = [&](int signum)
|
||||||
|
{
|
||||||
|
std::cout << "\n" << parse_signal(signum) << " received, stopping " << argv[1]
|
||||||
|
<< " execution." << std::endl;
|
||||||
|
pub->stop();
|
||||||
|
};
|
||||||
|
|
||||||
|
signal(SIGINT, signal_handler);
|
||||||
|
signal(SIGTERM, signal_handler);
|
||||||
|
#ifndef _WIN32
|
||||||
|
signal(SIGQUIT, signal_handler);
|
||||||
|
signal(SIGHUP, signal_handler);
|
||||||
|
#endif // _WIN32
|
||||||
|
|
||||||
|
pub_thread.join();
|
||||||
|
|
||||||
|
Log::Reset();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef _MSG_HPP_
|
||||||
|
#define _MSG_HPP_
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
#include <mutex>
|
||||||
|
#include "System.hpp"
|
||||||
|
|
||||||
|
class MsgData {
|
||||||
|
public:
|
||||||
|
static std::queue<PrintReq> PrintReq_queue_;
|
||||||
|
static std::mutex queue_cv_mtx_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue