Permalink
Browse files

From Stephan Huber, RestHttpDevice plugin for support of remote appli…

…cation control via Rest http.
  • Loading branch information...
1 parent b0b3158 commit 6445cab2e2a3375dfefb100e9cd72f754290ce5c @osg-training-guest osg-training-guest committed Oct 30, 2012
View
@@ -516,6 +516,7 @@ ELSE()
FIND_PACKAGE(GtkGl)
FIND_PACKAGE(DirectInput)
FIND_PACKAGE(NVTT)
+ FIND_PACKAGE(Asio)
ENDIF()
IF(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 8)
@@ -0,0 +1,20 @@
+# Locate ASIO-headers (http://think-async.com/Asio)
+# This module defines
+# ASIO_FOUND, if false, do not try to link to gdal
+# ASIO_INCLUDE_DIR, where to find the headers
+#
+# Created by Stephan Maximilian Huber
+
+FIND_PATH(ASIO_INCLUDE_DIR
+ NAMES
+ asio.hpp
+ PATHS
+ /usr/include
+ /usr/local/include
+)
+
+SET(ASIO_FOUND "NO")
+FIND_PACKAGE( Boost 1.37 )
+IF(Boost_FOUND AND ASIO_INCLUDE_DIR)
+ SET(ASIO_FOUND "YES")
+ENDIF()
@@ -28,6 +28,8 @@
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
+#include <osgGA/Device>
+
#include <iostream>
#include <osg/GLExtensions>
@@ -147,6 +149,7 @@ int main(int argc, char** argv)
arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access.");
+ arguments.getApplicationUsage()->addCommandLineOption("--device <device-name>","add named device to the viewer");
osgViewer::Viewer viewer(arguments);
@@ -182,6 +185,16 @@ int main(int argc, char** argv)
);
}
}
+
+ std::string device;
+ while(arguments.read("--device", device))
+ {
+ osg::ref_ptr<osgGA::Device> dev = osgDB::readFile<osgGA::Device>(device);
+ if (dev.valid())
+ {
+ viewer.addDevice(dev.get());
+ }
+ }
// set up the camera manipulators.
{
@@ -401,6 +401,36 @@ int main( int argc, char **argv )
viewer.readConfiguration(configurationFile);
doSetViewer = false;
}
+
+ std::string device;
+ while (arguments.read("--device", device))
+ {
+ osg::ref_ptr<osgGA::Device> dev = osgDB::readFile<osgGA::Device>(device);
+ if (dev.valid())
+ {
+ viewer.addDevice(dev.get());
+ }
+ }
+
+ if (arguments.read("--http-control"))
+ {
+
+ std::string server_address = "localhost";
+ std::string server_port = "8080";
+ std::string document_root = "htdocs";
+
+ while (arguments.read("--http-server-address", server_address)) {}
+ while (arguments.read("--http-server-port", server_port)) {}
+ while (arguments.read("--http-document-root", document_root)) {}
+
+ osg::ref_ptr<osgDB::Options> device_options = new osgDB::Options("documentRegisteredHandlers");
+
+ osg::ref_ptr<osgGA::Device> rest_http_device = osgDB::readFile<osgGA::Device>(server_address+":"+server_port+"/"+document_root+".resthttp", device_options);
+ if (rest_http_device.valid())
+ {
+ viewer.addDevice(rest_http_device.get());
+ }
+ }
// set up stereo masks
viewer.getCamera()->setCullMask(0xffffffff);
@@ -264,6 +264,9 @@ IF (SDL_FOUND)
ADD_SUBDIRECTORY(sdl)
ENDIF(SDL_FOUND)
+IF(ASIO_FOUND)
+ ADD_SUBDIRECTORY(RestHttpDevice)
+ENDIF(ASIO_FOUND)
##########to get all the variables of Cmake
#GET_CMAKE_PROPERTY(MYVARS VARIABLES)
@@ -0,0 +1,32 @@
+INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
+INCLUDE_DIRECTORIES(${ASIO_INCLUDE_DIR})
+
+SET(TARGET_SRC
+ connection.cpp
+ io_service_pool.cpp
+ mime_types.cpp
+ reply.cpp
+ request_handler.cpp
+ request_parser.cpp
+ server.cpp
+ RestHttpDevice.cpp
+ ReaderWriterRestHttpDevice.cpp
+)
+
+SET(TARGET_H
+ connection.hpp
+ header.hpp
+ io_service_pool.hpp
+ mime_types.hpp
+ reply.hpp
+ request_handler.hpp
+ request_parser.hpp
+ request.hpp
+ server.hpp
+ RestHttpDevice.hpp
+)
+
+SET(TARGET_ADDED_LIBRARIES osgPresentation )
+
+#### end var setup ###
+SETUP_PLUGIN(resthttp)
@@ -0,0 +1,126 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
+ *
+ * This library is open source and may be redistributed and/or modified under
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version. The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * This library 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
+ * OpenSceneGraph Public License for more details.
+*/
+
+
+/* README:
+ *
+ * This code is loosely based on the QTKit implementation of Eric Wing, I removed
+ * some parts and added other parts.
+ *
+ * What's new:
+ * - it can handle URLs currently http and rtsp
+ * - it supports OS X's CoreVideo-technology, this will render the movie-frames
+ * into a bunch of textures. If you load your movie via readImageFile you'll
+ * get the standard behaviour, an ImageStream, where the data gets updated on
+ * every new video-frame. This may be slow.
+ * To get CoreVideo, you'll need to use readObjectFile and cast the result (if any)
+ * to an osg::Texture and use that as your video-texture. If you need access to the
+ * imagestream, just cast getImage to an image-stream. Please note, the data-
+ * property of the image-stream does NOT store the current frame, instead it's empty.
+ *
+ */
+
+
+#include <osgDB/Registry>
+#include <osgDB/FileNameUtils>
+#include <osgDB/FileUtils>
+#include "RestHttpDevice.hpp"
+
+
+
+
+
+class ReaderWriterRestHttp : public osgDB::ReaderWriter
+{
+ public:
+
+ ReaderWriterRestHttp()
+ {
+ supportsExtension("resthttp", "Virtual Device Integration via a HTTP-Server and a REST-interface");
+
+ supportsOption("documentRoot", "document root of asset files to server via the http-server");
+ supportsOption("serverAddress", "server address to listen for incoming requests");
+ supportsOption("serverPort", "server port to listen for incoming requests");
+ supportsOption("documentRegisteredHandlers", "dump a documentation of all registered REST-handler to the console");
+
+ }
+
+ virtual const char* className() const { return "Rest/HTTP Virtual Device Integration plugin"; }
+
+ virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
+ {
+ if (osgDB::getFileExtension(file) == "resthttp")
+ {
+ std::string document_root = options ? options->getPluginStringData("documentRoot") : "htdocs/";
+ std::string server_address = options ? options->getPluginStringData("serverAddress") : "localhost";
+ std::string server_port = options ? options->getPluginStringData("serverPort") : "8080";
+
+ // supported file-name scheme to get address, port and document-root: <server-address>:<server-port>/<document-root>
+ // example: '192.168.1.1:88888/var/www/htdocs'
+
+ std::string file_wo_ext = osgDB::getNameLessAllExtensions(file);
+ if ((file_wo_ext.find('/') != std::string::npos) && (file_wo_ext.find(':') != std::string::npos)) {
+ std::string server_part = file_wo_ext.substr(0, file_wo_ext.find('/'));
+ document_root = file_wo_ext.substr(file_wo_ext.find('/'));
+ server_address = server_part.substr(0,server_part.find(':'));
+ server_port = server_part.substr(server_part.find(':') + 1);
+ }
+
+ try
+ {
+ osg::ref_ptr<RestHttpDevice> device = new RestHttpDevice(server_address, server_port, document_root);
+
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slide/first", osgGA::GUIEventAdapter::KEY_Home));
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slide/last", osgGA::GUIEventAdapter::KEY_End));
+
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slide/next", osgGA::GUIEventAdapter::KEY_Right));
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slide/previous", osgGA::GUIEventAdapter::KEY_Left));
+
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/layer/next", osgGA::GUIEventAdapter::KEY_Down));
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/layer/previous", osgGA::GUIEventAdapter::KEY_Up));
+
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slideorlayer/next", osgGA::GUIEventAdapter::KEY_Page_Down));
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/slideorlayer/previous", osgGA::GUIEventAdapter::KEY_Page_Up));
+
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/unpause", 'o'));
+ device->addRequestHandler(new SendKeystrokeRequestHandler("/pause", 'p'));
+
+
+
+ if (options && (options->getPluginStringData("documentRegisteredHandlers") == "true"))
+ {
+ std::cout << *device << std::endl;
+ }
+ return device.release();
+ }
+ catch(std::exception& e)
+ {
+ OSG_WARN << "ReaderWriterRestHttpDevice : could not create http-server! Reason: " << e.what() << std::endl;
+ return ReadResult::ERROR_IN_READING_FILE;
+ }
+ catch(...)
+ {
+ OSG_WARN << "ReaderWriterRestHttpDevice : could not create http-server, unknown excpetion thrown " << std::endl;
+ return ReadResult::ERROR_IN_READING_FILE;
+ }
+
+ }
+
+ return ReadResult::FILE_NOT_HANDLED;
+ }
+
+};
+
+// now register with Registry to instantiate the above
+// reader/writer.
+REGISTER_OSGPLUGIN(resthttp, ReaderWriterRestHttp)
Oops, something went wrong.

0 comments on commit 6445cab

Please sign in to comment.