classPiiNetworkInputOperation
#include <PiiNetworkInputOperation.h>
An operation that receives objects through a network connection with the HTTP protol.
Inherits PiiNetworkOperation, PiiHttpProtocol::UriHandler
Description
PiiNetworkInputOperation registers itself as a handler into a PiiHttpServer's protocol. It accepts many different input formats and is able to automatically decode data in various formats.
Inputs // This code implements a complete HTTP/1.1 server that calculates
// the sum of two numerical query arguments. The server supports
// both GET and POST queries.
// Start the server and browse to http://localhost:8080/sum?arg1=2&arg2=4.
// The server will respond with "6".
#include <QCoreApplication>
#include <PiiHttpServer.h>
#include <PiiEngine.h>
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
PiiEngine engine;
try
{
PiiEngine::loadPlugin("piinetwork");
PiiEngine::loadPlugin("piibase");
// Create a configuration
PiiOperation* net = engine.createOperation("PiiNetworkInputOperation");
net->setProperty("outputNames", QStringList() << "arg1" << "arg2");
net->setProperty("httpServer", "myServer");
net->setObjectName("sum");
PiiOperation* sum = engine.createOperation("PiiArithmeticOperation");
net->connectOutput("arg1", sum, "input0");
net->connectOutput("arg2", sum, "input1");
sum->connectOutput("output", net, "body");
// Start the HTTP server
PiiHttpServer* pServer = PiiHttpServer::addServer("myServer", "tcp://0.0.0.0:8080");
if (!pServer->start())
return 1;
// Start the execution engine
engine.execute();
}
catch (PiiException& ex)
{
qDebug(ex.message().toUtf8().constData());
return 1;
}
// Run eternally
return app.exec();
}
Input decoding
int, it will be converted. double will be tried next, and if that is not successful, the value will be used as a string. Properties
|
The name of the HTTP server you want to handle incoming requests to this operation. |
|
|
The message sent to the HTTP client if the operation is interrupted while processing a request. |
|
|
The message sent to the HTTP client if the operation fails to receive a response object within time limits. |
|
|
The URI of the operation within the server. |
Constructors and destructor
Public member functions
|
virtual void
|
(
Checks the operation for execution. |
|
virtual void
|
Handles a request. |
|
(
)
|
|
|
virtual void
|
(
)
Prepares the operation for pausing. |
|
void
|
|
|
void
|
|
|
void
|
|
|
void
|
|
|
virtual void
|
(
)
Prepares the operation for stopping. |
|
(
)
|
|
|
(
)
|
Protected member functions
|
virtual void
|
(
)
Executes one round of processing. |
Property details
-
QString httpServer
[read, write]The name of the HTTP server you want to handle incoming requests to this operation.
The server must be previously created with PiiHttpServer::addServer(). If you want to create a server for this operation only, use a URI here. See PiiHttpServer::addServer() for URI syntax.
PiiOperation* pReceiver = engine.createOperation("PiiNetworkInputOperation"); pReceiver->setProperty("httpServer", "tcp://0.0.0.0:8080/");
-
QString interruptedResponse
[read, write]The message sent to the HTTP client if the operation is interrupted while processing a request.
The default is "The operation was interrupted."
-
QString timeoutResponse
[read, write]The message sent to the HTTP client if the operation fails to receive a response object within time limits.
The default is "Timed out while waiting for response."
-
QString uri
[read, write]The URI of the operation within the server.
Note that each operation must have a unique URI in the context of a HTTP server. If
uriis empty,/objectNamewill be used as the URI.
Function details
-
PiiNetworkInputOperation
() -
~PiiNetworkInputOperation
() -
virtual void check
(- bool reset
[virtual]Checks the operation for execution.
This function creates a suitable flow controller by calling createFlowController(). It then sets the flow controller to the active processor and sets the processor as the input controller for all inputs.
If you change socket groupings in your overridden implementation, please call PiiDefaultOperation::check() after that. Otherwise, your new groupings will not be in effect.
Reimplemented from PiiNetworkOperation.
-
virtual void handleRequest
[virtual]Handles a request.
This function must be thread-safe.
void MyHandler::handleRequest(const QString& uri, PiiHttpDevice* dev, TimeLimiter*) { // Find the path of the request wrt to the "root" of this handler QString strRequestPath(dev->requestPath(uri)); if (strRequestPath == "index.html" && dev->requestMethod() == "GET") dev->print("<html><head><title>Hello world!</title></head><body><!-- Secret message --></body></html>"); }
Parameters
- uri
the URI the handler was registered at. Use the PiiHttpDevice::requestUri() function to fetch the full request URI.
- dev
the communication device.
PiiHttpProtocolhas already fetched request headers, and the device is positioned at the beginning of request data.- controller
a progress controller. Call the PiiProgressController::canContinue() with no parameters time to time to ensure you are still allowed to continue communication. Returning from this function will automatically flush the output pending in
dev.
Exceptions
- The
function may throw a PiiHttpException on error. PiiHttpProtocol sets the response header correspondingly and writes message to the response body.
Reimplemented from PiiHttpProtocol::UriHandler.
-
QString httpServer
() -
QString interruptedResponse
() -
virtual void pause
()[virtual]Prepares the operation for pausing.
The functioning is dependent on the type of the processor. In threaded mode, the operation will be turned into
Pausingstate, and processing will pause once the thread has finished its current processing round. In simple mode, the processor will check if the operation has connected inputs. If it does, the operation will turn intoPausingstate and wait until it receives pause signals from previous operations in the pipeline. If there are no connected inputs, the operation will immediately turn intoPausedstate.Reimplemented from PiiDefaultOperation.
-
void setHttpServer
-
void setInterruptedResponse
-
void setTimeoutResponse
-
void setUri
-
virtual void stop
()[virtual]Prepares the operation for stopping.
Works analogously to pause().
Reimplemented from PiiDefaultOperation.
-
QString timeoutResponse
() -
QString uri
() -
virtual void process
()[protected, virtual]Executes one round of processing.
This function is invoked by the processor if the necessary preconditions for a new processing round are met. This function does all the necessary calculations to create output objects and sends them to output sockets.
Calls to process(), syncEvent(), and setProperty() are synchronized and cannot occur simultaneously. PiiDefaultOperation ensures this by locking processLock() for reading before calling process().
Note: With time-consuming operations, one should occasionally check that the operation hasn't been interrupted, i.e. that state() returns
Running.Exceptions
- PiiExecutionException
whenever an unrecoverable error occurs during a processing round, the operation is interrupted, or finishes execution due to end of input data.
Reimplemented from PiiDefaultOperation.
Add a note
Not a single note added yet. Be the first, add yours.