Added skeleton for sitemap bundle.

This commit is contained in:
2015-10-14 00:47:05 +02:00
parent 0399674af7
commit 7f3395ce7e
9 changed files with 707 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
cmake_minimum_required(VERSION 2.8.3)
project(hos_cosgi_sitemap)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
hos_celix
hos_cosgi_webserver
hos_cosgi_devicerepository
hos_cosgi_core
hos_cpp_utils
hos_deps
roscpp
)
## System dependencies are found with CMake's conventions
find_package(APR REQUIRED)
find_package(CELIX REQUIRED)
find_package(Boost REQUIRED)
find_package(libvariant REQUIRED)
find_package(Poco REQUIRED COMPONENTS Net NetSSL)
###################################
## catkin specific configuration ##
###################################
catkin_package(
INCLUDE_DIRS include/${PROJECT_NAME}/
CATKIN_DEPENDS hos_celix hos_cosgi_core roscpp hos_cpp_utils
)
###########
## Build ##
###########
add_definitions(
-std=c++11
)
include_directories(
include/${PROJECT_NAME}/
${catkin_INCLUDE_DIRS}
${APR_INCLUDE_DIR}
${APR_INCLUDE_DIR}/apr-1.0
${libvariant_INCLUDE_DIR}
${Poco_INCLUDE_DIRS}
)
set(BUNDLE_SYMBOLICNAME "hos.hos_sitemap")
set(BUNDLE_VERSION "1.0.0")
set(BUNDLE_NAME "hos_sitemap")
bundle(hos_cosgi_sitemap
SOURCES
${PROJECT_SOURCE_DIR}/src/BundleActivator
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/sitemap/SitemapServiceImpl
${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/sitemap/webresource/SitemapHandler
)
target_link_libraries(hos_cosgi_sitemap
${catkin_LIBRARIES}
${APR_LIBRARIES}
${libvariant_LIBRARY}
${Poco_LIBRARIES}
)
#############
## Install ##
#############
## Mark cpp header files for installation
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
install(FILES
${CMAKE_BINARY_DIR}/hos_cosgi_sitemap.zip
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/bundles
)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
OPTIONAL
)
###################
## Documentation ##
###################
find_package(Doxygen)
if(DOXYGEN_FOUND)
add_custom_target(doc)
endif(DOXYGEN_FOUND)
@@ -0,0 +1,58 @@
#pragma once
#include <hos/cosgi/service/Interface.h>
#include <string>
#include <vector>
#include <chrono>
#include <memory>
namespace hos {
namespace cosgi {
namespace service {
// forward declarations
namespace core { class CoreService; }
namespace devicerepository { class DeviceRepositoryService; }
// service namespace
namespace sitemap {
/**
* Public COSGI interface of the sitemap service.
*/
class SitemapService
: public virtual interface::Service {
public:
/**
* The service name.
*/
static constexpr const char* SERVICE_NAME = "hos_sitemap_service";
/**
* Returns a reference to the core service.
*
* \return core service.
*/
virtual core::CoreService* getCoreService() noexcept = 0;
/**
* Returns a reference to the device repository service.
*
* \return device respository service.
*/
virtual devicerepository::DeviceRepositoryService* getDeviceRepository() noexcept = 0;
};
} // namespace sitemap
} // namespace service
} // namespace cosgi
} // namespace hos
+64
View File
@@ -0,0 +1,64 @@
<?xml version="1.0"?>
<package>
<name>hos_cosgi_sitemap</name>
<version>0.0.0</version>
<description>The hos_cosgi_sitemap package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
<!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> -->
<maintainer email="matzman@todo.todo">matzman</maintainer>
<!-- One license tag required, multiple allowed, one license per tag -->
<!-- Commonly used license strings: -->
<!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 -->
<license>TODO</license>
<!-- Url tags are optional, but mutiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/hos_cosgi_sitemap</url> -->
<!-- Author tags are optional, mutiple are allowed, one per tag -->
<!-- Authors do not have to be maintianers, but could be -->
<!-- Example: -->
<!-- <author email="jane.doe@example.com">Jane Doe</author> -->
<!-- The *_depend tags are used to specify dependencies -->
<!-- Dependencies can be catkin packages or system dependencies -->
<!-- Examples: -->
<!-- Use build_depend for packages you need at compile time: -->
<!-- <build_depend>message_generation</build_depend> -->
<!-- Use buildtool_depend for build tool packages: -->
<!-- <buildtool_depend>catkin</buildtool_depend> -->
<!-- Use run_depend for packages you need at runtime: -->
<!-- <run_depend>message_runtime</run_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>hos_celix</build_depend>
<build_depend>hos_cosgi_core</build_depend>
<build_depend>hos_cpp_utils</build_depend>
<build_depend>hos_deps</build_depend>
<build_depend>roscpp</build_depend>
<build_depend>hos_cosgi_webserver</build_depend>
<build_depend>hos_cosgi_devicerepository</build_depend>
<run_depend>hos_celix</run_depend>
<run_depend>hos_cosgi_core</run_depend>
<run_depend>hos_cpp_utils</run_depend>
<run_depend>hos_deps</run_depend>
<run_depend>roscpp</run_depend>
<run_depend>hos_cosgi_webserver</run_depend>
<run_depend>hos_cosgi_devicerepository</run_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
@@ -0,0 +1,215 @@
// std
#include <cstdlib>
#include <cstdio>
#include <string>
#include <memory>
#include <mutex>
// ROS
#include <ros/ros.h>
// APR
#include <apr_general.h>
// Celix
#include <hos/cosgi/celix.h>
// WebServer
#include <hos/cosgi/service/webserver/WebServerService.h>
// Boost
#include <boost/filesystem.hpp>
// CoreService
#include "hos/cosgi/service/sitemap/SitemapServiceImpl.h"
#include "hos/cosgi/service/sitemap/webresource/SitemapHandler.h"
#include <hos/cosgi/service/core/CoreService.h>
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
using namespace hos::cosgi;
using namespace hos::cosgi::service::webserver;
using namespace hos::cosgi::service::sitemap;
using namespace hos::cosgi::service::devicerepository;
using namespace hos::cosgi::service::core;
namespace siteweb = hos::cosgi::service::sitemap::webresource;
struct UserData {
apr_pool_t* memoryPool = nullptr;
std::unique_ptr<SitemapServiceImpl> serviceImpl;
std::unique_ptr<siteweb::SitemapHandlerFactory> webResourceImpl;
service_registration_pt serviceReg = nullptr;
service_registration_pt webResourceReg = nullptr;
serviceListener myServiceListener;
bundle_context_pt bundleContext;
};
celix_status_t cb_serviceChanged(void* listener, service_event_pt event) {
UserData* myUserData = (UserData*) ((serviceListener*)listener)->handle;
if (event->type == OSGI_FRAMEWORK_SERVICE_EVENT_REGISTERED) {
char* serviceName = nullptr;
bool checkReg = false;
serviceReference_getProperty(event->reference, (char*)OSGI_FRAMEWORK_OBJECTCLASS, &serviceName);
if (serviceName && std::strcmp(serviceName, DeviceRepositoryService::SERVICE_NAME) == 0) {
DeviceRepositoryService *service = nullptr;
bundleContext_getService(myUserData->bundleContext, event->reference, (void**)&service);
myUserData->serviceImpl->setRepoService(service);
checkReg = true;
}
// Register rule engine service and webresource
if (checkReg && !myUserData->serviceReg && myUserData->serviceImpl->isInitialized()) {
SitemapService* serviceInterface = dynamic_cast<SitemapService*>(myUserData->serviceImpl.get());
bundleContext_registerService(myUserData->bundleContext, (char*)SitemapServiceImpl::SERVICE_NAME, serviceInterface, nullptr, &myUserData->serviceReg);
// web resource
service::webserver::WebResourceHandlerFactory* webInterface = dynamic_cast<service::webserver::WebResourceHandlerFactory*>(myUserData->webResourceImpl.get());
// ToDo: Properly delete properties
properties_pt webResourceProps = properties_create();
properties_set(webResourceProps, (char*)"path", (char*)"/sitemap");
bundleContext_registerService(myUserData->bundleContext, (char*)service::webserver::WebResourceHandlerFactory::SERVICE_NAME, webInterface, webResourceProps, &myUserData->webResourceReg);
}
} else if (event->type == OSGI_FRAMEWORK_SERVICE_EVENT_UNREGISTERING) {
}
return CELIX_SUCCESS;
}
celix_status_t bundleActivator_create(bundle_context_pt context, void **userData) {
celix_status_t status = CELIX_START_ERROR;
apr_pool_t* memoryPool = nullptr;
// Create APR memory pool
apr_status_t astatus = apr_pool_create(&memoryPool, NULL);
if (astatus != APR_SUCCESS) {
ROS_ERROR("SitemapBundle: Could not create APR memory pool (%i).", astatus);
} else {
// Create user data
UserData* myUserData = (UserData*)apr_palloc(memoryPool, sizeof(UserData));
if (myUserData != nullptr) {
myUserData->memoryPool = memoryPool;
myUserData->bundleContext = context;
*userData = (void*)myUserData;
status = CELIX_SUCCESS;
ROS_INFO("SitemapBundle: Bundle successfully created");
} else {
ROS_ERROR("SitemapBundle: Could not create user data struct");
apr_pool_destroy(memoryPool);
}
}
return status;
}
celix_status_t bundleActivator_start(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_SUCCESS;
if (userData != nullptr) {
UserData* myUserData = (UserData*)userData;
// Create sitemap service
if (myUserData->serviceReg != nullptr) {
serviceRegistration_unregister(myUserData->serviceReg);
myUserData->serviceReg = nullptr;
}
myUserData->serviceImpl.reset(new SitemapServiceImpl());
// Create sitemap webresource
if (myUserData->webResourceReg != nullptr) {
serviceRegistration_unregister(myUserData->webResourceReg);
}
myUserData->webResourceImpl.reset(new siteweb::SitemapHandlerFactory(myUserData->serviceImpl.get()));
// Get core Service
service_reference_pt ref = nullptr;
bundleContext_getServiceReference(context, (char*)CoreService::SERVICE_NAME, &ref);
CoreService* coreService = nullptr;
bundleContext_getService(context, ref, (void**)&coreService);
if (coreService == nullptr) {
ROS_ERROR("SitemapBundle: Could not retrieve HOSCoreService. Are you using hos_cosgi_launcher?");
} else {
myUserData->serviceImpl->setCoreService(coreService);
}
// Get device repository service
service_reference_pt ref2 = nullptr;
bundleContext_getServiceReference(context, (char*)DeviceRepositoryService::SERVICE_NAME, &ref2);
DeviceRepositoryService* repoService = nullptr;
bundleContext_getService(context, ref2, (void**)&repoService);
if (repoService) {
myUserData->serviceImpl->setRepoService(repoService);
}
// Register sitemap service and webresource
if (myUserData->serviceImpl->isInitialized()) {
SitemapService* engineInterface = dynamic_cast<SitemapService*>(myUserData->serviceImpl.get());
bundleContext_registerService(context, (char*)SitemapService::SERVICE_NAME, engineInterface, nullptr, &myUserData->serviceReg);
// web resource
service::webserver::WebResourceHandlerFactory* webInterface = dynamic_cast<service::webserver::WebResourceHandlerFactory*>(myUserData->webResourceImpl.get());
// ToDo: Properly delete properties
properties_pt webResourceProps = properties_create();
properties_set(webResourceProps, (char*)"path", (char*)"/sitemap");
bundleContext_registerService(context, (char*)service::webserver::WebResourceHandlerFactory::SERVICE_NAME, webInterface, webResourceProps, &myUserData->webResourceReg);
}
// Register service listener
myUserData->myServiceListener.handle = myUserData;
myUserData->myServiceListener.serviceChanged = cb_serviceChanged;
bundleContext_addServiceListener(context, &myUserData->myServiceListener, nullptr);
ROS_INFO("SitemapBundle: Bundle successfully started");
} else {
ROS_ERROR("SitemapBundle: Could not start bundle, user data struct is null");
}
return status;
}
celix_status_t bundleActivator_stop(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_ILLEGAL_STATE;
if (userData != nullptr) {
UserData* myUserData = (UserData*)userData;
status = bundleContext_removeServiceListener(context, &myUserData->myServiceListener);
if (myUserData->webResourceReg != nullptr) {
serviceRegistration_unregister(myUserData->webResourceReg);
myUserData->webResourceReg = nullptr;
}
if (myUserData->webResourceImpl) {
myUserData->webResourceImpl.reset();
}
if (myUserData->serviceReg != nullptr) {
serviceRegistration_unregister(myUserData->serviceReg);
myUserData->serviceReg = nullptr;
}
if (myUserData->serviceImpl) {
myUserData->serviceImpl.reset();
}
ROS_INFO("SitemapBundle: Bundle successfully stopped");
status = CELIX_SUCCESS;
} else {
ROS_ERROR("SitemapBundle: Could not stop bundle, user data struct is null");
}
return status;
}
celix_status_t bundleActivator_destroy(void * userData, bundle_context_pt context) {
celix_status_t status = CELIX_ILLEGAL_STATE;
if (userData != nullptr) {
apr_pool_t* memoryPool = ((UserData*)userData)->memoryPool;
if (memoryPool != nullptr) {
apr_pool_destroy(memoryPool);
ROS_INFO("SitemapBundle: Bundle successfully destroyed");
} else {
ROS_ERROR("SitemapBundle: Could not destroy bundle, there is no APR memory pool");
}
status = CELIX_SUCCESS;
} else {
ROS_ERROR("SitemapBundle: Could not destroy bundle, user data struct is null");
}
return status;
}
@@ -0,0 +1,65 @@
#include "SitemapServiceImpl.h"
// std
#include <exception>
#include <iostream>
#include <unordered_set>
// ROS
#include <ros/ros.h>
// libvariant
#include <Variant/Variant.h>
// HOS
#include <hos/cosgi/service/core/CoreService.h>
#include <hos/utils/SchemaLoader.h>
// Service Namespace
namespace hos {
namespace cosgi {
namespace service {
namespace sitemap {
// Service information
HOS_SERVICE_IMPL_INFO( SitemapServiceImpl, // Implementation
service::sitemap::SitemapService::SERVICE_NAME, // Service Name
1, 0 ) // Version
void SitemapServiceImpl::setCoreService(service::core::CoreService* coreService) noexcept {
if (coreService && !this->coreService) {
this->coreService = coreService;
this->nodehandle = &coreService->getPrivateNodeHandle();
int major = 0, minor = 0;
coreService->getServiceVersion(major, minor);
ROS_INFO_STREAM("SitemapService: Found HOSCoreService Version " << major << "." << minor);
if (repoService) {
init();
}
}
}
void SitemapServiceImpl::setRepoService(devicerepository::DeviceRepositoryService* repoService) noexcept {
if (repoService && !this->repoService) {
this->repoService = repoService;
int major = 0, minor = 0;
repoService->getServiceVersion(major, minor);
ROS_INFO_STREAM("RuleEngineService: Found HOSDeviceRepositoryService Version " << major << "." << minor);
if (coreService) {
init();
}
}
}
void SitemapServiceImpl::init() noexcept {
initialized = true;
ROS_INFO_STREAM("RuleEngineService: Initialized and Ready.");
}
} /* namespace sitemap */
} /* namespace service */
} /* namespace cosgi */
} /* namespace hos */
@@ -0,0 +1,66 @@
#pragma once
#include <hos/cosgi/service/sitemap/SitemapService.h>
// hos includes
#include <hos/cosgi/service/core/CoreService.h>
#include <hos/cosgi/service/devicerepository/DeviceRepositoryService.h>
#include <hos/cosgi/service/Implementation.h>
//#include <hos/utils/SchemaLoader.h>
// ROS includes
#include <ros/ros.h>
// std includes
#include <mutex>
#include <map>
#include <set>
#include <memory>
#include <string>
namespace hos {
namespace cosgi {
namespace service {
namespace sitemap {
class SitemapServiceImpl
: public virtual SitemapService,
public implementation::Service<SitemapServiceImpl> {
private:
service::core::CoreService* coreService = nullptr;
devicerepository::DeviceRepositoryService* repoService = nullptr;
ros::NodeHandle* nodehandle = nullptr;
bool initialized = false;
void init() noexcept;
public:
/* Initialization */
void setCoreService(service::core::CoreService* coreService) noexcept;
void setRepoService(devicerepository::DeviceRepositoryService* repoService) noexcept;
/* Public Service Implementations */
virtual core::CoreService* getCoreService() noexcept override {
return coreService;
}
virtual devicerepository::DeviceRepositoryService* getDeviceRepository() noexcept override {
return repoService;
}
/* Internal */
bool isInitialized() noexcept { return initialized; }
};
} // namespace sitemap
} // namespace service
} // namespace cosgi
} // namespace hos
@@ -0,0 +1,66 @@
#include "SitemapHandler.h"
#include <hos/utils/Timestamp.h>
#include <boost/format.hpp>
#include <boost/algorithm/string.hpp>
#include <Poco/URI.h>
#include <Poco/Timestamp.h>
#include <boost/algorithm/string.hpp>
#include <chrono>
#include <Poco/Net/HTTPServerRequestImpl.h>
#include <Poco/Net/HTTPServerResponseImpl.h>
#include <Poco/Net/WebSocket.h>
#include "Poco/Net/NetException.h"
#include <Poco/Net/StreamSocket.h>
namespace hos {
namespace cosgi {
namespace service {
namespace sitemap {
namespace webresource {
using namespace Poco;
using namespace libvariant;
HOS_SERVICE_IMPL_INFO( SitemapHandlerFactory, // Implementation
service::webserver::WebResourceHandlerFactory::SERVICE_NAME, // Service Name
1, 0 ) // Version
SitemapHandlerFactory::SitemapHandlerFactory(SitemapService* service)
: service(service) {}
Net::HTTPRequestHandler* SitemapHandlerFactory::createRequestHandler( Poco::URI matchUri, const Net::HTTPServerRequest& request, bool ssl ) noexcept {
return new SitemapHandler( this, matchUri, service, ssl);
}
void SitemapHandler::_handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Net::HTMLForm& form) {
if(req.find("Upgrade") != req.end() && Poco::icompare(req["Upgrade"], "websocket") == 0) {
//__handleEvent(req, resp, true);
throw webserver::webresource::WebResourceHandlerException("ToDo",
Net::HTTPResponse::HTTP_NOT_IMPLEMENTED);
} else {
if (req.getMethod() == Net::HTTPRequest::HTTP_PUT || req.getMethod() == Net::HTTPRequest::HTTP_GET || req.getMethod() == Net::HTTPRequest::HTTP_POST) {
// Put Command
throw webserver::webresource::WebResourceHandlerException("Still a ToDo.",
Net::HTTPResponse::HTTP_NOT_IMPLEMENTED);
} else {
throw webserver::webresource::WebResourceHandlerException("Could not understood request",
Net::HTTPResponse::HTTP_BAD_REQUEST);
}
}
}
} // namespace webresource
} // namespace sitemap
} // namespace service
} // namespace cosgi
} // namespace hos
@@ -0,0 +1,74 @@
#pragma once
#include <hos/cosgi/service/Implementation.h>
#include <hos/cosgi/service/webserver/WebServerService.h>
#include <hos/cosgi/service/webserver/webresource/WebResourceHandlerImpl.h>
#include <hos/cosgi/service/sitemap/SitemapService.h>
#include <hos/utils/FunctionTable.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Variant/Variant.h>
#include <ros/ros.h>
#include <vector>
#include <string>
#include <queue>
#include <condition_variable>
#include <memory>
#include <thread>
#include <functional>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
// Service Namespace
namespace hos {
namespace cosgi {
namespace service {
namespace sitemap {
namespace webresource {
class SitemapHandlerFactory;
class SitemapHandler;
class SitemapHandlerFactory
: public webserver::webresource::WebResourceHandlerFactoryImpl<SitemapHandlerFactory, false> {
private:
SitemapService* service;
public:
SitemapHandlerFactory(SitemapService* service);
virtual Poco::Net::HTTPRequestHandler* createRequestHandler(Poco::URI matchUri, const Poco::Net::HTTPServerRequest& request, bool ssl ) noexcept override;
};
class SitemapHandler
: public webserver::webresource::WebResourceHandlerImpl {
protected:
SitemapHandlerFactory* parent;
Poco::URI matchUri;
SitemapService* service;
bool ssl;
public:
SitemapHandler( SitemapHandlerFactory* parent,
Poco::URI matchUri,
SitemapService* service,
bool ssl = false )
: parent(parent), matchUri(matchUri), service(service), ssl(ssl) {}
virtual void _handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp, Poco::Net::HTMLForm& form) override;
};
} // namespace webresource
} // namespace sitemap
} // namespace service
} // namespace cosgi
} // namespace hos
+1
View File
@@ -13,6 +13,7 @@
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_ruleengine/hos_cosgi_ruleengine.zip
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_devicerepository/hos_cosgi_devicerepository.zip
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_webserver/hos_cosgi_webserver.zip
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_sitemap/hos_cosgi_sitemap.zip
$(find hos_cosgi_core)/../../build_isolated/hos_cosgi_testwebapp/hos_cosgi_testwebapp.zip"/>
</node>
</launch>