⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macedon-declares.c

📁 这是一个著名的应用层组播中间件的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    fprintf(hfile, "  void neighbor_clearit() {\n", temp->name);    //fprintf(hfile, "    bzero(this, sizeof(neighbor_%s));\n",temp->name);    fprintf(hfile, "    ipaddr = 0;\n");    fprintf(hfile, "    delay = 0.0;\n");    fprintf(hfile, "    time_last_heard = 0.0;\n");    temp_field = temp->fields;    while (temp_field)    {      /** FIXME: A DIRTY HACK -- unknown objects are not cleared at all. **/      if(!strcmp(temp_field->type, "int") || !strcmp(temp_field->type, "macedon_key"))        fprintf(hfile, "    %s = 0;\n", temp_field->name);      else if(!strcmp(temp_field->type, "float") || !strcmp(temp_field->type, "double"))        fprintf(hfile, "    %s = 0.0;\n", temp_field->name);      else if (temp_field->neighbor)        fprintf(hfile, "    %s.neighbor_clearit();\n", temp_field->name);      else if ( strchr(temp_field->type, '<') || strstr(temp_field->type, "mapping_type") ) {        fprintf(hfile, "    %s.clear();\n", temp_field->name);       }      else        ;      temp_field  = temp_field->next;    }    fprintf(hfile, "  }\n");    fprintf(hfile, "  void neighbor_copy(neighbor_%s *from) {\n", temp->name);     fprintf(hfile, "    ipaddr = from->ipaddr;\n");    fprintf(hfile, "    delay = from->delay;\n");    fprintf(hfile, "    time_last_heard = from->time_last_heard;\n");    temp_field = temp->fields;    while (temp_field)    {      fprintf(hfile, "    %s = from->%s;\n", temp_field->name, temp_field->name);      temp_field  = temp_field->next;    }    fprintf(hfile, "  }\n");    fprintf(hfile, "  int neighbor_serialize(char* buffer) {\n");    fprintf(hfile, "    int position = 0;\n");    fprintf(hfile, "    *((int*)(buffer+position)) = ipaddr;\n");    fprintf(hfile, "    position += sizeof(int);\n");    fprintf(hfile, "    *((double*)(buffer+position)) = delay;\n");    fprintf(hfile, "    position += sizeof(double);\n");    fprintf(hfile, "    *((double*)(buffer+position)) = time_last_heard;\n");    fprintf(hfile, "    position += sizeof(double);\n");    temp_field = temp->fields;    while (temp_field)    {      if(!strcmp(temp_field->size,"1")) {        if(temp_field->neighbor) {          fprintf(hfile, "    position += %s.serialize(buffer+position);\n",temp_field->name);        } else {          fprintf(hfile, "    *((%s*)(buffer+position)) = %s;\n",temp_field->type,temp_field->name);          fprintf(hfile, "    position += sizeof(%s);\n",temp_field->type);        }      } else {        fprintf(hfile,"    for(int k=0; k<%s; k++) {\n",temp_field->size);        if(temp_field->neighbor) {          fprintf(hfile, "      position += %s[k].serialize(buffer+position);\n",temp_field->name);        } else {          fprintf(hfile, "      *((%s*)(buffer+position)) = %s[k];\n",temp_field->type,temp_field->name);          fprintf(hfile, "      position += sizeof(%s);\n",temp_field->type);        }        fprintf(hfile,"    }\n");      }      temp_field  = temp_field->next;    }    fprintf(hfile, "    return position;\n");    fprintf(hfile, "  }\n");    fprintf(hfile, "  int neighbor_deserialize(char* buffer) {\n");    fprintf(hfile, "    int position = 0;\n");    fprintf(hfile, "    ipaddr = *((int*)(buffer+position));\n");    fprintf(hfile, "    position += sizeof(int);\n");    fprintf(hfile, "    delay = *((double*)(buffer+position));\n");    fprintf(hfile, "    position += sizeof(double);\n");    fprintf(hfile, "    time_last_heard = *((double*)(buffer+position));\n");    fprintf(hfile, "    position += sizeof(double);\n");    fprintf(hfile, "    //printf(\"deserialize: ipaddr=%%.8x delay=%%f time_last_heard=%%d\\n\",ipaddr,delay,time_last_heard);\n");    fprintf(hfile, "    //fflush(NULL);\n");    temp_field = temp->fields;    while (temp_field)    {      if(!strcmp(temp_field->size,"1")) {        if(temp_field->neighbor) {          fprintf(hfile, "    position += %s.deserialize(buffer+position);\n",temp_field->name);        } else {          fprintf(hfile, "    %s = *((%s*)(buffer+position));\n",temp_field->name,temp_field->type);          fprintf(hfile, "    position += sizeof(%s);\n",temp_field->type);        }      } else {        fprintf(hfile,"    for(int k=0; k<%s; k++) {\n",temp_field->size);        if(temp_field->neighbor) {          fprintf(hfile, "      position += %s[k].deserialize(buffer+position);\n",temp_field->name);        } else {          fprintf(hfile, "      %s[k] = *((%s*)(buffer+position));\n",temp_field->name,temp_field->type);          fprintf(hfile, "      position += sizeof(%s);\n",temp_field->type);        }        fprintf(hfile,"    }\n");      }      temp_field  = temp_field->next;    }    fprintf(hfile, "    return position;\n");    fprintf(hfile, "  }\n");    fprintf(hfile, "  neighbor_%s() {\n", temp->name);    fprintf(hfile, "    neighbor_clearit();\n");    /**      temp_field = temp->fields;      while (temp_field)      {      if(!strcmp(temp_field->type, "int") || !strcmp(temp_field->type, "double"))      fprintf(hfile, "    %s = 0;\n", temp_field->name);      else if (temp_field->neighbor)      fprintf(hfile, "    (&%s)->neighbor_clearit();\n", temp_field->name, temp_field->type);      else      ;  // do nothing      temp_field  = temp_field->next;      }     **/    fprintf(hfile, "  }\n");    fprintf(hfile, "};\n\n");    /**      fprintf(hfile, "class %s\n", temp->name);      fprintf(hfile, "{\n");      fprintf(hfile, "public:\n");      fprintf(hfile, "  %s() {\n", temp->name);            fprintf(hfile, "    int i;\n");      fprintf(hfile, "    for (i=0; i<%s; i++)\n", temp->size);      fprintf(hfile, "    {\n");      fprintf(hfile, "    }\n");      fprintf(hfile, "    neighbor_clearit();\n");      fprintf(hfile, "    maxsize = %s;\n", temp->size);            fprintf(hfile, "  }\n");      temp2 = my_neighbor_types;      while (temp2)      {      if (strcmp(temp->name, temp2->name))      {      fprintf(hfile, "  %s operator= (%s);\n", temp->name, temp2->name);      }      temp2 = temp2->next;      }      fprintf(hfile, "  neighbor_%s entries[%s];\n", temp->name, temp->size);       fprintf(hfile, "  neighbor_%s *neighbor_entry(int addr);\n",temp->name);       fprintf(hfile, "  neighbor_%s *neighbor_closest();\n", temp->name);       fprintf(hfile, "  void neighbor_clearit();\n", temp->name);       fprintf(hfile, "  int is_neighbor(int who);\n");      fprintf(hfile, "  neighbor_%s *rand_neighbor();\n", temp->name);      fprintf(hfile, "  int count;\n");      fprintf(hfile, "  int maxsize;\n");           fprintf(hfile, "};\n\n");     **/    temp = temp->next;  }  fprintf(hfile, "}\n\n"); // end of namespace block  /* the namespace can only be used after it is declared*/  fprintf(hfile, "using namespace %s_namespace;\n\n", protocol_name);  /*fprintf(hfile, "{ using namespace ::%s_namespace;\n\n", protocol_name);*/  /*fprintf(hfile, "namespace %s_namespace {\n\n", protocol_name);*/  /**    temp = my_neighbor_types;    fprintf(cfile, "namespace %s_namespace {\n\n", protocol_name);    while (temp)    {    temp2 = my_neighbor_types;    while (temp2)    {    if (strcmp(temp->name, temp2->name))    {    fprintf(cfile, "%s\n", temp->name);    fprintf(cfile, "%s::operator= (%s f)\n", temp->name, temp2->name);    fprintf(cfile, "{\n");    fprintf(cfile, "  int i;\n");    fprintf(cfile, "  for (i=0; i<f.count; i++)\n");    fprintf(cfile, "  {\n");    fprintf(cfile, "    (neighbor &)entries[i] = (neighbor &)f.entries[i];\n");    fprintf(cfile, "  }\n");    fprintf(cfile, "  count = f.count;\n");    fprintf(cfile, "}\n\n");    }    temp2 = temp2->next;    }    fprintf(cfile, "neighbor_%s *\n", temp->name);    fprintf(cfile, "%s::neighbor_entry(int addr)\n", temp->name);    fprintf(cfile, "{\n");    fprintf(cfile, "  int i;\n");    fprintf(cfile, "  for (i=0; i<count; i++)\n");    fprintf(cfile, "  {\n");    fprintf(cfile, "    if (entries[i].ipaddr == addr)\n");	    fprintf(cfile, "      return &(entries[i]);\n");    fprintf(cfile, "  }\n");    fprintf(cfile, "  return 0;\n");    fprintf(cfile, "}\n\n");    fprintf(cfile, "neighbor_%s *%s::rand_neighbor()\n", temp->name, temp->name);    fprintf(cfile, "{\n");    fprintf(cfile, "  int rand;\n");    fprintf(cfile, "  rand = random_integer(count);\n");    fprintf(cfile, "  return &(entries[rand]);\n");    fprintf(cfile, "}\n");    fprintf(cfile, "\n");    fprintf(cfile, "int %s::is_neighbor(int who)\n", temp->name);    fprintf(cfile, "{\n");    fprintf(cfile, "  int i;\n");    fprintf(cfile, "  int response = 0;\n");    fprintf(cfile, "  for (i=0; i<count; i++)\n");    fprintf(cfile, "    if (entries[i].ipaddr == who)\n");    fprintf(cfile, "      response = 1;\n");    fprintf(cfile, "  return response;\n");    fprintf(cfile, "}\n");    fprintf(cfile, "\n");    fprintf(cfile, "neighbor_%s *\n", temp->name);    fprintf(cfile, "%s::neighbor_closest()\n", temp->name);    fprintf(cfile, "{\n");    fprintf(cfile, "  int i;\n");    fprintf(cfile, "  neighbor_%s *closest=0;\n", temp->name);    fprintf(cfile, "  double closest_time=999999999;\n");    fprintf(cfile, "  for (i=0; i<count; i++)\n");    fprintf(cfile, "  {\n");    fprintf(cfile, "    if (entries[i].delay < closest_time)\n");	    fprintf(cfile, "    {\n");    fprintf(cfile, "      closest = &(entries[i]);\n");    fprintf(cfile, "      closest_time = entries[i].delay;\n");    fprintf(cfile, "    }\n");    fprintf(cfile, "  }\n");    fprintf(cfile, "  return closest;\n");    fprintf(cfile, "}\n\n");  fprintf(cfile, "void %s::neighbor_clearit()\n", temp->name, temp->name);  fprintf(cfile, "{\n");  fprintf(cfile, "  int i;\n");  fprintf(cfile, "  for (i=0; i<%s; i++)\n", temp->size);  fprintf(cfile, "  {\n");  fprintf(cfile, "    entries[i].ipaddr = 0;\n");	  fprintf(cfile, "    entries[i].delay = 0.0;\n");	  fprintf(cfile, "    entries[i].time_last_heard = 0.0;\n");	  tempntfields = temp->fields;  while (tempntfields)  {    if(!strcmp(tempntfields->type, "int") || !strcmp(tempntfields->type, "double"))      fprintf(cfile, "    entries[i].%s = 0;\n", tempntfields->name);    else if (tempntfields->neighbor)      fprintf(cfile, "    entries[i].%s.neighbor_clearit();\n", tempntfields->name);	    else	          ;  // do nothing    tempntfields = tempntfields->next;  }  fprintf(cfile, "  }\n");  fprintf(cfile, "  count=0;\n");  fprintf(cfile, "}\n\n");  temp = temp->next;}fprintf(cfile, "}\n\n");**/ }assoc_nbr_class( char *class){  if (my_periphs->neighbor == 0)  {    printf("Wierd error, outtie\n");    exit(43);  }  my_periphs->neighbor->class = malloc(400);  sprintf( my_periphs->neighbor->class, "%s", class);  return;}  add_failure(){  my_periphs->failure=1;  printf("Will do failure detection for neighbor %s\n", my_periphs->name);}add_periph (int noprint, char *field, char *type, int exported){  declare *new = (declare *) malloc (sizeof(declare));  neighbor_type *temp = my_neighbor_types;  new->neighbor = 0;  while(temp)  {    if (strcmp(temp->name, type)==0)      new->neighbor = temp;    temp = temp->next;  }  new->failure = 0;  new->type = (char *) malloc (400);  sprintf(new->type, "%s", type);  new->name = (char *) malloc (400);  sprintf(new->name, "%s", field);  new->size = (char *) malloc (400);  sprintf(new->size, "1");  new->exported = exported;  new->array = 0;  new->nodump = noprint;  new->next = my_periphs;  my_periphs = new;#ifdef GEN_TRACE  printf("Added periph %s %s %x %x\n", new->name, new->type, new, new->size);#endif  return;}add_periph_array (int noprint,char *field, char *type, char *size){  declare *new = (declare *) malloc (sizeof(declare));  neighbor_type *temp = my_neighbor_types;#ifdef GEN_TRACE  printf("Added periph array %s %s %s\n", field, type, size);#endif  new->neighbor = 0;  while(temp)  {    if (strcmp(temp->name, type)==0)      new->neighbor = temp;    temp = temp->next;  }  new->failure = 0;  new->type = (char *) malloc (400);  sprintf(new->type, type);  new->name = (char *) malloc (400);  sprintf(new->name, "%s", field);  new->size = (char *) malloc (400);  sprintf(new->size, "%s", size);  new->array = 1;  new->nodump = noprint;  new->next = my_periphs;  my_periphs = new;  return;}add_periph_array_2d (int noprint, char *field, char *type, char *size, char *size2){  declare *new = (declare *) malloc (sizeof(declare));  neighbor_type *temp = my_neighbor_types;#ifdef GEN_TRACE  printf("Added periph 2d array %s %s %s %s\n", field, type, size, size2);#endif  new->neighbor = 0;  while(temp)  {    if (strcmp(temp->name, type)==0)      new->neighbor = temp;    temp = temp->next;  }  new->failure = 0;  new->type = (char *) malloc (400);  sprintf(new->type, type);  new->name = (char *) malloc (400);  sprintf(new->name, "%s", field);  new->size = (char *) malloc (400);  sprintf(new->size, "%s", size);  new->size2 = (char *) malloc (400);  sprintf(new->size2, "%s", size2);    new->array = 2;  new->nodump = noprint;  new->next = my_periphs;  my_periphs = new;  return;}add_periph_array_3d (int noprint, char *field, char *type, char *size, char *size2, char *size3){  declare *new = (declare *) malloc (sizeof(declare));  neighbor_type *temp = my_neighbor_types;#ifdef GEN_TRACE  printf("Added periph 3d array %s %s %s %s %s\n", field, type, size, size2, size3);#endif  new->neighbor = 0;  while(temp)  {    if (strcmp(temp->name, type)==0)      new->neighbor = temp;    temp = temp->next;  }  new->failure = 0;  new->type = (char *) malloc (400);  sprintf(new->type, type);  new->name = (char *) malloc (400);  sprintf(new->name, "%s", field);  new->size = (char *) malloc (400);  sprintf(new->size, "%s", size);  new->size2 = (char *) malloc (400);  sprintf(new->size2, "%s", size2);  new->size3 = (char *) malloc (400);  sprintf(new->size3, "%s", size3);  new->array = 3;  new->nodump = noprint;  new->next = my_periphs;  my_periphs = new;  return;}add_timer(char *name, char *timeout){  timer *new = (timer *) malloc (sizeof (timer));  new->timer_name = (char *) malloc (400);  sprintf(new->timer_name, "timer_%s", name);  new->timer_timeout = (char *) malloc (400);  sprintf(new->timer_timeout, "%s", timeout);  new->timer_period = 1;  new->next = my_timers;  my_timers = new;#ifdef GEN_TRACE  printf("Added timer period %s %s\n", name, timeout);#endif}add_timer_nonperiod(char *name){  timer *new = (timer *) malloc (sizeof (timer));  new->timer_name = (char *) malloc (400);  sprintf(new->timer_name, "timer_%s", name);  new->timer_timeout = 0;  new->timer_period = 0;  new->next = my_timers;  my_timers = new;#ifdef GEN_TRACE  printf("Added timer nonperiod %s\n", name );#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -