While working on docs, I was getting excited about something cool that I could 
do with pn_messenger_route() ----
And then I realized that I couldn't.

What would you think about allowing replacement of an old route by a new route 
with the same pattern ?

It seems like this would allow some cool, adaptive behavior in Proton networks.

I just coded and tested a simple case successfully -- sending 2 messages to an 
abstract address, and having them go to 2 different receivers because a new 
route got set in between.

Would this behavior cause any problems that would outweigh the coolness?


Code seems pretty straightforward - - - -


int pn_messenger_route(pn_messenger_t *messenger, const char *pattern, const 
char *address)
{
  if (strlen(pattern) > PN_MAX_PATTERN || strlen(address) > PN_MAX_ROUTE) {
    return PN_ERR;
  }
  pn_route_t *new_route = (pn_route_t *) malloc(sizeof(pn_route_t));
  if (!new_route) return PN_ERR;

  strcpy(new_route->pattern, pattern);
  strcpy(new_route->address, address);
  new_route->next = NULL;

  /* The list is empty. */
  if (! messenger->routes ) {
    messenger->routes = new_route;
    return 0;
  } 

  pn_route_t *old;

  /* The route to be replaced is first on the list. */
  if ( ! strcmp ( messenger->routes->pattern, new_route->pattern ) ) {
    old = messenger->routes;
    new_route->next = old->next;
    messenger->routes = new_route;
    free ( (char *) old );
    return 0;
  }

  pn_route_t *route = messenger->routes;

  /* The route to be replaced is somewhere down the list, or not there. */
  while ( 1 ) {
    /* No route in list had same pattern. */
    if ( ! route->next ) {
      route->next = new_route;
      return 0;
    }
    /* Bingo ! */
    if ( ! strcmp ( route->next->pattern, new_route->pattern ) ) {
      old = route->next;
      new_route->next = old->next;
      route->next = new_route;
      free ( (char *) old );
      return 0;
    }
    
    route = route->next;
  }

  return 0;
}

Reply via email to