📄 macedon-declares.c
字号:
//Copyright (c) 2004, Charles Killian, Adolfo Rodriguez, Dejan Kostic, Sooraj Bhat, and Amin Vahdat//All rights reserved.////Redistribution and use in source and binary forms, with or without//modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright// notice, this list of conditions and the following disclaimer in// the documentation and/or other materials provided with the// distribution.// * Neither the names of Duke University nor The University of// California, San Diego, nor the names of its contributors// may be used to endorse or promote products derived from// this software without specific prior written permission.////THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"//AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE//USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./* macedon-neighbor-types.c *//* * For MACEDON code generation * * Adolfo Rodriguez *//* includes */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "macedon-conv.h"#include "macedon-conv-utils.h"extern FILE *yyin, *yyout;/* imports */extern int protocol_done;extern char *message_str, transition_state_str, transition_stimulus_str;extern char *protocol_name;extern char *base_name;extern FILE *hfile, *cfile, *cfilefuncs, *hfilemacros, *cfilemacros;extern node_type *my_node_types;extern message *my_messages;extern transport *my_transports;extern neighbor_type *my_neighbor_types;extern declare *my_periphs;extern declare *my_locals;extern state *my_states;extern timer *my_timers;extern constant *my_constants;extern object_replace *my_replaces;extern int foreachcount;extern int tracing;begin_constants(){ fprintf(hfile, "namespace %s_namespace {\n\n", protocol_name);}add_constant(char *name, char * value, char * type){ char * temp = (char *) malloc (400); constant *tempc = (constant *)malloc(sizeof (constant)); tempc->next = my_constants; my_constants = tempc; sprintf (temp, "%s", name); tempc->constant_name = temp; temp = (char *) malloc (400); sprintf (temp, "%s", name); tempc->constant_simple_name = temp;#ifdef GEN_TRACE printf("Added constant %s %s (%s)\n", tempc->constant_name, value, type);#endif fprintf(hfile, "const %s %s = %s;\n", type, tempc->constant_name, value); return 0;}done_constants(){ fprintf(hfile,"}\n\n");}add_state(char *name, char* hint){ state *news; int status = -1; if(hint == NULL || !strcmp(hint, "ready")) { status = 0; } else if(!strcmp(hint, "unready")) { status = 1; } else { printf("ERROR: State hint invalid (expecting 'ready' or 'unready', got %s)\n", hint); exit(-1); } news = (state *) malloc (sizeof(state)); news->base_state_name = (char*) malloc(400); news->status = status; sprintf(news->base_state_name,"%s",name); news->state_name = (char *) malloc (400); sprintf(news->state_name, "%s_state_%s", protocol_name, name); news->state_hint_name = (char *) malloc (400); sprintf(news->state_hint_name, "%s_state_hint_%s", protocol_name, name);#ifdef GEN_TRACE printf("Added state %s\n", news->state_name);#endif news->next = my_states; my_states = news;}char *proc_object(char *in){ char *out, *temp; constant *tempc; object_replace *tempor; int replace_it=0; out = (char *)malloc(80); tempc = my_constants; while (tempc) { if (strcmp(in, tempc->constant_simple_name) == 0) { strcpy(out, tempc->constant_name); return out; } tempc = tempc->next; } if (strncmp(in, "from", 4) == 0) { temp = in; sprintf(out, "%s", temp); } else if (strncmp(in, "field_", 6) == 0) { char *field = in + 6; sprintf(out, "hdr->%s_header_%s", protocol_name, field); } else if (strncmp(in, "randint_", 8) == 0) { char *field = in + 8; sprintf(out, "random_integer(%s)", field); } else if (strncmp(in, "hashof_", 7) == 0) { char *field = in + 7; sprintf(out, "macedon_hash(%s)", field); } else if (strncmp(in, "size_", 5) == 0) { char *field = in + 5; sprintf(out, "%s.count", proc_object(field)); } else if (strncmp(in, "random_", 7) == 0) { char *neighbortype = in + 7; sprintf(out, "((&%s)->rand_neighbor())", neighbortype); } else { char *extrastuff = 0; temp = in; tempor = my_replaces; while (tempor && replace_it == 0) { if (!strncmp(in, tempor->var, strlen(tempor->var))) { replace_it = 1; if (strlen(tempor->var) < strlen(in)) { extrastuff = in+strlen(tempor->var); } } else tempor=tempor->next; } if (replace_it) { if (extrastuff) sprintf(out, "%s.entries[q%d]%s", tempor->replace, tempor->count, extrastuff); else sprintf(out, "%s.entries[q%d]", tempor->replace, tempor->count); } else sprintf(out, "%s", temp); } return out;}transport *add_transport (char *low, char *high, int queue_size){ transport *new = (transport *) malloc (sizeof(transport)); static int portval=0; if (!base_name) if (strcmp(low, "TCP") && strcmp(low, "SWP") && strcmp(low, "UDP") && strcmp(low, "TFRC")) { printf ("Unrecognized transport type %s\n", low); exit(6); } new->over = (char *) malloc (strlen(high)+1); new->under = (char *) malloc (strlen(low)+1); sprintf (new->over, "%s", high); sprintf (new->under, "%s", low); new->type = 0; new->port = portval++; if (!strcmp(low, "TCP")) { new->type = (char *) malloc (500);#ifdef ADOLFO_TRANSPORTS sprintf (new->type, "macedon_tcp");#else sprintf (new->type, "threaded");#endif new->threads = 20; new->queue = queue_size; if (new->queue == 0) new->queue = 20; // new->queue = 0; } else if (!strcmp(low, "TFRC")) { new->type = (char *) malloc (500); sprintf (new->type, "tfrc"); new->threads = 1; new->queue = queue_size; if (new->queue == 0) new->queue = 20; } else if (!strcmp(low, "UDP")) { new->type = (char *) malloc (500); sprintf (new->type, "macedon_udp"); new->threads = 1; new->queue = queue_size; if (new->queue == 0) new->queue = 1; } new->next = my_transports; my_transports = new;#ifdef GEN_TRACE printf("Added transport %s %s queue_size %d\n", low, high, queue_size);#endif return new;}transport *find_transport (char *find) { transport *temp = my_transports; if (find) while (temp) { if (!strcmp(temp->over, find)) return temp; temp = temp->next; } else if (base_name) while (temp) { if (!strcmp(temp->under, "0")) return temp; temp = temp->next; } else while (temp) { if (!strcmp(temp->over, "TCP")) return temp; temp = temp->next; } if (!base_name) { if (!find || !strcmp(find, "TCP") || !strcmp(find, "SWP") || !strcmp(find, "UDP") || !strcmp(find, "TFRC")) { if (!find) return add_transport("TCP", "TCP", 0); else return add_transport(find, find, 0); } else return 0; } else { if (find) return add_transport (find, find, 0); else return add_transport ("0", "0", 0); }}start_messages(){ // add heartbeat message transport *da_tran; message *new = (message *) malloc (sizeof(message)); new->fields=0; new->num_fields=0; new->message_name = (char *) malloc (400); sprintf(new->message_name, "%s_message_%s", protocol_name, "pulse"); new->simple_name = (char *) malloc (400); sprintf(new->simple_name, "%s", "pulse"); if (!base_name) { da_tran = find_transport("UDP"); if (!da_tran) { printf ("Unrecognized transport type %s specified for message %s.\n", "UDP", "pulse"); exit(4); } } else { da_tran = find_transport(0); } new->message_transport = da_tran; //#ifdef GEN_TRACE printf("Adding pulse message %s with transport %s \n", new->message_name, new->message_transport->under); //#endif new->next = my_messages; my_messages = new; add_message_field ("pulse", "int", "request", "1", 0);}message *add_message (char *name, char *transport_name){ transport *da_tran; message *new = (message *) malloc (sizeof(message)); new->fields=0; new->num_fields=0; new->message_name = (char *) malloc (400); sprintf(new->message_name, "%s_message_%s", protocol_name, name); new->simple_name = (char *) malloc (400); sprintf(new->simple_name, "%s", name); da_tran = find_transport(transport_name); if (!da_tran) { printf ("Unrecognized transport type %s specified for message %s.\n", transport_name, name); exit(4); } new->message_transport = da_tran;#ifdef GEN_TRACE printf("Adding message %s with transport %s \n", new->message_name, new->message_transport->under);#endif new->next = my_messages; my_messages = new; return new;}fill_pkt(message* temp, FILE* ostr, char* endl) { message_field *field = temp->fields; while (field) { if (!strcmp(field->field_size,"1")) {#ifndef VARIABLE_HEADERS fprintf(ostr, " outhdr->%s = %s; %s", field->field_name, field->field_nickname, endl);#else if(field->neighbor) { fprintf(ostr, " position += %s.serialize(outhdr+position); %s", field->field_nickname, endl); } else { fprintf(ostr, " *(%s*)(outhdr+position) = %s; %s", field->field_type, field->field_nickname, endl); fprintf(ostr, " position += sizeof(%s); %s", field->field_nickname, endl); }#endif } else { fprintf(ostr, " for (int k=0; k<%s; k++) %s", field->field_size, endl); fprintf(ostr, " { %s", endl);#ifndef VARIABLE_HEADERS fprintf(ostr, " outhdr->%s[k] = %s[k]; %s", field->field_name, field->field_nickname, endl);#else if(field->neighbor) { fprintf(ostr, " position += %s[k].serialize(outhdr+position); %s", field->field_nickname, endl); } else { fprintf(ostr, " *(%s*)(outhdr+position) = %s[k]; %s", field->field_type, field->field_nickname, endl); fprintf(ostr, " position += sizeof(%s[k]); %s", field->field_nickname, endl); }#endif fprintf(ostr, " } %s", endl); } field = field->next; }}alloc_and_fill_pkt(message* temp){ message_field* field; /* allocate */ fprintf(hfilemacros,"{ \\\n"); fprintf(hfilemacros," Packet *send_pkt = create_pkt(%s, dest); \\\n", temp->message_name); /* fill in fields */ #ifdef ALLINONEHDR fprintf(hfilemacros, " hdr_%s* outhdr = (hdr_%s*)gethdr%s( send_pkt ); \\\n", protocol_name, protocol_name, protocol_name);#else#ifndef VARIABLE_HEADERS fprintf(hfilemacros, " hdr_%s_%s* outhdr = (hdr_%s_%s*)gethdr%s( send_pkt ); \\\n", protocol_name, temp->simple_name, protocol_name, temp->simple_name, protocol_name);#else fprintf(hfilemacros, " ((struct macedon_fields*)gethdr%s( send_pkt ))->mh_expanded_size_=sizeof(hdr_%s_%s); \\\n", protocol_name, protocol_name, temp->simple_name); fprintf(hfilemacros, " char* outhdr = (char*)gethdr%s( send_pkt ); \\\n", protocol_name, temp->simple_name, protocol_name, temp->simple_name, protocol_name); fprintf(hfilemacros, " int position=sizeof(hdr_%s); \\\n",protocol_name);#endif#endif fill_pkt(temp, hfilemacros, "\\\n");#ifndef ALLINONEHDR#ifdef VARIABLE_HEADERS fprintf(hfilemacros, " ASSERT(send_pkt->hdrlen_ >= position + sizeof(hdr_ip)+sizeof(hdr_cmn));\\\n"); fprintf(hfilemacros, " send_pkt->hdrlen_ = position + sizeof(hdr_ip)+sizeof(hdr_cmn);\\\n");#endif#endif fprintf(hfilemacros, " bytes_sent += send_pkt->hdrlen_; \\\n");}done_messages(){ transport *temptran = my_transports; message *temp = my_messages; message_field *field; int count = 0; while (temp) { fflush(stdout); fprintf(hfile, "#define %s (PT_%s * 100)+%d\n", temp->message_name, protocol_name, count); count ++; temp = temp->next; } fprintf(hfile, "\n"); fprintf(hfile, "struct hdr_%s\n", protocol_name); fprintf(hfile, "{\n"); fprintf(hfile, " struct macedon_fields %s_mf_;\n", protocol_name); temp = my_messages;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -