Skip to content

Instantly share code, notes, and snippets.

@isaac-ped
Created October 16, 2018 18:04
Show Gist options
  • Save isaac-ped/4c93fe0af1bcd79217e12c70f897c0eb to your computer and use it in GitHub Desktop.
Save isaac-ped/4c93fe0af1bcd79217e12c70f897c0eb to your computer and use it in GitHub Desktop.
int rt_id_routing(struct msu_type *type, struct local_msu *sender,
struct msu_msg *msg, struct msu_endpoint *output) {
struct routing_table *table = get_type_from_route_set(&sender->routes, type->id);
if (table == NULL) {
log_error("No routes available from msu %d to type %s (%d)",
sender->id, type->name, type->id);
return -1;
}
// Line MAX_ENDPOINTS could be replaced by a call to get_n_endpoints()
struct msu_endpoint endpoints[MAX_ENDPOINTS];
int n_endpoints = get_endpoints_by_runtime_id(table, RT_ID, endpoints, MAX_ENDPOINTS);
if (n_endpoints <= 0) {
log_error("No endpoints available on runtime %d", RT_ID);
return -1;
}
// To choose a random one of the returned endpoints
int idx = int(rand() * (n_endpoints - 1));
// You could also use (msg->id % n_endpoints) if you wanted some
// sort of consistency
// Fill the output with the chosen endpoint
*output = endpoints[idx];
return 0;
}
...
struct msu_type MY_MSU_TYPE {
.name = "My_MSU",
...
.route = rt_id_routing
};
@yuankaiz
Copy link

Hi Isaac,
For line 11, struct msu_endpoint endpoints[MAX_ENDPOINTS];, if it is guaranteed that there is only one msu of the specific type on the specific runtime, may I just create an array of length 1?
Thanks.
YZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment