Changeset 631


Ignore:
Timestamp:
Apr 13, 2015, 8:17:18 PM (9 years ago)
Author:
djay
Message:

Add readBase64 function, avoid calling it prior to fork . Add dumpMapsValuesToFiles function used to simplify OTB support.

Location:
trunk/zoo-project/zoo-kernel
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/zoo-project/zoo-kernel/request_parser.c

    r629 r631  
    902902                          addToMap (tmpmaps->content, "value",
    903903                                    (char *) tmp);
    904                           map *tmpv = getMap (tmpmaps->content, "value");
    905                           char *res = NULL;
    906                           char *curs = tmpv->value;
    907                           int i = 0;
    908                           for (i = 0; i <= strlen (tmpv->value) / 64;
    909                                i++)
    910                             {
    911                               if (res == NULL)
    912                                 res =
    913                                   (char *) malloc (67 * sizeof (char));
    914                               else
    915                                 res =
    916                                   (char *) realloc (res,
    917                                                     (((i + 1) * 65) +
    918                                                      i) * sizeof (char));
    919                               int csize = i * 65;
    920                               strncpy (res + csize, curs, 64);
    921                               if (i == xmlStrlen (tmp) / 64)
    922                                 strcat (res, "\n\0");
    923                               else
    924                                 {
    925                                   strncpy (res + (((i + 1) * 64) + i),
    926                                            "\n\0", 2);
    927                                   curs += 64;
    928                                 }
    929                             }
    930                           free (tmpv->value);
    931                           tmpv->value = zStrdup (res);
    932                           free (res);
    933904                          xmlFree (tmp);
    934905                        }
     
    937908                }
    938909              cur2 = cur2->next;
     910              while (cur2 != NULL && cur2->type != XML_ELEMENT_NODE){
     911                cur2 = cur2->next;
     912              }
    939913            }
    940 
    941914          {
    942915            maps *testPresence =
  • trunk/zoo-project/zoo-kernel/service.h

    r627 r631  
    10581058        fprintf(stderr,"%s = %s\n",tmpV[i],tmpVI->value);
    10591059#endif
    1060         if(i<7)
    1061           setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
    1062         else
    1063           if(strncasecmp(tmpV[7],"mimeType",8)==0)
    1064             setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
     1060        setMapArray(_cursor->content,tmpV[i],len,tmpVI->value);
    10651061      }
    10661062    }
  • trunk/zoo-project/zoo-kernel/service_internal.c

    r630 r631  
    29482948}
    29492949
     2950/**
     2951 * Write a file from value and length
     2952 *
     2953 * @param fname the file name
     2954 * @param val the value
     2955 * @param length the value length
     2956 */
     2957int writeFile(char* fname,char* val,int length){
     2958  FILE* of=fopen(fname,"wb");
     2959  if(of==NULL){
     2960    return -1;
     2961  }
     2962  size_t ret=fwrite(val,sizeof(char),length,of);
     2963  if(ret<length){
     2964    fprintf(stderr,"Write error occured!\n");
     2965    fclose(of);
     2966    return -1;
     2967  }
     2968  fclose(of);
     2969  return 1;
     2970}
     2971
     2972/**
     2973 * Dump all values in a maps as files
     2974 *
     2975 * @param main_conf the maps containing the settings of the main.cfg file
     2976 * @param in the maps containing values to dump as files
     2977 */
     2978void dumpMapsValuesToFiles(maps** main_conf,maps** in){
     2979  map* tmpPath=getMapFromMaps(*main_conf,"main","tmpPath");
     2980  map* tmpSid=getMapFromMaps(*main_conf,"lenv","sid");
     2981  maps* inputs=*in;
     2982  int length=0;
     2983  while(inputs!=NULL){
     2984    if(getMap(inputs->content,"mimeType")!=NULL &&
     2985       getMap(inputs->content,"cache_file")==NULL){
     2986      map* cMap=inputs->content;
     2987      if(getMap(cMap,"length")!=NULL){
     2988        map* tmpLength=getMap(cMap,"length");
     2989        int len=atoi(tmpLength->value);
     2990        int k=0;
     2991        for(k=0;k<len;k++){
     2992          map* cMimeType=getMapArray(cMap,"mimeType",k);
     2993          map* cValue=getMapArray(cMap,"value",k);
     2994          map* cSize=getMapArray(cMap,"size",k);
     2995          char file_ext[32];
     2996          getFileExtension(cMimeType != NULL ? cMimeType->value : NULL, file_ext, 32);
     2997          char* val=(char*)malloc((strlen(tmpPath->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
     2998          sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,inputs->name,tmpSid->value,k,file_ext);
     2999          length=0;
     3000          if(cSize!=NULL){
     3001            length=atoi(cSize->value);
     3002          }
     3003          writeFile(val,cValue->value,length);
     3004          setMapArray(cMap,"cache_file",k,val);
     3005          free(val);
     3006        }
     3007      }else{
     3008        int length=0;
     3009        map* cMimeType=getMap(cMap,"mimeType");
     3010        map* cValue=getMap(cMap,"value");
     3011        map* cSize=getMap(cMap,"size");
     3012        char file_ext[32];
     3013        getFileExtension(cMimeType != NULL ? cMimeType->value : NULL, file_ext, 32);
     3014        char *val=(char*)malloc((strlen(tmpPath->value)+strlen(inputs->name)+strlen(tmpSid->value)+strlen(file_ext)+16)*sizeof(char));
     3015        sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,inputs->name,tmpSid->value,0,file_ext);
     3016        if(cSize!=NULL){
     3017          length=atoi(cSize->value);
     3018        }
     3019        writeFile(val,cValue->value,length);
     3020        addToMap(cMap,"cache_file",val);
     3021        free(val);
     3022      }
     3023    }
     3024    inputs=inputs->next;
     3025  }
     3026}
    29503027
    29513028/**
     
    30083085
    30093086/**
     3087 * Read Base64 value and split it value by lines of 64 char.
     3088 *
     3089 * @param in the map containing the value to split
     3090 */
     3091void readBase64(map **in){
     3092  char *res = NULL;
     3093  char *curs = (*in)->value;
     3094  int i = 0;
     3095  for (i = 0; i <= strlen ((*in)->value) / 64;
     3096       i++)
     3097    {
     3098      if (res == NULL)
     3099        res =
     3100          (char *) malloc (65 * sizeof (char));
     3101      else
     3102        res =
     3103          (char *) realloc (res,
     3104                            (((i + 1) * 65) +
     3105                             i) * sizeof (char));
     3106      int csize = i * 65;
     3107      strncpy (res + csize, curs, 64);
     3108      if (i == strlen ((*in)->value) / 64)
     3109        strcat (res, "\n\0");
     3110      else
     3111        {
     3112          strncpy (res + (((i + 1) * 64) + i),
     3113                   "\n\0", 2);
     3114          curs += 64;
     3115        }
     3116    }
     3117  free ((*in)->value);
     3118  (*in)->value = zStrdup (res);
     3119  free (res);
     3120}
     3121
     3122/**
    30103123 * Make sure that each value encoded in base64 in a maps is decoded.
    30113124 *
    30123125 * @param in the maps containing the values
     3126 * @see readBase64
    30133127 */
    30143128void ensureDecodedBase64(maps **in){
     
    30183132    if(tmp!=NULL && strncasecmp(tmp->value,"base64",6)==0){
    30193133      tmp=getMap(cursor->content,"value");
     3134      readBase64(&tmp);
    30203135      addToMap(cursor->content,"base64_value",tmp->value);
    30213136      int size=0;
     
    30373152          sprintf(key,"base64_value_%d",i);
    30383153          tmp=getMapArray(cursor->content,"value",i);
     3154          readBase64(&tmp);
    30393155          addToMap(cursor->content,key,tmp->value);
    30403156          int size=0;
     
    41224238#endif
    41234239}
     4240
  • trunk/zoo-project/zoo-kernel/service_internal.h

    r621 r631  
    202202  void outputResponse(service*,maps*,maps*,map*,int,maps*,int);
    203203
     204  void dumpMapsValuesToFiles(maps**,maps**);
     205
    204206  char *base64(const char*,int);
    205207  char *base64d(const char*,int,int*);
  • trunk/zoo-project/zoo-kernel/service_internal_otb.c

    r619 r631  
    9898
    9999/**
    100  * Write a file from value and length
    101  *
    102  * @param fname the file name
    103  * @param val the value
    104  * @param length the value length
    105  */
    106 int writeFile(char* fname,char* val,int length){
    107   FILE* of=fopen(fname,"wb");
    108   if(of==NULL){
    109     return -1;
    110   }
    111   size_t ret=fwrite(val,sizeof(char),length,of);
    112   if(ret<length){
    113     fprintf(stderr,"Write error occured!\n");
    114     fclose(of);
    115     return -1;
    116   }
    117   fclose(of);
    118   return 1;
    119 }
    120 
    121 /**
    122100 * Load and run an OTB Application corresponding to the service by using inputs parameters.
    123101 * Define the m_Conf
     
    137115  map* tmp=NULL;
    138116  int res=-1;
    139 
    140117  std::vector<std::string> list = ApplicationRegistry::GetAvailableApplications();
    141118  if (list.size() == 0){
     
    146123    free(tmps);
    147124    res=-1;
     125    return res;
    148126  }
    149127  else{
     128    dumpMapsValuesToFiles(main_conf,real_inputs);
    150129    for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it){
    151130      if(s->name==*it){
     
    212191                    setMapInMaps(inputs,paramKey.c_str(),"generated_file",tmp);
    213192                    dynamic_cast<OutputImageParameter *> (param.GetPointer())->SetPixelType(outPixType);
    214                   }else{
    215                     map* tmpPath=getMapFromMaps(m,"main","tmpPath");
    216                     map* tmpSid=getMapFromMaps(m,"lenv","sid");
    217                     map* tmpVal=getMapFromMaps(inputs,paramKey.c_str(),"mimeType");
    218                     char file_ext[32];
    219                     getFileExtension(tmpVal != NULL ? tmpVal->value : NULL, file_ext, 32);
    220                     if(type == ParameterType_InputImageList){
    221                       char *val=(char*)malloc((strlen(tmpPath->value)+strlen(s->name)+strlen(tmpSid->value)+strlen(file_ext)+13)*sizeof(char));
    222                       sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,s->name,tmpSid->value,0,file_ext);
    223                       int length=0;
    224                       map* tmpSize=getMap(test,"size");
    225                       if(tmpSize!=NULL){
    226                         length=atoi(tmpSize->value);
    227                       }
    228                       writeFile(val,test->value,length);
    229                       values.push_back(val);
    230                       free(val);
    231                       map* tmpLength=getMapFromMaps(inputs,paramKey.c_str(),"length");
    232                       if(tmpLength!=NULL){
    233                         int len=atoi(tmpLength->value);
    234                         maps* tmpI=getMaps(inputs,paramKey.c_str());
    235                         for(int k=1;k<len;k++){
    236                           val=(char*)malloc((strlen(tmpPath->value)+strlen(s->name)+strlen(tmpSid->value)+strlen(file_ext)+13)*sizeof(char));
    237                           sprintf(val,"%s/Input_%s_%s_%d.%s",tmpPath->value,s->name,tmpSid->value,k,file_ext);
    238                           length=0;
    239                           map* tmpV=getMapArray(tmpI->content,"value",k);
    240                           tmpSize=getMapArray(tmpI->content,"size",k);
    241                           if(tmpSize!=NULL){
    242                             length=atoi(tmpSize->value);
    243                           }
    244                           writeFile(val,tmpV->value,length);
    245                           values.push_back(val);
    246                           free(val);
    247                         }
    248                       }
    249                       dynamic_cast<InputImageListParameter *> (param.GetPointer())->SetListFromFileName(values);
    250                     }
    251                     else
    252                       if(type == ParameterType_InputVectorData || type == ParameterType_InputImage
    253                            || type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData
    254                            || type == ParameterType_InputFilename){
    255                         char tmp[1024];
    256                         char* ext="json";
    257                         if(tmpVal!=NULL){
    258                           char *val=(char*)malloc((strlen(tmpPath->value)+strlen(s->name)+strlen(tmpSid->value)+strlen(file_ext)+10)*sizeof(char));
    259                           sprintf(val,"%s/Input_%s_%s.%s",tmpPath->value,s->name,tmpSid->value,file_ext);
    260                           int length=0;
    261                           map* tmpSize=getMap(test,"size");
    262                           if(tmpSize!=NULL){
    263                             length=atoi(tmpSize->value);
    264                           }
    265                           writeFile(val,test->value,length);
    266 
    267                           if(strncasecmp(tmpVal->value,"application/zip",14)==0){
    268 
    269                             char tmpName[1024];
    270                             sprintf(tmpName,"/vsizip/%s",val);
    271                             char **files=VSIReadDir(tmpName);
    272                             int nFiles = CSLCount( files );
    273                             char tmpSSName[1024];
    274                             sprintf(tmpSSName,"%s/Input_%s_%s",tmpPath->value,s->name,tmpSid->value);
    275                             mkdir(tmpSSName,0777);
    276                            
    277                             char tmpSName[1024];
    278                             for(int kk=0;kk<nFiles;kk++){
    279                               sprintf(tmpSName,"%s/%s",tmpName,files[kk]);
    280                               VSILFILE* fmain=VSIFOpenL(tmpSName, "rb");
    281                               if(fmain!=NULL){
    282                                 VSIFSeekL(fmain,0,SEEK_END);
    283                                 long count=VSIFTellL(fmain);
    284                                 VSIRewindL(fmain);
    285                                
    286                                 char *content=(char*) malloc((count+1)*sizeof(char)); 
    287                                 VSIFReadL(content,1,count*sizeof(char),fmain);
    288                                
    289                                 char tmpSSSName[1024];
    290                                 sprintf(tmpSSSName,"%s/%s",tmpSSName,files[kk]);
    291                                
    292                                 FILE* fx=fopen(tmpSSSName, "wb");
    293                                 fwrite(content,1,count,fx);
    294                                 fclose(fx);
    295                                 VSIFCloseL(fmain);
    296                                 free(content);
    297                                 std::string test1(tmpSSSName);
    298                                 if(test1.find(".shp")!=std::string::npos){
    299                                   setMapInMaps(inputs,paramKey.c_str(),"cache_file",tmpSSSName);
    300                                   test=getMapFromMaps(inputs,paramKey.c_str(),"cache_file");
    301                                 }
    302                               }
    303                             }
    304                             m_Application->SetParameterString(paramKey, test->value);
    305                           }else{
    306                             m_Application->SetParameterString(paramKey, val);
    307                           }
    308                           free(val);
    309                         }
    310                       }
    311                       else
    312                         if(test->value!=NULL)
    313                           m_Application->SetParameterString(paramKey, test->value);
    314                   }
    315 
     193                  }
     194                  else{
     195                    if(test!=NULL && test->value!=NULL)
     196                      m_Application->SetParameterString(paramKey, test->value);
     197                  }
    316198                }else{
    317199                  if(type == ParameterType_OutputVectorData){
  • trunk/zoo-project/zoo-kernel/service_internal_ruby.c

    r581 r631  
    3232/**
    3333 * Load a Ruby file then run the function corresponding to the service by
    34  * passing the conf, inputs and outputs parameters by value as JavaScript
    35  * Objects.
     34 * passing the conf, inputs and outputs parameters by refernce as Ruby Hash.
    3635 *
    3736 * @param main_conf the conf maps containing the main.cfg settings
  • trunk/zoo-project/zoo-kernel/zoo_service_loader.c

    r621 r631  
    19681968          if (lid < 0)
    19691969            {
    1970               fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
    1971               fflush (stderr);
    1972               return -1;
     1970              return errorException (m, _("Lock failed"),
     1971                              "InternalError", NULL);
    19731972            }
    19741973          else
     
    19761975              if (lockShm (lid) < 0)
    19771976                {
    1978                   fprintf (stderr, "ERROR %s %d\n", __FILE__, __LINE__);
    1979                   fflush (stderr);
    1980                   return -1;
     1977                  return errorException (m, _("Lock failed"),
     1978                                         "InternalError", NULL);
    19811979                }
    1982               fflush (stderr);
    19831980            }
    19841981          f0 = freopen (fbkp, "w+", stdout);
     
    19871984          fclose (stdin);
    19881985#endif
     1986
     1987          /**
     1988           * set status to SERVICE_STARTED and flush stdout to ensure full
     1989           * content was outputed (the file used to store the ResponseDocument).
     1990           * The rewind stdout to restart writing from the bgining of the file,
     1991           * this way the data will be updated at the end of the process run.
     1992           */
     1993          printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
     1994                                SERVICE_STARTED, request_input_real_format,
     1995                                request_output_real_format);
     1996          fflush (stdout);
     1997          unlockShm (lid);
     1998          fflush (stderr);
     1999          fbkp1 =
     2000            (char *)
     2001            malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
     2002                     1024) * sizeof (char));
     2003          sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
     2004                   r_inputs1->value, cpid);
     2005
     2006          f1 = freopen (fbkp1, "w+", stdout);
    19892007          free (flog);
     2008
    19902009          if(validateRequest(&m,s1,request_inputs, &request_input_real_format,&request_output_real_format,&hInternet)<0){
    19912010            freeService (&s1);
     
    20062025            return -1;
    20072026          }
    2008       /**
    2009        * set status to SERVICE_STARTED and flush stdout to ensure full
    2010        * content was outputed (the file used to store the ResponseDocument).
    2011        * The rewind stdout to restart writing from the bgining of the file,
    2012        * this way the data will be updated at the end of the process run.
    2013        */
    2014           printProcessResponse (m, request_inputs, cpid, s1, r_inputs1->value,
    2015                                 SERVICE_STARTED, request_input_real_format,
    2016                                 request_output_real_format);
    2017           fflush (stdout);
    2018           unlockShm (lid);
    2019           fflush (stderr);
    2020           fbkp1 =
    2021             (char *)
    2022             malloc ((strlen (r_inputs->value) + strlen (r_inputs1->value) +
    2023                      1024) * sizeof (char));
    2024           sprintf (fbkp1, "%s/%s_final_%d.xml", r_inputs->value,
    2025                    r_inputs1->value, cpid);
    2026           f1 = freopen (fbkp1, "w+", stdout);
    20272027          loadServiceAndRun (&m, s1, request_inputs,
    20282028                             &request_input_real_format,
    20292029                             &request_output_real_format, &eres);
     2030
    20302031        }
    20312032      else
Note: See TracChangeset for help on using the changeset viewer.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png