00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include "drmssite_info.h"
00061 #include "jsoc.h"
00062 #include "serverdefs.h"
00063
00064 short drmssite_code_from_sunum (unsigned long long sunum) {
00065 return (sunum & 0xffff000000000000) >> 48;
00066 }
00067
00068 #ifdef DEFS_CLIENT
00069 short drmssite_client_info_from_sunum (unsigned long long sunum,
00070 int sockfd, DRMSSiteInfo_t **info) {
00071 #else
00072 short drmssite_server_info_from_sunum (unsigned long long sunum,
00073 DB_Handle_t *dbin, DRMSSiteInfo_t **info) {
00074 #endif
00075 DB_Text_Result_t *text_res = NULL;
00076 int current, col;
00077 char query[DRMSSITE_MAXQUERYLEN];
00078 char *schema, *series;
00079
00080 short site_code = -1;
00081 *info = NULL;
00082
00083 site_code = (sunum & 0xffff000000000000) >> 48;
00084 if (site_code < 0) return site_code;
00085
00086
00087 schema = strdup (DEFAULT_SITE_TABLE_0);
00088 series = strchr (schema, '.');
00089 *series = '\0';
00090 series++;
00091 sprintf (query,
00092 "select * from pg_catalog.pg_tables where schemaname = \'%s\' and tablename = \'%s\'",
00093 schema, series);
00094 #ifdef DEFS_CLIENT
00095 text_res = db_client_query_txt (sockfd, query, 0, NULL);
00096 #else
00097 text_res = db_query_txt (dbin, query);
00098 #endif
00099 if (!text_res->num_rows) {
00100
00101 free (schema);
00102 schema = strdup (DEFAULT_SITE_TABLE_1);
00103 series = strchr (schema, '.');
00104 *series = '\0';
00105 series++;
00106 sprintf (query,
00107 "select * from pg_catalog.pg_tables where schemaname = \'%s\' and tablename = \'%s\'",
00108 schema, series);
00109
00110 if (text_res) {
00111 db_free_text_result(text_res);
00112 text_res = NULL;
00113 }
00114 #ifdef DEFS_CLIENT
00115 text_res = db_client_query_txt (sockfd, query, 0, NULL);
00116 #else
00117 text_res = db_query_txt (dbin, query);
00118 #endif
00119 if (!text_res->num_rows) {
00120 fprintf (stderr, "Warning: neither default table %s nor %s exists\n",
00121 DEFAULT_SITE_TABLE_0, DEFAULT_SITE_TABLE_1);
00122 return site_code;
00123 }
00124 }
00125
00126 sprintf (query, "select * from %s.%s where SiteCode = %d order by recnum",
00127 schema, series, site_code);
00128
00129 if (text_res) {
00130 db_free_text_result(text_res);
00131 text_res = NULL;
00132 }
00133 #ifdef DEFS_CLIENT
00134 text_res = db_client_query_txt (sockfd, query, 0, NULL);
00135 #else
00136 text_res = db_query_txt (dbin, query);
00137 #endif
00138 current = text_res->num_rows - 1;
00139 if (current < 0) return site_code;
00140
00141 *info = (DRMSSiteInfo_t *)malloc (sizeof (struct DRMSSiteInfo_struct));
00142 memset(*info, 0, sizeof (struct DRMSSiteInfo_struct));
00143 (*info)->site_code = site_code;
00144
00145 for (col = 0; col < text_res->num_cols; col++) {
00146 if (!strcasecmp (text_res->column_name[col], "SiteName"))
00147 (*info)->site_name = strdup (text_res->field[current][col]);
00148 if (!strcasecmp (text_res->column_name[col], "Location"))
00149 (*info)->location = strdup (text_res->field[current][col]);
00150 if (!strcasecmp (text_res->column_name[col], "Contact"))
00151 (*info)->contact = strdup (text_res->field[current][col]);
00152 if (!strcasecmp (text_res->column_name[col], "Notify"))
00153 (*info)->notify = strdup (text_res->field[current][col]);
00154 if (!strcasecmp (text_res->column_name[col], "DataURL"))
00155 (*info)->data_URL = strdup (text_res->field[current][col]);
00156 if (!strcasecmp (text_res->column_name[col], "QueryURL"))
00157 (*info)->query_URL = strdup (text_res->field[current][col]);
00158 if (!strcasecmp (text_res->column_name[col], "ReqstURL"))
00159 (*info)->request_URL = strdup (text_res->field[current][col]);
00160 if (!strcasecmp (text_res->column_name[col], "SUMS_URL"))
00161 (*info)->SUMS_URL = strdup (text_res->field[current][col]);
00162 if (!strcasecmp (text_res->column_name[col], "Owner"))
00163 (*info)->owner = strdup (text_res->field[current][col]);
00164 if (!strcasecmp (text_res->column_name[col], "DBName"))
00165 (*info)->db_name = strdup (text_res->field[current][col]);
00166 if (!strcasecmp (text_res->column_name[col], "DBServer"))
00167 (*info)->db_server = strdup (text_res->field[current][col]);
00168 if (!strcasecmp (text_res->column_name[col], "NameList"))
00169 (*info)->namelist = strdup (text_res->field[current][col]);
00170 }
00171
00172 if (text_res) {
00173 db_free_text_result(text_res);
00174 text_res = NULL;
00175 }
00176
00177 free (schema);
00178
00179 return site_code;
00180 }
00181
00182 #ifdef DEFS_CLIENT
00183 short drmssite_client_getlocalinfo(int sockfd, DRMSSiteInfo_t **info) {
00184 return drmssite_client_info_from_sunum(DRMS_LOCAL_SITE_CODE, sockfd, info);
00185 }
00186 #else
00187 short drmssite_server_getlocalinfo(DB_Handle_t *dbin, DRMSSiteInfo_t **info) {
00188 return drmssite_server_info_from_sunum(DRMS_LOCAL_SITE_CODE, dbin, info);
00189 }
00190 #endif
00191
00192 int drmssite_sunum_is_local (unsigned long long sunum) {
00193 return (drmssite_code_from_sunum (sunum) == DRMS_LOCAL_SITE_CODE);
00194 }
00195
00196 void drmssite_freeinfo(DRMSSiteInfo_t **info) {
00197 if (info && *info) {
00198 DRMSSiteInfo_t *iinfo = *info;
00199
00200
00201 if (iinfo->site_name) free(iinfo->site_name);
00202 if (iinfo->location) free(iinfo->location);
00203 if (iinfo->contact) free(iinfo->contact);
00204 if (iinfo->notify) free(iinfo->notify);
00205 if (iinfo->data_URL) free(iinfo->data_URL);
00206 if (iinfo->query_URL) free(iinfo->query_URL);
00207 if (iinfo->request_URL) free(iinfo->request_URL);
00208 if (iinfo->SUMS_URL) free(iinfo->SUMS_URL);
00209 if (iinfo->owner) free(iinfo->owner);
00210 if (iinfo->db_name) free(iinfo->db_name);
00211 if (iinfo->db_server) free(iinfo->db_server);
00212 if (iinfo->namelist) free(iinfo->namelist);
00213
00214 free(*info);
00215 *info = NULL;
00216 }
00217 }
00218
00219 #ifndef DEFS_CLIENT
00220 int drmssite_server_siteinfo(int sockfd, DB_Handle_t *db_handle)
00221 {
00222
00223
00224
00225
00226 int err = 0;
00227
00228 err = db_server_query_txt(sockfd, db_handle);
00229 err |= db_server_query_txt(sockfd, db_handle);
00230 err |= db_server_query_txt(sockfd, db_handle);
00231
00232 return err;
00233 }
00234
00235 int drmssite_server_localsiteinfo(int sockfd, DB_Handle_t *db_handle)
00236 {
00237 return drmssite_server_siteinfo(sockfd, db_handle);
00238 }
00239 #endif
00240
00241
00242
00243
00244
00245
00246