HttpClient::HttpClient(const std::string& url) throw(JsonRpcException)
        : AbstractClientConnector(), url(url)
    {
        curl = curl_easy_init();
        if (!curl)
        {
            throw JsonRpcException(Errors::ERROR_CLIENT_CONNECTOR, ": libcurl initialization error");
        }

        curl_easy_setopt(curl, CURLOPT_URL, this->url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    }
Esempio n. 2
0
  void SocketServer::CreateSocket() throw (JsonRpcException) {
    int yes = 1;
    socket_ = socket(host_info_->ai_family, host_info_->ai_socktype, host_info_->ai_protocol);
    CHECK_SOCKET(socket_);
    CHECK_STATUS(setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)));
    CHECK_STATUS(bind(socket_, host_info_->ai_addr, host_info_->ai_addrlen));
    CHECK_STATUS(listen(socket_, poolSize_));
    return;
error:
    CloseSocket();
    throw JsonRpcException(Errors::ERROR_SERVER_CONNECTOR);
  }
Esempio n. 3
0
    Json::Value
    SynchronousCallback::invoke
        ( JsonRpc * jsonRpc
        , std::string const & methodName
        , Json::Value const & params
        )
    {
        SynchronousCallback callback;

        // Acquire the mutex. This ensures the response thread is
        // blocked until this thread has had a chance to start waiting
        // for a response
        QMutexLocker locker (& callback.m_mutex);

        // Send the request
        jsonRpc->invoke (methodName, params, & callback);

        // Start waiting for a response
        Json::Value const response (callback.wait ());

        // Handle the response
        if (isErrorResponse (response))
        {
            Json::Value const error (response["error"]);
            int const code (error["code"].asInt ());
            std::string const message (error["message"].asString ());
            Json::Value const data (error["data"]);

            fprintf(stderr,
                "Error invoking \"%s\"\n"
                "This may indicate mismatched "
                "Conveyor and MakerWare versions,\n"
                "check your Conveyor and MakerWare "
                "logs for more information.\n\n",
                methodName.c_str());

            throw JsonRpcException (methodName, params, code, message, data);
        }
        else
        if (not isSuccessResponse (response))
        {
            throw std::runtime_error("Response is not success!");
        }
        else
        {
            Json::Value const result (response["result"]);
            return result;
        }
    }
    void SpecificationParser::GetFileContent(const std::string &filename, std::string& target)
    {
        ifstream config(filename.c_str());

        if (config)
        {
            config.open(filename.c_str(), ios::in);
            target.assign((std::istreambuf_iterator<char>(config)),
                          (std::istreambuf_iterator<char>()));
        }
        else
        {
            throw JsonRpcException(Errors::ERROR_SERVER_PROCEDURE_SPECIFICATION_NOT_FOUND, filename);
        }
    }
Esempio n. 5
0
  SocketServer::SocketServer(const std::string& port, const int type, const int pool) throw (JsonRpcException) :
    AbstractServerConnector(),
    host_info_(NULL),
    shutdown_(false),
    poolSize_(pool)
  {
    struct addrinfo hint;
    memset((char*)&hint, 0, sizeof(hint));
    hint.ai_family = AF_INET;
    hint.ai_socktype = type;
    hint.ai_flags = AI_PASSIVE;
    int status = getaddrinfo(NULL, port.c_str(), &hint, &host_info_);
    CHECK(status);
    return;
error:
    throw JsonRpcException(Errors::ERROR_SERVER_CONNECTOR);
  }
    procedurelist_t *SpecificationParser::GetProceduresFromString(const string &content) throw(JsonRpcException)
    {

        Json::Reader reader;
        Json::Value val;
        if(!reader.parse(content,val)) {
            throw JsonRpcException(Errors::ERROR_RPC_JSON_PARSE_ERROR, " specification file contains syntax errors");
        }

        procedurelist_t* procedures = new procedurelist_t();
        Procedure* proc;
        for (unsigned int i = 0; i < val.size(); i++)
        {
            proc = GetProcedure(val[i]);
            (*procedures)[proc->GetProcedureName()] = proc;
        }
        return procedures;
    }
    void HttpClient::SendMessage(const std::string& message, std::string& result) throw (JsonRpcException)
    {
        CURLcode res;

        struct string s;
        init_string(&s);

        struct curl_slist * headers = NULL;
        //Maybe to restrictive
        //headers = curl_slist_append(headers, "Accept: application/json");
        for (std::map<std::string, std::string>::iterator header = this->headers.begin(); header != this->headers.end(); ++header) {
            headers = curl_slist_append(headers, (header->first + ": " + header->second).c_str());
		}

        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "charsets: utf-8");

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        res = curl_easy_perform(curl);

        result = s.ptr;
        free(s.ptr);
        curl_slist_free_all(headers);
        if (res != CURLE_OK)
        {
            stringstream str;
            if(res == 7)
            {
                str << ": Could not connect to " << this->url;
            }
            else
            {
                str << ": libcurl error: " << res;
            }
            throw JsonRpcException(Errors::ERROR_CLIENT_CONNECTOR, str.str());
        }
    }
    Procedure* SpecificationParser::GetProcedure(Json::Value &signature)
    {
        procedure_t procedureType;
        std::string name_key;
        std::string procedureName;
        Procedure* result;

        if ((signature.isMember(KEY_METHOD_NAME)
             || signature.isMember(KEY_NOTIFICATION_NAME))
                && signature.isMember(KEY_PROCEDURE_PARAMETERS))
        {
            if (signature.isMember(KEY_METHOD_NAME))
            {
                name_key = KEY_METHOD_NAME;
                procedureType = RPC_METHOD;
            }
            else
            {
                name_key = KEY_NOTIFICATION_NAME;
                procedureType = RPC_NOTIFICATION;

            }
            if (signature[name_key].isString()
                    && (signature[KEY_PROCEDURE_PARAMETERS].isObject() || signature[KEY_PROCEDURE_PARAMETERS].isNull()))
            {
                procedureName = signature[name_key].asString();
                vector<string> parameters =
                        signature[KEY_PROCEDURE_PARAMETERS].getMemberNames();
                if(procedureType == RPC_NOTIFICATION)
                {
                    result = new Procedure(procedureName, NULL);
                }
                else
                {
                    jsontype_t returntype = JSON_OBJECT;
                    if(signature.isMember(KEY_RETURN_TYPE))
                    {
                        returntype = toJsonType(signature[KEY_RETURN_TYPE]);
                    }
                    result = new Procedure(procedureName, returntype, NULL);
                }
                for (unsigned int i = 0; i < parameters.size(); i++)
                {
                    result->AddParameter(parameters.at(i), toJsonType(signature[KEY_PROCEDURE_PARAMETERS][parameters.at(i)]));
                }
            }
            else
            {
                throw JsonRpcException(Errors::ERROR_SERVER_PROCEDURE_SPECIFICATION_SYNTAX,
                                       "Invalid signature types in fileds: "
                                       + signature.toStyledString());
            }
        }
        else
        {
            throw JsonRpcException(Errors::ERROR_SERVER_PROCEDURE_SPECIFICATION_SYNTAX,
                                   "procedure declaration does not contain method/notification name or paramters: "
                                   + signature.toStyledString());
        }
        return result;
    }