Changeset 604 for branches/PublicaMundi_David-devel/zoo-project/zoo-kernel
- Timestamp:
- Mar 4, 2015, 11:25:35 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_service_loader.c
r553 r604 29 29 30 30 #include "cgic.h" 31 31 #include "zoo_amqp.h" 32 #include "zoo_sql.h" 32 33 extern "C" 33 34 { … … 39 40 #include <libgen.h> 40 41 } 42 43 41 44 #include "ulinet.h" 42 45 #include "service_zcfg.h" … … 272 275 } 273 276 277 #include "zoo_json.h" 274 278 void 275 279 loadServiceAndRun (maps ** myMap, service * s1, map * request_inputs, … … 277 281 { 278 282 283 json_object *obj; 284 mapstojson(&obj,*myMap); 285 fprintf(stderr,"myMaps: %s\n",json_object_to_json_string(obj)); 286 287 288 json_object *obj3; 289 mapstojson(&obj3,*inputs); 290 fprintf(stderr,"inputs: %s\n",json_object_to_json_string(obj3)); 291 292 293 json_object *obj1; 294 maptojson(&obj1,request_inputs); 295 fprintf(stderr,"request_inputs: %s\n",json_object_to_json_string(obj1)); 296 297 json_object *obj2; 298 mapstojson(&obj2,*ioutputs); 299 fprintf(stderr,"ioutputs: %s\n",json_object_to_json_string(obj2)); 300 301 302 303 fflush(stderr); 304 279 305 280 306 char tmps1[1024]; … … 425 451 426 452 *eres = -1; 427 return;428 453 } 429 454 … … 681 706 { 682 707 683 684 708 struct cgi_env *cgi = *c; 685 709 #ifndef USE_GDB … … 694 718 map *r_inputs = NULL; 695 719 map *request_inputs = *inputs; 696 maps *m = NULL; 720 maps *m = (maps *) malloc (MAPS_SIZE); 721 722 if (m == NULL) 723 { 724 return errorException (m, _("Unable to allocate memory."), 725 "InternalError", NULL,request->out); 726 } 727 697 728 char *REQUEST = NULL; 729 map *store = getMap (request_inputs,"storeExecuteResponse"); 730 map *status = getMap (request_inputs, "status"); 731 732 /** 733 * 05-007r7 WPS 1.0.0 page 57 : 734 * 'If status="true" and storeExecuteResponse is "false" then the service 735 * shall raise an exception.' 736 */ 737 if (status != NULL && strcmp(status->value,"true") == 0 && store != NULL && strcmp (store->value, "false") == 0) 738 { 739 errorException (m, 740 _ 741 ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), 742 "InvalidParameterValue", "storeExecuteResponse",request->out); 743 //freeService (&s1); 744 //free (s1); 745 freeMaps (&m); 746 free (m); 747 free (REQUEST); 748 return 1; 749 } 750 751 752 753 /* 754 Si status =! NULLMAP le service est executé enbackground 755 */ 756 if (status != NULL) 757 if (strcasecmp (status->value, "false") == 0) 758 status = NULLMAP; 759 760 761 698 762 /** 699 763 * Parsing service specfic configuration file 700 764 */ 701 765 702 m = (maps *) malloc (MAPS_SIZE);703 if (m == NULL)704 {705 return errorException (m, _("Unable to allocate memory."),706 "InternalError", NULL,request->out);707 }708 766 709 767 char ntmp[1024]; … … 1091 1149 1092 1150 s1 = NULL; 1093 /**s1 = (service *) malloc (SERVICE_SIZE);1094 if (s1 == NULL)1095 {1096 freeMaps (&m);1097 free (m);1098 free (REQUEST);1099 free (SERVICE_URL);1100 return errorException (m, _("Unable to allocate memory."),1101 "InternalError", NULL);1102 }1103 r_inputs = getMap (request_inputs, "MetaPath");1104 if (r_inputs != NULL)1105 snprintf (tmps1, 1024, "%s/%s", ntmp, r_inputs->value);1106 else1107 snprintf (tmps1, 1024, "%s/", ntmp);1108 **/1109 1151 r_inputs = getMap (request_inputs, "Identifier"); 1110 /** 1111 char *ttmp = zStrdup (tmps1); 1112 snprintf (tmps1, 1024, "%s/%s.zcfg", ttmp, r_inputs->value); 1113 free (ttmp); 1114 #ifdef DEBUG 1115 fprintf (stderr, "Trying to load %s\n", tmps1); 1116 #endif 1117 if (strstr (r_inputs->value, ".") != NULL) 1118 { 1119 char *identifier = zStrdup (r_inputs->value); 1120 parseIdentifier (m, conf_dir, identifier, tmps1); 1121 map *tmpMap = getMapFromMaps (m, "lenv", "metapath"); 1122 if (tmpMap != NULL) 1123 addToMap (request_inputs, "metapath", tmpMap->value); 1124 free (identifier); 1125 } 1126 else 1127 setMapInMaps (m, "lenv", "Identifier", r_inputs->value); 1128 int saved_stdout = dup (fileno (stdout)); 1129 dup2 (fileno (stderr), fileno (stdout)); 1130 t = readServiceFile (m, tmps1, &s1, r_inputs->value); 1131 fflush (stdout); 1132 **/ 1133 //int saved_stdout = dup (fileno (stdout)); 1134 //dup2 (saved_stdout, fileno (stdout)); 1135 s1 = search_service (r_inputs->value); 1152 char * service_identifier = zStrdup (r_inputs->value); 1153 s1 = search_service (service_identifier); 1136 1154 if (s1 == NULL) 1137 1155 { … … 1369 1387 #endif 1370 1388 fflush (stderr); 1371 #ifdef DEBUG1372 fprintf (stderr, "***%s***\n", pToken);1373 #endif1374 1389 inputs_as_text[i] = 1375 1390 (char *) malloc ((strlen (pToken) + 1) * sizeof (char)); … … 1508 1523 #endif 1509 1524 addToMap (tmpmaps->content, tmpn1, tmpx2); 1525 1526 if (status == NULLMAP){ 1527 1510 1528 #ifndef WIN32 1511 if (CHECK_INET_HANDLE (hInternet))1512 #endif 1513 { 1529 if (CHECK_INET_HANDLE (hInternet)) 1530 #endif 1531 { 1514 1532 if (loadRemoteFile 1515 1533 (&m, &tmpmaps->content, &hInternet, tmpx2) < 0) … … 1525 1543 } 1526 1544 } 1545 } 1546 1527 1547 free (tmpx2); 1528 1548 addToMap (tmpmaps->content, "Reference", tmpv1 + 1); … … 1530 1550 tmpc = strtok (NULL, "@"); 1531 1551 } 1532 #ifdef DEBUG 1533 dumpMaps (tmpmaps); 1534 fflush (stderr); 1535 #endif 1552 1536 1553 if (request_input_real_format == NULL) 1537 1554 request_input_real_format = dupMaps (&tmpmaps); … … 1694 1711 * Header and Body gesture should be added here 1695 1712 */ 1696 #ifdef DEBUG 1697 fprintf (stderr, "REFERENCE\n"); 1698 #endif 1713 1699 1714 const char *refs[5] = 1700 1715 { "mimeType", "encoding", "schema", … … 1728 1743 && CHECK_INET_HANDLE (hInternet)) 1729 1744 { 1745 if (status == NULLMAP) 1730 1746 if (loadRemoteFile 1731 1747 (&m, … … 2882 2898 */ 2883 2899 r_inputs = NULL; 2884 map *store = getMap (request_inputs, 2885 "storeExecuteResponse"); 2886 map *status = getMap (request_inputs, "status"); 2887 /** 2888 * 05-007r7 WPS 1.0.0 page 57 : 2889 * 'If status="true" and storeExecuteResponse is "false" then the service 2890 * shall raise an exception.' 2891 */ 2892 if (status != NULL 2893 && 2894 strcmp 2895 (status->value, 2896 "true") == 0 && store != NULL && strcmp (store->value, "false") == 0) 2897 { 2898 errorException (m, 2899 _ 2900 ("Status cannot be set to true with storeExecuteResponse to false. Please, modify your request parameters."), 2901 "InvalidParameterValue", "storeExecuteResponse",request->out); 2902 //freeService (&s1); 2903 //free (s1); 2904 freeMaps (&m); 2905 free (m); 2906 freeMaps (&request_input_real_format); 2907 free (request_input_real_format); 2908 freeMaps (&request_output_real_format); 2909 free (request_output_real_format); 2910 free (REQUEST); 2911 free (SERVICE_URL); 2912 return 1; 2913 } 2914 r_inputs = getMap (request_inputs, "storeExecuteResponse"); 2900 //r_inputs = getMap (request_inputs, "storeExecuteResponse"); 2915 2901 int eres = SERVICE_STARTED; 2916 2902 int cpid = getpid (); … … 2936 2922 _tmpMaps->name = zStrdup ("lenv"); 2937 2923 char tmpBuff[100]; 2938 sprintf (tmpBuff, "%i", (cpid + (int) time (NULL))); 2939 _tmpMaps->content = createMap ("usid", tmpBuff); 2924 //sprintf (tmpBuff, "%i", (cpid + (int) time (NULL))); 2925 char *uuid = get_uuid(); 2926 _tmpMaps->content = createMap ("usid", uuid); 2940 2927 _tmpMaps->next = NULL; 2941 2928 sprintf (tmpBuff, "%i", cpid); … … 3051 3038 3052 3039 3053 char *fbkp, *fbkp1;3054 FILE *f0, *f1;3055 3040 if (status != NULL) 3056 3041 if (strcasecmp (status->value, "false") == 0) … … 3063 3048 &request_input_real_format, &request_output_real_format, &eres,request->out,request->err); 3064 3049 } 3065 else 3066 { 3067 int pid; 3068 #ifdef DEBUG 3069 fprintf (stderr, "\nPID : %d\n", cpid); 3070 #endif 3071 #ifndef WIN32 3072 pid = fork (); 3073 #else 3074 if (cgiSid == NULL) 3075 { 3076 createProcess (m, 3077 request_inputs, 3078 s1, 3079 NULL, 3080 cpid, 3081 request_input_real_format, 3082 request_output_real_format); 3083 pid = cpid; 3084 } 3085 else 3086 { 3087 pid = 0; 3088 cpid = atoi (cgiSid); 3089 } 3090 #endif 3091 if (pid > 0) 3092 { 3093 /** 3094 * dady : 3095 * set status to SERVICE_ACCEPTED 3096 */ 3097 #ifdef DEBUG 3098 fprintf (stderr, 3099 "father pid continue (origin %d) %d ...\n", 3100 cpid, getpid ()); 3101 #endif 3102 eres = SERVICE_ACCEPTED; 3103 } 3104 else if (pid == 0) 3105 { 3106 /** 3107 * son : have to close the stdout, stdin and stderr to let the parent 3108 * process answer to http client. 3109 */ 3110 r_inputs = getMapFromMaps (m, "main", "tmpPath"); 3111 map *r_inputs1 = getMap (s1->content, 3112 "ServiceProvider"); 3113 fbkp = (char *) 3114 malloc ((strlen 3115 (r_inputs->value) + 3116 strlen (r_inputs1->value) + 1024) * sizeof (char)); 3117 sprintf (fbkp, "%s/%s_%d.xml", r_inputs->value, 3118 r_inputs1->value, cpid); 3119 char *flog = 3120 (char *) 3121 malloc ((strlen (r_inputs->value) + 3122 strlen (r_inputs1->value) + 1024) * sizeof (char)); 3123 sprintf (flog, "%s/%s_%d_error.log", r_inputs->value, 3124 r_inputs1->value, cpid); 3125 #ifdef DEBUG 3126 fprintf (stderr, "RUN IN BACKGROUND MODE \n"); 3127 fprintf (stderr, 3128 "son pid continue (origin %d) %d ...\n", cpid, getpid ()); 3129 fprintf (stderr, "\nFILE TO STORE DATA %s\n", r_inputs->value); 3130 #endif 3131 freopen (flog, "w+", stderr); 3132 f0 = freopen (fbkp, "w+", stdout); 3133 #ifndef WIN32 3134 fclose (stdin); 3135 #endif 3136 free (flog); 3137 /** 3138 * set status to SERVICE_STARTED and flush stdout to ensure full 3139 * content was outputed (the file used to store the ResponseDocument). 3140 * The rewind stdout to restart writing from the bgining of the file, 3141 * this way the data will be updated at the end of the process run. 3142 */ 3143 printProcessResponse 3144 (m, request_inputs, 3145 cpid, s1, 3146 r_inputs1->value, 3147 SERVICE_STARTED, 3148 request_input_real_format, request_output_real_format,request->out); 3149 #ifndef WIN32 3150 fflush (stdout); 3151 rewind (stdout); 3152 #else 3153 #endif 3154 fbkp1 = (char *) 3155 malloc ((strlen 3156 (r_inputs->value) + 3157 strlen (r_inputs1->value) + 1024) * sizeof (char)); 3158 sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value, 3159 r_inputs1->value, cpid); 3160 f1 = freopen (fbkp1, "w+", stdout); 3161 loadServiceAndRun 3162 (&m, s1, 3163 request_inputs, 3164 &request_input_real_format, &request_output_real_format, &eres,request->out,request->err); 3165 } 3166 else 3167 { 3168 /** 3169 * error server don't accept the process need to output a valid 3170 * error response here !!! 3171 */ 3172 eres = -1; 3173 errorException (m, 3174 _ 3175 ("Unable to run the child process properly"), 3176 "InternalError", NULL,request->out); 3177 } 3178 } 3050 3051 /* processus en background */ 3052 3053 else 3054 { 3055 3056 eres = SERVICE_ACCEPTED; 3057 json_object * msg_jobj = json_object_new_object(); 3058 json_object_object_add(msg_jobj,"service_identifier",json_object_new_string(service_identifier)); 3059 3060 3061 json_object *maps_obj; 3062 mapstojson(&maps_obj,m); 3063 json_object_object_add(msg_jobj,"maps",maps_obj); 3064 3065 3066 json_object *req_format_jobj; 3067 mapstojson(&req_format_jobj,request_input_real_format); 3068 json_object_object_add(msg_jobj,"request_input_real_format",req_format_jobj); 3069 3070 json_object *req_jobj; 3071 maptojson(&req_jobj,request_inputs); 3072 json_object_object_add(msg_jobj,"request_inputs",req_jobj); 3073 3074 3075 json_object *outputs_jobj; 3076 mapstojson(&outputs_jobj,request_output_real_format); 3077 json_object_object_add(msg_jobj,"request_output_real_format",outputs_jobj); 3078 3079 if ( (send_msg(json_object_to_json_string(msg_jobj),"application/json") != 0) || (add_status(uuid) != 0) ){ 3080 eres = SERVICE_FAILED; 3081 } 3082 3083 } 3084 3085 free(uuid); 3086 3087 /* fin processus background */ 3088 3179 3089 3180 3090 #ifdef DEBUG … … 3201 3111 (void) signal (SIGABRT, donothing); 3202 3112 #endif 3203 if (((int) getpid ()) != cpid || cgi->cgiSid != NULL)3204 {3205 fclose (stdout);3206 fclose (stderr);3207 unhandleStatus (m);3208 /**3209 * Dump back the final file fbkp1 to fbkp3210 */3211 fclose (f0);3212 fclose (f1);3213 FILE *f2 = fopen (fbkp1, "rb");3214 FILE *f3 = fopen (fbkp, "wb+");3215 free (fbkp);3216 fseek (f2, 0, SEEK_END);3217 long flen = ftell (f2);3218 fseek (f2, 0, SEEK_SET);3219 char *tmps1 = (char *) malloc ((flen + 1) * sizeof (char));3220 fread (tmps1, flen, 1, f2);3221 fwrite (tmps1, 1, flen, f3);3222 fclose (f2);3223 fclose (f3);3224 unlink (fbkp1);3225 free (fbkp1);3226 free (tmps1);3227 }3228 3113 3229 3114 //freeService (&s1);
Note: See TracChangeset
for help on using the changeset viewer.