Iray SDK API nvidia_logo_transpbg.gif Up
Example for the HTTP server
[Previous] [Next] [Up]

This example imports a scene file, renders the scene, and serves the image via an HTTP server

New Topics

  • Running an HTTP server

Detailed Description

Running an HTTP server


This example provides a very simple HTTP server which just serves one particular image. The image is rendered in the same way as in the Example for Rendering. See HTTP server for a general introduction to the HTTP server.

This examples uses a simple request handler which always serves a fixed image, independent of the requested URL, etc. A response handler is installed to set the content type to 'image/jpeg'. Note that this is just done for illustration purposes. The content type could also be set directly by the request handler.

Example Source

Source Code Location: examples/example_http_server.cpp

/******************************************************************************
* Copyright 2023 NVIDIA Corporation. All rights reserved.
*****************************************************************************/
// examples/example_http_server.cpp
//
// Imports a scene file, renders the scene, and serves the image via the HTTP server
//
// The example expects the following command line arguments:
//
// example_rendering <scene_file> <mdl_path> <port>
//
// scene_file some scene file, e.g., main.mi
// mdl_path path to the MDL modules, e.g., iray-<version>/mdl
// port port for the HTTP server
#include <iostream>
#include <mi/neuraylib.h>
// Include code shared by all examples.
#include "example_shared.h"
// Include an implementation of IRender_target.
#include "example_render_target_simple.h"
// Simple request handler which always sends the buffer passed in the constructor
class Request_handler : public mi::base::Interface_implement<mi::http::IRequest_handler>
{
public:
// Constructor. Stores the passed buffer.
Request_handler( mi::neuraylib::IBuffer* buffer)
: m_buffer( buffer, mi::base::DUP_INTERFACE) { }
// Send buffer contents over the connection.
bool handle( mi::http::IConnection* connection)
{
connection->enqueue( m_buffer.get());
return true;
}
private:
// The stored buffer
};
// Simple response handler which always sets the content type for JPG images
class Response_handler : public mi::base::Interface_implement<mi::http::IResponse_handler>
{
public:
void handle( mi::http::IConnection* connection)
{
mi::http::IResponse* iresponse( connection->get_response());
iresponse->set_header( "Content-Type", "image/jpeg");
}
};
void configuration( mi::neuraylib::INeuray* neuray, const char* mdl_path)
{
// Configure the neuray library. Here we set the search path for .mdl files.
check_success( rc->add_mdl_path( mdl_path) == 0);
check_success( rc->add_mdl_path( ".") == 0);
// Load the OpenImageIO, Iray Photoreal, and .mi importer plugins.
check_success( pc->load_plugin_library( "nv_openimageio" MI_BASE_DLL_FILE_EXT) == 0);
check_success( pc->load_plugin_library( "libiray" MI_BASE_DLL_FILE_EXT) == 0);
check_success( pc->load_plugin_library( "mi_importer" MI_BASE_DLL_FILE_EXT) == 0);
}
mi::neuraylib::INeuray* neuray, const char* scene_file)
{
// Get the database, the global scope of the database, and create a transaction in the global
// scope for importing the scene file and storing the scene.
check_success( database.is_valid_interface());
database->get_global_scope());
scope->create_transaction());
check_success( transaction.is_valid_interface());
// Import the scene file
check_success( import_api.is_valid_interface());
mi::base::Handle<const mi::IString> uri( import_api->convert_filename_to_uri( scene_file));
import_api->import_elements( transaction.get(), uri->get_c_str()));
check_success( import_result->get_error_number() == 0);
// Create the scene object
transaction->create<mi::neuraylib::IScene>( "Scene"));
scene->set_rootgroup( import_result->get_rootgroup());
scene->set_options( import_result->get_options());
scene->set_camera_instance( import_result->get_camera_inst());
transaction->store( scene.get(), "the_scene");
// Create the render context using the Iray Photoreal render mode
scene = transaction->edit<mi::neuraylib::IScene>( "the_scene");
scene->create_render_context( transaction.get(), "iray"));
check_success( render_context.is_valid_interface());
mi::base::Handle<mi::IString> scheduler_mode( transaction->create<mi::IString>());
scheduler_mode->set_c_str( "batch");
render_context->set_option( "scheduler_mode", scheduler_mode.get());
scene = 0;
// Create the render target and render the scene
new Render_target( image_api.get(), "Color", 512, 384));
check_success( render_context->render( transaction.get(), render_target.get(), 0) >= 0);
// Access the first canvas of the render target
mi::base::Handle<mi::neuraylib::ICanvas> canvas( render_target->get_canvas( 0));
// Convert content of the canvas to a JPG image
image_api->create_buffer_from_canvas( canvas.get(), "jpg", "Rgb", "100"));
transaction->commit();
return buffer;
}
void run_http_server(
mi::neuraylib::INeuray* neuray, mi::neuraylib::IBuffer* buffer, const char* port)
{
// Create a server instance
http_factory->create_server());
// Install our request and response handlers
mi::base::Handle<mi::http::IRequest_handler> request_handler( new Request_handler( buffer));
http_server->install( request_handler.get());
mi::base::Handle<mi::http::IResponse_handler> response_handler( new Response_handler());
http_server->install( response_handler.get());
// Assemble server address
std::string address = "0.0.0.0:";
address += port;
// Run server for fixed time interval
http_server->start( address.c_str());
sleep_seconds( 30);
http_server->shutdown();
}
int main( int argc, char* argv[])
{
// Collect command line parameters
if( argc != 4) {
std::cerr << "Usage: example_http_server <scene_file> <mdl_path> <port>" << std::endl;
keep_console_open();
return EXIT_FAILURE;
}
const char* scene_file = argv[1];
const char* mdl_path = argv[2];
const char* port = argv[3];
// Access the neuray library
mi::base::Handle<mi::neuraylib::INeuray> neuray( load_and_get_ineuray());
check_success( neuray.is_valid_interface());
// Configure the neuray library
configuration( neuray.get(), mdl_path);
// Start the neuray library
mi::Sint32 result = neuray->start();
check_start_success( result);
// Do the actual rendering
mi::base::Handle<mi::neuraylib::IBuffer> buffer = rendering( neuray.get(), scene_file);
// Serve image via HTTP server on given port
run_http_server( neuray.get(), buffer.get(), port);
buffer = 0;
// Shut down the neuray library
check_success( neuray->shutdown() == 0);
neuray = 0;
// Unload the neuray library
check_success( unload());
keep_console_open();
return EXIT_SUCCESS;
}
A simple string class.
Definition: istring.h:22
Handle class template for interfaces, automatizing the lifetime control via reference counting.
Definition: handle.h:113
Mixin class template for deriving interface implementations.
Definition: interface_implement.h:41
The connection class represents a connection from a client to the server.
Definition: http.h:277
virtual IResponse * get_response()=0
Returns the response associated with the connection.
virtual bool enqueue(neuraylib::IBuffer *buffer)=0
Enqueues a buffer to be sent on the connection.
The factory can be used to instantiate the built-in HTTP classes.
Definition: http.h:922
This interface holds all the parameters of a response.
Definition: http.h:197
Abstract interface for a simple buffer with binary data.
Definition: ibuffer.h:25
This interface is used to interact with the distributed database.
Definition: idatabase.h:293
This interface provides various utilities related to canvases and buffers.
Definition: iimage_api.h:49
This interface is used to import files.
Definition: iimport_api.h:100
This is an object representing the Iray library.
Definition: ineuray.h:44
virtual Sint32 shutdown(bool blocking=true)=0
Shuts down the library.
virtual base::IInterface * get_api_component(const base::Uuid &uuid) const =0
Returns an API component from the Iray SDK API.
virtual Sint32 start(bool blocking=true)=0
Starts the operation of the Iray library.
This interface is used to load plugins and to query information about loaded plugins.
Definition: iplugin_configuration.h:24
This interface is used to query and change the rendering configuration.
Definition: irendering_configuration.h:109
The scene is the top-level element describing a subset of DB elements to be rendered.
Definition: iscene.h:44
#define MI_BASE_DLL_FILE_EXT
The operating system specific default filename extension for shared libraries (DLLs)
Definition: config.h:340
static const Dup_interface DUP_INTERFACE
Symbolic constant to trigger a special constructor in the Handle class.
Definition: handle.h:37
Interface * get() const
Access to the interface. Returns 0 for an invalid interface.
Definition: handle.h:294
signed int Sint32
32-bit signed integer.
Definition: types.h:46
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: neuraylib.h:179
Iray SDK API.
[Previous] [Next] [Up]