Ejemplo n.º 1
0
static void jsmn_dump_tokens(const char const * string, const jsmntok_t const * tokens, const word object_index)
{
   char zs[256], zs1[256];

   word i;
   for(i = object_index + 1; tokens[i].start >= 0 && tokens[i].start < tokens[object_index].end; i++)
   {
      sprintf(zs, "Token %2d: Type=", i);
      switch(tokens[i].type)
      {
      case JSMN_PRIMITIVE: strcat(zs, "Primitive"); break;
      case JSMN_OBJECT:    strcat(zs, "Object   "); break;
      case JSMN_ARRAY:     strcat(zs, "Array    "); break;
      case JSMN_STRING:    strcat(zs, "String   "); break;
      case JSMN_NAME:      strcat(zs, "Name     "); break;
      }
      sprintf(zs1, " Start=%d End=%d", tokens[i].start, tokens[i].end);
      strcat(zs, zs1);
      if(tokens[i].type == JSMN_OBJECT || tokens[i].type == JSMN_ARRAY)
      {
         sprintf(zs1, " Size=%d", tokens[i].size);
         strcat(zs, zs1);
      }
      if(tokens[i].type == JSMN_STRING || tokens[i].type == JSMN_NAME || tokens[i].type == JSMN_PRIMITIVE)
      {
         jsmn_extract_token(string, tokens, i, zs1, sizeof(zs1));
         strcat(zs, " \"");
         strcat(zs, zs1);
         strcat(zs, "\"");
      }
      _log(GENERAL, zs);
   }

   return;
}
Ejemplo n.º 2
0
static void process_frame(const char * body)
{
   jsmn_parser parser;
   time_t elapsed = time(NULL);
   
   jsmn_init(&parser);
   int r = jsmn_parse(&parser, body, tokens, NUM_TOKENS);
   if(r != 0) 
   {
      _log(MAJOR, "Parser result %d.  Message discarded.", r);
      stats[NotVSTP]++;
   }
   else
   {
      // Good message
      char message_name[128];
      jsmn_extract_token(body, tokens, 1, message_name, sizeof(message_name));
      if(!strcmp(message_name, "VSTPCIFMsgV1")) 
      {
         process_vstp(body, tokens);
      }
      else
      {
         _log(MINOR, "Unrecognised message name \"%s\".", message_name);
         jsmn_dump_tokens(body, tokens, 0);
         stats[NotVSTP]++;
      }
   }
   elapsed = time(NULL) - elapsed;
   if(elapsed > 1 || debug)
   {
      _log(MINOR, "Transaction took %ld seconds.", elapsed);
   }
}
Ejemplo n.º 3
0
static void process_object(const char * object_string)
{
   // Passed a string containing a JSON object.
   // Parse it and process it.
   char zs[256];

   _log(PROC, "process_object()");

   jsmn_parser parser;
   jsmn_init(&parser);

   int r = jsmn_parse(&parser, object_string, tokens, MAX_TOKENS);
   if(r != JSMN_SUCCESS)
   {
      sprintf(zs, "Parser result %d.  ", r);
      
      switch(r)
      {
      case JSMN_SUCCESS:     strcat(zs, "Success.  "); break;
      case JSMN_ERROR_INVAL: strcat(zs, "Error - Invalid.  "); break;
      case JSMN_ERROR_NOMEM: strcat(zs, "Error - Nomem.  "); break;
      case JSMN_ERROR_PART:  strcat(zs, "Error - Part JSON.  "); break;
      default:               strcat(zs, "Unknown response.  "); break;
      }

      _log(MAJOR, zs);
      return;
   }

   if(tokens[1].type == JSMN_NAME)
   {
      char message_name[128];

      stats[JSONRecords]++; // NB This INCLUDES ones we don't use, like TiplocV1
      jsmn_extract_token(object_string, tokens, 1, message_name, sizeof(message_name));
      if(!strcmp(message_name, "JsonTimetableV1"))        process_timetable(object_string, tokens);
      else if(!strcmp(message_name, "JsonAssociationV1")) ;
      else if(!strcmp(message_name, "JsonScheduleV1"))    process_schedule(object_string, tokens);
      else if(!strcmp(message_name, "TiplocV1"))          ;
      else if(!strcmp(message_name, "EOF"))               ;
      else
      {
         _log(MINOR, "Unrecognised message name \"%s\".", message_name);
         jsmn_dump_tokens(object_string, tokens, 0);
         stats[JSONRecords]--;
      }
   }
   else
   {
      _log(MAJOR, "Unrecognised message.");
      jsmn_dump_tokens(object_string, tokens, 0);
   }
   return;
}
Ejemplo n.º 4
0
void jsmn_find_extract_token(const char * string, const jsmntok_t * tokens, const word object_index, const char * search, char * result, const size_t max_length)
{
   // Search the object at token number object_index for a name of value search, and return the value of the next token.
   size_t index = jsmn_find_name_token(string, tokens, object_index, search);
   if(index > 0 && tokens[index + 1].start >= 0)
   {
      jsmn_extract_token(string, tokens, index + 1, result, max_length);
   }
   else
   {
      result[0] = '\0';
   }
}
Ejemplo n.º 5
0
word jsmn_find_name_token(const char * string, const jsmntok_t * tokens, const word object_index, const char * search)
{
   // Search the object at token number object_index for a name of value search, and return its token number or 0 for not found.
   char zs[2048];

   word i;
   for(i = object_index + 1; tokens[i].start >= 0 && tokens[i].start < tokens[object_index].end; i++)
   {
      if(tokens[i].type == JSMN_NAME)
      {
         jsmn_extract_token(string, tokens, i, zs, sizeof(zs));
         if(!strcasecmp(zs, search))
         {
            // Hit
            return i;
         }
      }
   }

   //sprintf(zs, "jsmn_find_name_token(\"%s\") Miss.", search);
   //_log(MAJOR, zs);

   return 0;
}