Ignore:
Timestamp:
May 7, 2019, 2:17:08 PM (3 years ago)
Author:
djay
Message:

Merge prototype-v0 branch in trunk

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/zoo-project/zoo-kernel/service_internal_mono.c

    r794 r917  
    3030#include "response_print.h"
    3131
     32MonoClass *iclasses[4];
     33MonoMethod *imethods[10];
     34
    3235/**
    3336 * Load a Mono dll then run the static public method corresponding to the
     
    6972    mono_config_parse(NULL);
    7073
    71   MonoDomain* monoDomain = mono_jit_init_version("ZOO_Embedded_Domain",
    72                                                  "v4.0.30319");
    73 
    74   map* cwdMap=getMapFromMaps(*main_conf,"main","servicePath");
    75   if(cwdMap==NULL)
    76     cwdMap=getMapFromMaps(*main_conf,"lenv","cwd");
    77  
    78   char *zooAssembly=(char*)malloc((strlen(cwdMap->value)+11)*sizeof(char));
    79   sprintf(zooAssembly,"%s/ZMaps.dll",cwdMap->value);
    80   assembly = mono_domain_assembly_open(monoDomain,zooAssembly);
    81   if(assembly==NULL){
    82     setMapInMaps(*main_conf,"lenv","message",_("The ZMaps.dll assembly cannot be found!"));
    83     return 4;
    84   }
    85   mono_add_internal_call ("ZOO_API::Translate", MonoTranslate);
    86   mono_add_internal_call ("ZOO_API::UpdateStatus", MonoUpdateStatus);
    87 
     74  MonoDomain* monoDomain;
     75  map* mono;
     76  if (hasvalue(*main_conf, "mono", "version", &mono)) {   
     77          monoDomain = mono_jit_init_version("ZOO_Embedded_Domain", mono->value);
     78  }
     79  else {         
     80          monoDomain = mono_jit_init("ZOO_Embedded_Domain"); // use default framework version
     81  }
     82
     83  //MonoDomain* monoDomain = mono_jit_init_version("ZOO_Embedded_Domain", "v4.0.30319");
     84  //MonoDomain* monoDomain = mono_jit_init("ZOO_Embedded_Domain"); 
     85
     86  char* ZMapsLib = "ZMaps.dll";
     87  char *zooAssembly = NULL;
     88 
     89  map* config = NULL, *basedir = NULL;
     90  if (hasvalue(*main_conf, "mono", "configdir", &basedir) && // knut: rename to e.g. "base_dir"?
     91          hasvalue(*main_conf, "mono", "configfile", &config)) {         
     92          mono_domain_set_config(monoDomain, basedir->value, config->value);
     93          zooAssembly = file_exists(basedir->value, ZMapsLib);
     94  }
     95  else {         
     96          mono_domain_set_config(monoDomain, "./", "");
     97  }   
     98
     99  map* cwdMap;
     100  if (! hasvalue(*main_conf, "main", "servicePath", &cwdMap) &&
     101          ! hasvalue(*main_conf, "main", "libPath", &cwdMap) ) {
     102          cwdMap = getMapFromMaps(*main_conf, "lenv", "cwd");
     103  } 
     104
     105  if (zooAssembly != NULL || (zooAssembly = file_exists(cwdMap->value, ZMapsLib)) != NULL) {
     106          assembly = mono_domain_assembly_open(monoDomain, zooAssembly);
     107  }
     108  else {
     109          // try to load from default locations (GAC?)
     110          assembly = mono_domain_assembly_open(monoDomain, ZMapsLib);
     111  } 
     112  free(zooAssembly);
     113
     114  if (assembly == NULL) {
     115          setMapInMaps(*main_conf, "lenv", "message", _("The ZMaps.dll assembly cannot be found!"));
     116          return 4;
     117  }
     118
     119  mono_add_internal_call ("ZOO_API::Translate", MonoTranslate);   
     120  mono_add_internal_call("ZOO_API::UpdateStatus", MonoUpdateStatus);
     121 
    88122  monoImage = mono_assembly_get_image(assembly);
    89123  MonoClass *KeysList = mono_class_from_name(monoImage, "ZooGenerics", "KeysList");
     
    104138  MonoClass *classes[3]={_ZMapsClass,ZMapsClass,ZMapClass};
    105139  MonoMethod *methods[5]={ZMapsAdd,_ZMapsSetContent,_ZMapsSetChild,ZMapAdd,ZMapDisplay};
    106 
     140 
    107141  MonoMethod *ZMapsGetKeys = mono_class_get_method_from_name(ZMapsClass, "getKeys", 0);
     142 
    108143  MonoMethod *ZMapsGetKeysCount = mono_class_get_method_from_name(ZMapsClass, "getKeysCount", 0);
    109144  MonoMethod *ZMapsGetKey = mono_class_get_method_from_name(ZMapsClass, "getKey", 1);
     
    116151  MonoClass *oclasses[4]={_ZMapsClass,ZMapsClass,ZMapClass,KeysList};
    117152  MonoMethod *omethods[10]={ZMapsGetMaps,ZMapsGetKeysCount,ZMapsGetKey,ZMapGetMap,ZMapGetKeysCount,ZMapGetKey,_ZMapsGetContent,_ZMapsGetChild,ZMapGetSize,ZMapGetMapAsBytes};
    118 
     153 
    119154  for(int i=0;i<4;i++){
    120155    iclasses[i]=oclasses[i];
     
    131166
    132167  MonoObject * out=ZMapsFromMaps(monoDomain,classes,methods,&exc,outputs);
    133 
     168 
    134169  args [0] = main;
    135170  args [1] = in;
    136171  args [2] = out;
    137172
    138   tmp=getMap(s->content,"serviceProvider");
    139   char *sName=(char*)malloc((strlen(tmp->value)+strlen(cwdMap->value)+2)*sizeof(char));
    140   sprintf(sName,"%s/%s",cwdMap->value,tmp->value);
    141   MonoAssembly* serviceAssembly = mono_domain_assembly_open(monoDomain,sName);
     173  char* sName = NULL;
     174  MonoAssembly* serviceAssembly = NULL;
     175 
     176  tmp = getMap(s->content, "serviceProvider"); 
     177  char* test = file_exists(NULL, "log.txt");
     178  if ( (sName = file_exists(cwdMap->value, tmp->value)) != NULL ||
     179           (sName = file_exists(".", tmp->value)) != NULL ||  // in case servicePath is defined but serviceProvider is in working directory
     180           (basedir != NULL && (sName = file_exists(basedir->value, tmp->value)) != NULL) ) {
     181
     182           serviceAssembly = mono_domain_assembly_open(monoDomain, sName);
     183  }
     184  else {
     185          // try to open from default locations   
     186          serviceAssembly = mono_domain_assembly_open(monoDomain, tmp->value);
     187  }
     188  free(sName);
     189
     190  //char *sName=(char*)malloc((strlen(tmp->value)+strlen(cwdMap->value)+2)*sizeof(char));
     191  //sprintf(sName,"%s/%s",cwdMap->value,tmp->value);
     192  //MonoAssembly* serviceAssembly = mono_domain_assembly_open(monoDomain,sName);
     193 
    142194  if(serviceAssembly==NULL){
    143195    char *msg=(char*)malloc((strlen(_("Your service assembly: %s cannot be found!"))+strlen(tmp->value)+1)*sizeof(char));
     
    147199    return 4;
    148200  }
    149 
     201 
    150202  MonoImage* serviceImage = mono_assembly_get_image(serviceAssembly);
    151203
    152204  map* tmp1=getMap(s->content,"serviceNameSpace");
     205 
    153206  tmp=getMap(s->content,"serviceClass");
    154207  MonoClass *serviceClass = mono_class_from_name(serviceImage, tmp1->value, tmp->value);
     
    160213    return 4;
    161214  }
     215 
    162216  MonoMethod *serviceFunction = mono_class_get_method_from_name(serviceClass, s->name, 3);
    163217  if(serviceFunction==NULL){
     
    168222    return 4;
    169223  }
     224 
    170225  MonoObject *exception=NULL;
    171226  MonoObject *result = mono_runtime_invoke(serviceFunction,NULL,args,&exception);
     227 
     228  if (exception != NULL) { // knut: add exception handling
     229          MonoString* pMsg = mono_object_to_string(exception, NULL);     
     230          setErrorMessage(*main_conf, s->name, NoApplicableCode, mono_string_to_utf8(pMsg));
     231          return 4;
     232  }
     233
    172234  res=*(int*)mono_object_unbox (result);
     235 
    173236  if(res==3){
    174237    freeMaps(&outputs);
    175238    free(outputs);
    176     outputs=mapsFromZMaps(monoDomain,oclasses,omethods,args[2]);
     239    outputs=mapsFromZMaps(monoDomain,oclasses,omethods,(MonoObject*)args[2]);
    177240    *real_outputs=outputs;
    178241  }
     242 
    179243  freeMaps(&m);
    180244  free(m);
    181   m=mapsFromZMaps(monoDomain,oclasses,omethods,args[0]);
     245  m=mapsFromZMaps(monoDomain,oclasses,omethods,(MonoObject*)args[0]);
    182246  *main_conf=m;
    183   mono_jit_cleanup (monoDomain);
     247  mono_jit_cleanup (monoDomain); 
    184248  return res;
    185249}
     
    288352      values[0]=&i;
    289353      exc=NULL;
    290       MonoObject *okey = (MonoString*) mono_runtime_invoke(methods[2],m,values,&exc);
     354      MonoString *okey = (MonoString*) mono_runtime_invoke(methods[2],m,values,&exc);
    291355      char* key = mono_string_to_utf8(okey);
    292356      maps* tmp=createMaps(key);
    293357      values[0]=mono_string_new (domain, key);
    294       MonoObject *content_ZMaps = (MonoString*) mono_runtime_invoke(methods[0],m,values,&exc);
    295       MonoObject *content = mono_runtime_invoke(methods[6],content_ZMaps,NULL,&exc);
     358      MonoString *content_ZMaps = (MonoString*) mono_runtime_invoke(methods[0],m,values,&exc);
     359      MonoObject *content = mono_runtime_invoke(methods[6],(MonoObject*)content_ZMaps,NULL,&exc);
    296360      tmp->content=mapFromZMap(domain,classes,methods,content);
    297361      MonoObject *childs = mono_runtime_invoke(methods[7],content_ZMaps,NULL,&exc);
     
    329393      values[0]=&i;
    330394      exc=NULL;
    331       MonoObject *okey = (MonoString*) mono_runtime_invoke(methods[5],m,values,&exc);
     395      MonoString *okey = (MonoString*) mono_runtime_invoke(methods[5],m,values,&exc);
    332396      char* key = mono_string_to_utf8(okey);
    333397      values[0]=mono_string_new (domain, key);
    334398      exc=NULL;
    335       MonoObject *ovalue = (MonoString*) mono_runtime_invoke(methods[3],m,values,&exc);
     399      MonoString *ovalue = (MonoString*) mono_runtime_invoke(methods[3],m,values,&exc);
    336400      char* value = mono_string_to_utf8(ovalue);
    337401      if(res==NULL)
     
    350414 */
    351415static MonoString* MonoTranslate(MonoString *str){
    352   return mono_string_new (mono_domain_get(), mono_string_to_utf8(_(str)));
     416  return mono_string_new (mono_domain_get(), _(mono_string_to_utf8(str)));
    353417}
    354418
     
    360424 * @return the translated string
    361425 */
    362 static void MonoUpdateStatus(MonoObject* obj,MonoString *str,int* p){
    363   MonoDomain* domain = mono_object_get_domain (obj);
    364   maps *confMaps=mapsFromZMaps(domain,iclasses,imethods,obj);
    365   setMapInMaps(confMaps,"lenv","message",mono_string_to_utf8(str));
    366   int tmp=p;
    367   if (p < 0 || p > 100){
    368     tmp=-1;
    369   }
    370   char tmpStatus[5];
    371   snprintf(tmpStatus, 4, "%d", tmp);
    372   setMapInMaps(confMaps,"lenv","status",(char*)tmpStatus);
    373   _updateStatus(confMaps);
    374   freeMaps(&confMaps);
    375   free(confMaps);
    376   return;
    377 }
     426static void MonoUpdateStatus(MonoObject* obj, MonoString *str, int p) {
     427        MonoDomain* domain = mono_object_get_domain(obj);       
     428        maps *confMaps = mapsFromZMaps(domain, iclasses, imethods, obj);       
     429        setMapInMaps(confMaps, "lenv", "message", mono_string_to_utf8(str));   
     430        int tmp = p > 100 || p < 0 ? -1 : p;   
     431        char tmpStatus[5];
     432        snprintf(tmpStatus, 4, "%d", tmp);     
     433        setMapInMaps(confMaps, "lenv", "status", (char*)tmpStatus);     
     434        _updateStatus(confMaps);       
     435        freeMaps(&confMaps);   
     436        free(confMaps);
     437        return;
     438}
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