コード例 #1
0
ファイル: Engine.cpp プロジェクト: Meronw/ngrest
void Engine::dispatchMessage(MessageContext* context)
{
    NGREST_ASSERT_PARAM(context);
    NGREST_ASSERT_NULL(context->request);
    NGREST_ASSERT_NULL(context->response);
    NGREST_ASSERT_NULL(context->callback);

    try {
        // this will replace context callback and restore it after dispatching the message
        context->pool->alloc<EngineHookCallback>(context);

        if (context->request->body) {
            context->request->node = context->transport->parseRequest(context->pool, context->request);
            NGREST_ASSERT(context->request->node, "Failed to read request"); // should never throw
        }

        dispatcher.dispatchMessage(context);
    } catch (const Exception& err) {
        LogWarning() << err.getFileLine() << " " << err.getFunction() << " : " << err.what();
        context->callback->error(err);
    }
}
コード例 #2
0
void TestDeploymentWrapper::invoke(const OperationDescription* operation, MessageContext* context)
{
    if (operation->name == "echoSync") {
        NGREST_ASSERT(context->request->node, "Request expected for Service:operation");
        NGREST_ASSERT_PARAM(context->request->node->type == NodeType::Object);

        const Object* requestNode = static_cast<const Object*>(context->request->node);

        NamedNode* valueNode = requestNode->findChildByName("value");
        NGREST_ASSERT_NULL(valueNode);
        NGREST_ASSERT_PARAM(valueNode->node->type == NodeType::Value);
        const char* value = static_cast<const Value*>(valueNode->node)->value;
        NGREST_ASSERT_NULL(value);


        const std::string& result = service->echoSync(value);

        Object* responseNode = context->pool->alloc<Object>();

        NamedNode* resultNode = context->pool->alloc<NamedNode>("result");
        resultNode->node = context->pool->alloc<Value>(ValueType::String, result.c_str());

        responseNode->firstChild = resultNode;

        context->response->node = responseNode;

        context->callback->success();
    } else if (operation->name == "echoASync") {
        NGREST_ASSERT(context->request->node, "Request expected for Service:operation");
        NGREST_ASSERT_PARAM(context->request->node->type == NodeType::Object);

        const Object* requestNode = static_cast<const Object*>(context->request->node);

        NamedNode* valueNode = requestNode->findChildByName("value");
        NGREST_ASSERT_NULL(valueNode);
        NGREST_ASSERT_PARAM(valueNode->node->type == NodeType::Value);
        const char* value = static_cast<const Value*>(valueNode->node)->value;
        NGREST_ASSERT_NULL(value);

        class Callback_echoASync: public Callback<const std::string&>
        {
        public:
            Callback_echoASync(MessageContext* context_):
                context(context_)
            {
            }

            void success(const std::string& result) override
            {
                Object* responseNode = context->pool->alloc<Object>();

                NamedNode* resultNode = context->pool->alloc<NamedNode>("result");
                resultNode->node = context->pool->alloc<Value>(ValueType::String, result.c_str());

                responseNode->firstChild = resultNode;

                context->response->node = responseNode;

                context->callback->success();
                // no need to "delete this" - it's in mempool
            }

            virtual void error(const Exception& error)
            {
                context->callback->error(error);
                // no need to "delete this" - it's in mempool
            }

            MessageContext* context;
        };

        service->echoASync(value, *context->pool->alloc<Callback_echoASync>(context));

    } else {
        NGREST_THROW_ASSERT("No operation " + operation->name + " found");
    }
}