My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
msu_type.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
26 #include "dfg.h"
27 #include "msu_type.h"
28 #include "logging.h"
29 #include "msu_type_list.h"
30 
35 #define MAX_TYPE_ID 1000
36 
42 
46 #define N_MSU_TYPES (sizeof(DEFINED_MSU_TYPES) / sizeof(struct msu_type*))
47 
51 static struct msu_type *msu_types[MAX_TYPE_ID];
52 
58 static int register_msu_type(struct msu_type *type) {
59  if (type->id > MAX_TYPE_ID) {
60  log_error("MSU type %s not registered. Type ID %d too high. Max: %d",
61  type->name, type->id, MAX_TYPE_ID);
62  return -1;
63  }
64  msu_types[type->id] = type;
65  log(LOG_MSU_TYPE_INIT, "Registered MSU type %s", type->name);
66  return 0;
67 }
68 
70  for (int i=0; i < N_MSU_TYPES; i++) {
71  struct msu_type *type = DEFINED_MSU_TYPES[i];
72  if (msu_types[type->id] != NULL) {
73  if (msu_types[type->id]->destroy_type != NULL) {
74  msu_types[type->id]->destroy_type(type);
75  }
76  }
77  }
78 }
79 
80 struct msu_type *get_msu_type(int id) {
81  if (id > MAX_TYPE_ID) {
82  log_error("MSU type %d cannot be found Type ID too high. Max: %d",
83  id, MAX_TYPE_ID);
84  return NULL;
85  }
86  return msu_types[id];
87 }
88 
94 static bool has_required_fields(struct msu_type *type) {
95  if (type->receive == NULL) {
96  log_error("MSU type %s does not register a recieve function",
97  type->name);
98  return false;
99  }
100  return true;
101 }
102 
109 static int init_msu_type(struct msu_type *type) {
110  if (!has_required_fields(type)) {
111  log_error("Not registering MSU type %s due to missing fields",
112  type->name);
113  return -1;
114  }
115  if (type->init_type) {
116  if (type->init_type(type) != 0) {
117  log_error("MSU Type %s not initialized: failed custom constructor",
118  type->name);
119  return -1;
120  } else {
121  log(LOG_MSU_TYPE_INIT, "Initialized msu type %s", type->name);
122  }
123  }
124  if (register_msu_type(type) != 0) {
125  log_error("MSU type %s not registered", type->name);
126  return -1;
127  }
128  return 0;
129 }
130 
131 
132 int init_msu_type_id(unsigned int type_id) {
133  log(TEST, "Number of MSU types: %d", (int)N_MSU_TYPES);
134  for (int i=0; i<N_MSU_TYPES; i++) {
135  struct msu_type *type = DEFINED_MSU_TYPES[i];
136  if (type->id == type_id) {
137  return init_msu_type(type);
138  }
139  }
140  log_error("MSU Type ID %u not found!", type_id);
141  return -1;
142 }
#define MAX_TYPE_ID
MOVEME: MAX_TYPE_ID This is the maximum ID that can be assigned to an MSU type.
Definition: msu_type.c:35
void destroy_msu_types()
Calls the type-sepecific constructor for each instantiated MSU type.
Definition: msu_type.c:69
Defines a type of MSU, including callback and accessor functions.
static struct msu_type * DEFINED_MSU_TYPES[]
Every MSU type that can be used.
Definition: msu_type.c:41
#define MSU_TYPE_LIST
The complete list of MSU types.
struct msu_type * get_msu_type(int id)
Gets the MSU type with the provided ID.
Definition: msu_type.c:80
Logging of status messages to the terminal.
Defines the list of MSUs that can be instantiated on a runtime.
int(* receive)(struct local_msu *self, struct msu_msg *msg)
Handles the receiving of data sent from other MSUs.
Definition: msu_type.h:89
unsigned int id
Numerical identifier for the MSU.
Definition: msu_type.h:51
#define log_error(fmt,...)
Definition: logging.h:101
static bool has_required_fields(struct msu_type *type)
Checks whether the msu type has the required fields to be used.
Definition: msu_type.c:94
int init_msu_type_id(unsigned int type_id)
Initializes the MSU type with the given ID, calling the custom constructor if appropriate.
Definition: msu_type.c:132
Defines a type of MSU.
Definition: msu_type.h:46
#define N_MSU_TYPES
The number of available MSU types.
Definition: msu_type.c:46
Interfaces for the creation and modification of the data-flow-graph and and general description of th...
static int register_msu_type(struct msu_type *type)
Regsiters an MSU type so that it can be later referenced by its ID.
Definition: msu_type.c:58
static int init_msu_type(struct msu_type *type)
Initializes a specific MSU type, calling its init function and registering it for future calls...
Definition: msu_type.c:109
void(* destroy_type)(struct msu_type *type)
Destructor for an MSU type, should destroy context initialized in init_type.
Definition: msu_type.h:64
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
char * name
Name for the msu type.
Definition: msu_type.h:48
int(* init_type)(struct msu_type *type)
Constructor for an MSU_type itself, should initialize whatever context all MSUs of this type might ne...
Definition: msu_type.h:59
static struct msu_type * msu_types[1000]
Pointers to MSU Types, indexed by ID.
Definition: msu_type.c:51