2010-05-14 03:38:39 +04:00
/* -------------------------------------------------------------------------- */
2020-04-30 16:00:02 +03:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2010-05-14 03:38:39 +04:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef ONE_CLIENT_H_
# define ONE_CLIENT_H_
# include <xmlrpc-c/base.hpp>
# include <xmlrpc-c/client_simple.hpp>
2016-03-24 04:07:04 +03:00
# include <xmlrpc-c/girerr.hpp>
2010-05-14 03:38:39 +04:00
# include <string>
2010-05-14 17:30:21 +04:00
2010-05-14 03:38:39 +04:00
// =============================================================================
// Doc:
// http://xmlrpc-c.sourceforge.net/doc/#clientexamplepp
// http://xmlrpc-c.sourceforge.net/doc/libxmlrpc_client++.html#simple_client
// =============================================================================
/**
* This class represents the connection with the core and handles the
* xml - rpc calls .
*/
2016-03-24 04:07:04 +03:00
class Client
2010-05-14 03:38:39 +04:00
{
public :
2016-03-08 18:38:51 +03:00
/**
* Singleton accessor
*/
static Client * client ( )
{
return _client ;
} ;
2010-05-14 03:38:39 +04:00
/**
2016-03-08 18:38:51 +03:00
* Singleton initializer
2010-05-14 03:38:39 +04:00
*/
2016-03-24 04:07:04 +03:00
static Client * initialize ( const std : : string & secret ,
const std : : string & endpoint , size_t message_size , unsigned int tout )
2010-05-14 03:38:39 +04:00
{
2016-03-08 18:38:51 +03:00
if ( _client = = 0 )
{
2016-03-24 04:07:04 +03:00
_client = new Client ( secret , endpoint , message_size , tout ) ;
2016-03-08 18:38:51 +03:00
}
2010-05-17 13:23:26 +04:00
2016-03-08 18:38:51 +03:00
return _client ;
} ;
2010-05-14 03:38:39 +04:00
2016-03-08 18:38:51 +03:00
size_t get_message_size ( ) const
2013-12-21 04:35:51 +04:00
{
return xmlrpc_limit_get ( XMLRPC_XML_SIZE_LIMIT_ID ) ;
2016-03-08 18:38:51 +03:00
} ;
2013-12-21 04:35:51 +04:00
2016-03-08 18:38:51 +03:00
/**
* Reads ONE_AUTH from environment or its default location at
* $ HOME / . one / one_auth
*/
2016-03-24 04:07:04 +03:00
static int read_oneauth ( std : : string & secret , std : : string & error ) ;
2017-05-08 20:48:41 +03:00
/**
* Performs a xmlrpc call to the initialized server
* @ param method name
* @ param plist initialized param list
* @ param result of the xmlrpc call
*/
void call ( const std : : string & method , const xmlrpc_c : : paramList & plist ,
xmlrpc_c : : value * const result ) ;
/**
* Performs a xmlrpc call
* @ param endpoint of server
* @ param method name
* @ param plist initialized param list
2017-05-24 21:38:13 +03:00
* @ param timeout ( ms ) for the request , set 0 for global xml_rpc timeout
2017-05-08 20:48:41 +03:00
* @ param result of the xmlrpc call
* @ param error string if any
* @ return 0
*/
static int call ( const std : : string & endpoint , const std : : string & method ,
2017-06-03 19:16:24 +03:00
const xmlrpc_c : : paramList & plist , unsigned int _timeout ,
2017-05-08 20:48:41 +03:00
xmlrpc_c : : value * const result , std : : string & error ) ;
2016-03-24 04:07:04 +03:00
/**
* Performs an xmlrpc call to the initialized server and credentials .
* This method automatically adds the credential argument .
* @ param method name
* @ param format of the arguments , supported arguments are i : int , s : string
* and b : bool
* @ param result to store the xmlrpc call result
* @ param . . . xmlrpc arguments
*/
void call ( const std : : string & method , const std : : string format ,
xmlrpc_c : : value * const result , . . . ) ;
2010-05-14 03:38:39 +04:00
private :
2016-03-08 18:38:51 +03:00
/**
* Creates a new xml - rpc client with specified options .
*
* @ param secret A string containing the ONE user : password tuple .
* If not set , the auth . file will be assumed to be at $ ONE_AUTH
* @ param endpoint Where the rpc server is listening , must be something
* like " http://localhost:2633/RPC2 " . If not set , the endpoint will be set
* to $ ONE_XMLRPC .
* @ param message_size for XML elements in the client library ( in bytes )
* @ throws Exception if the authorization options are invalid
*/
2020-07-02 23:42:10 +03:00
Client ( const std : : string & secret , const std : : string & endpoint , size_t message_size ,
2016-03-24 04:07:04 +03:00
unsigned int tout ) ;
2016-03-08 18:38:51 +03:00
2020-07-02 23:42:10 +03:00
std : : string one_auth ;
std : : string one_endpoint ;
2010-05-14 03:38:39 +04:00
2016-03-24 04:07:04 +03:00
unsigned int timeout ;
2016-03-08 18:38:51 +03:00
static Client * _client ;
2010-05-14 03:38:39 +04:00
} ;
# endif /*ONECLIENT_H_*/