Patch from Scott McKellar; use camelCase instead of under_score function names
[OpenSRF.git] / src / libopensrf / osrf_settings.c
1 #include <opensrf/osrf_settings.h> 
2
3 osrf_host_config* config = NULL;
4
5 char* osrf_settings_host_value(const char* format, ...) {
6         VA_LIST_TO_STRING(format);
7
8         if( ! config ) {
9                 const char * msg = "NULL config pointer";
10                 fprintf( stderr, "osrf_settings_host_value: %s\n", msg );
11                 osrfLogError( OSRF_LOG_MARK, msg );
12                 exit( 99 );
13         }
14
15         jsonObject* o = jsonObjectFindPath(config->config, VA_BUF);
16         char* val = jsonObjectToSimpleString(o);
17         jsonObjectFree(o);
18         return val;
19 }
20
21 jsonObject* osrf_settings_host_value_object(const char* format, ...) {
22         VA_LIST_TO_STRING(format);
23
24         if( ! config ) {
25                 const char * msg = "config pointer is NULL";
26                 fprintf( stderr, "osrf_settings_host_value_object: %s\n", msg );
27                 osrfLogError( OSRF_LOG_MARK, msg );
28                 exit( 99 );
29         }
30
31         return jsonObjectFindPath(config->config, VA_BUF);
32 }
33
34
35 int osrf_settings_retrieve(const char* hostname) {
36
37         if(!config) {
38
39                 osrfAppSession* session = osrf_app_client_session_init("opensrf.settings");
40                 jsonObject* params = jsonNewObject(NULL);
41                 jsonObjectPush(params, jsonNewObject(hostname));
42                 int req_id = osrfAppSessionMakeRequest( 
43                         session, params, "opensrf.settings.host_config.get", 1, NULL );
44                 osrf_message* omsg = osrfAppSessionRequestRecv( session, req_id, 60 );
45                 jsonObjectFree(params);
46
47                 if(!omsg) {
48                         osrfLogError( OSRF_LOG_MARK, "No osrf_message received from host %s (timeout?)", hostname);
49                 } else if(!omsg->_result_content) {
50                         osrf_message_free(omsg);
51                         osrfLogError(
52                                 OSRF_LOG_MARK,
53                                 "NULL or non-existant osrf_message result content received from host %s, "
54                                 "broken message or no settings for host",
55                                 hostname
56                         );
57                 } else {
58                         config = osrf_settings_new_host_config(hostname);
59                         config->config = jsonObjectClone(omsg->_result_content);
60                         osrf_message_free(omsg);
61                 }
62
63                 osrf_app_session_request_finish( session, req_id );
64                 osrfAppSessionFree( session );
65
66                 if(!config) {
67                         osrfLogError( OSRF_LOG_MARK, "Unable to load config for host %s", hostname);
68                         return -1;
69                 }
70         }
71
72         return 0;
73 }
74
75 osrf_host_config* osrf_settings_new_host_config(const char* hostname) {
76         if(!hostname) return NULL;
77         osrf_host_config* c = safe_malloc(sizeof(osrf_host_config));
78         c->hostname = strdup(hostname);
79         c->config = NULL;
80         return c;
81 }
82
83 void osrf_settings_free_host_config(osrf_host_config* c) {
84         if(!c) c = config;
85         if(!c) return;
86         free(c->hostname);
87         jsonObjectFree(c->config);      
88         free(c);
89 }