void unit_message_is_response(UNUSED(void **state))
{
  msgpack_sbuffer sbuf;
  msgpack_packer pk;
  msgpack_zone mempool;
  msgpack_object deserialized;

  msgpack_sbuffer_init(&sbuf);
  msgpack_zone_init(&mempool, 2048);

  /* positiv test */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_pack_uint8(&pk, 0);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_true(message_is_response(&deserialized));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong type test */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 0);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint8(&pk, 1);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_false(message_is_response(&deserialized));

  msgpack_sbuffer_clear(&sbuf);

  /* no array test */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_uint8(&pk, 1);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_false(message_is_response(&deserialized));

  msgpack_sbuffer_clear(&sbuf);

  /* NULL test */
  assert_false(message_is_response(NULL));

  msgpack_sbuffer_clear(&sbuf);

  msgpack_zone_destroy(&mempool);
  msgpack_sbuffer_destroy(&sbuf);
}
Beispiel #2
0
void api_request_status(rpc_io *rpcio, msgpack_object *request_obj,
                        msgpack_packer *response_msg) {
  int rc;
  char *program_name = NULL;
  size_t program_len = 0;
  rc = obj_get(request_obj, "program", MSGPACK_OBJECT_RAW, &program_name, &program_len);
  /* A missing 'program' field is OK. It means we want all programs */

  msgpack_pack_map(response_msg, 1);
  msgpack_pack_string(response_msg, "programs", -1);

  /* programs is a map of:
   *   programname => {
   *     command: "string",
   *     args: "string",
   *     uid: uid,
   *     gid: gid,
   *     nice: nice,
   *     ionice: ionice,
   *     is_running: boolean
   *     instances: {
   *       pid: ...
   *       state: ...
   *       admin_state: ...
   *       start: ...
   *       duration: ...
   *     }
   *   }
   */
  msgpack_pack_map(response_msg, rpcio->procnanny->programs_len);

  pn_prog_each(rpcio->procnanny, i, program, {
    if (program_name != NULL && strncmp(program->name, program_name, program_len)) {
      continue;
    }

    msgpack_pack_string(response_msg, program->name, program->name_len);
    msgpack_pack_map(response_msg, 8);  /* 8 fields */

    msgpack_pack_string(response_msg, "command", -1);
    msgpack_pack_string(response_msg, program->command, program->command_len);

    msgpack_pack_string(response_msg, "args", -1);
    msgpack_pack_array(response_msg, program->args_len);
    int argind = 0;
    for (argind = 0; argind < program->args_len; argind++) {
      msgpack_pack_string(response_msg, program->args[argind], -1);
    }

    msgpack_pack_string(response_msg, "uid", -1);
    msgpack_pack_uint32(response_msg, program->uid);

    msgpack_pack_string(response_msg, "gid", -1);
    msgpack_pack_uint32(response_msg, program->gid);

    msgpack_pack_string(response_msg, "nice", -1);
    msgpack_pack_int32(response_msg, program->nice);

    msgpack_pack_string(response_msg, "ionice", -1);
    msgpack_pack_int32(response_msg, program->ionice);

    msgpack_pack_string(response_msg, "active", -1);
    msgpack_pack_true(response_msg);

    msgpack_pack_string(response_msg, "instances", -1);
    msgpack_pack_map(response_msg, program->nprocs);

    pn_prog_proc_each(program, instance, process, {
      msgpack_pack_uint32(response_msg, instance);
      msgpack_pack_map(response_msg, 5);

      msgpack_pack_string(response_msg, "pid", -1);
      msgpack_pack_uint32(response_msg, process->pid);

      msgpack_pack_string(response_msg, "state", -1);
      switch (process->state) {
        case PROCESS_STATE_STARTING:
          msgpack_pack_string(response_msg, "starting", -1); break;
        case PROCESS_STATE_RUNNING:
          msgpack_pack_string(response_msg, "running", -1); break;
        case PROCESS_STATE_STOPPING:
          msgpack_pack_string(response_msg, "stopping", -1); break;
        case PROCESS_STATE_EXITED:
          msgpack_pack_string(response_msg, "exited", -1); break;
        case PROCESS_STATE_BACKOFF:
          msgpack_pack_string(response_msg, "backoff", -1); break;
        case PROCESS_STATE_NEW:
          msgpack_pack_string(response_msg, "new", -1); break;
        default:
          msgpack_pack_string(response_msg, "unknown", -1); break;
      }

      msgpack_pack_string(response_msg, "exitcode", -1);
      msgpack_pack_uint8(response_msg, process->exit_status);

      msgpack_pack_string(response_msg, "exitsignal", -1);
      msgpack_pack_uint8(response_msg, process->exit_signal);

      msgpack_pack_string(response_msg, "admin_state", -1);
      switch (process->admin_state) {
        case ADMIN_STATE_DOWN:
          msgpack_pack_string(response_msg, "down", -1); break;
        case ADMIN_STATE_UP:
          msgpack_pack_string(response_msg, "up", -1); break;
        default:
          msgpack_pack_string(response_msg, "unknown", -1); break;
      }
    })
  });
void unit_message_deserialize_response(UNUSED(void **state))
{
  struct api_error error = ERROR_INIT;
  msgpack_sbuffer sbuf;
  msgpack_packer pk;
  msgpack_zone mempool;
  msgpack_object deserialized;
  struct message_response response;

  msgpack_sbuffer_init(&sbuf);
  msgpack_zone_init(&mempool, 2048);

  /* positiv test */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint32(&pk, 1234);
  msgpack_pack_nil(&pk);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  free_params(response.params);

  /* wrong type type */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_nil(&pk);
  msgpack_pack_uint32(&pk, 1234);
  msgpack_pack_nil(&pk);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong type */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 0);
  msgpack_pack_uint32(&pk, 1234);
  msgpack_pack_nil(&pk);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong msgid */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_int(&pk, -1234);
  msgpack_pack_nil(&pk);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong msgid value*/
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint32(&pk, UINT32_MAX);
  msgpack_pack_nil(&pk);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong nil */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint32(&pk, 1234);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_array(&pk, 1);
  msgpack_pack_uint8(&pk, 0);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* wrong params */
  msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
  msgpack_pack_array(&pk, 4);
  msgpack_pack_uint8(&pk, 1);
  msgpack_pack_uint32(&pk, 1234);
  msgpack_pack_nil(&pk);
  msgpack_pack_nil(&pk);
  msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);

  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      &error));

  msgpack_sbuffer_clear(&sbuf);

  /* null input params */
  assert_int_not_equal(0, message_deserialize_response(&response, &deserialized,
      NULL));
  assert_int_not_equal(0, message_deserialize_response(&response, NULL,
      &error));
  assert_int_not_equal(0, message_deserialize_response(NULL, &deserialized,
      &error));

  msgpack_zone_destroy(&mempool);
  msgpack_sbuffer_destroy(&sbuf);
}
Beispiel #4
0
int msgpack_pack_uint8_wrap(msgpack_packer* pk, uint8_t d)
{
  return msgpack_pack_uint8(pk, d);
}
void ANetworkController::ReceivedGetBehaviors() {
	msgpack_sbuffer sbuf;
	msgpack_sbuffer_init(&sbuf);

	/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
	msgpack_packer pk;
	msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);

	msgpack_pack_array(&pk, 2);
	msgpack_pack_uint16(&pk, EPacketType::PT_GetBehaviors);

	TArray<ABehaviours*> BehaviorsList;
	for (TActorIterator<ABehaviours> ObjIt(GetWorld()); ObjIt; ++ObjIt) {
		ABehaviours* Behaviors = *ObjIt;
		BehaviorsList.Add(Behaviors);
	}

	int32 BehaviorsListNum = BehaviorsList.Num();
	msgpack_pack_array(&pk, BehaviorsListNum);
	for (int i = 0; i < BehaviorsListNum; i++) {
		ABehaviours* Behaviors = BehaviorsList[i];
		msgpack_pack_map(&pk, 4);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "FlockingState", 13);
		msgpack_pack_uint8(&pk, (uint8)Behaviors->FlockingState);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "ThrottleGains", 13);
		Pack(&pk, Behaviors->ThrottleGains);
		msgpack_pack_str(&pk, 13);
		msgpack_pack_str_body(&pk, "SteeringGains", 13);
		Pack(&pk, Behaviors->SteeringGains);

		int32 BehaviorsNum = Behaviors->Behaviours.Num();
		msgpack_pack_str(&pk, 9);
		msgpack_pack_str_body(&pk, "Behaviors", 9);
		msgpack_pack_array(&pk, BehaviorsNum);
		for (int j = 0; j < BehaviorsNum; j++) {
			UBehaviour* Behavior = Behaviors->Behaviours[j];

			TArray<UProperty*> Properties;
			for (TFieldIterator<UProperty> PropIt(Behavior->GetClass()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				if (Cast<UNumericProperty>(Property) || Cast<UBoolProperty>(Property)) {
					Properties.Add(Property);
				}
			}

			msgpack_pack_map(&pk, Properties.Num() + 1);
			msgpack_pack_str(&pk, 4);
			msgpack_pack_str_body(&pk, "Name", 4);
			FString Name = Behavior->GetClass()->GetName();
			msgpack_pack_str(&pk, Name.Len());
			msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
			for (auto PropIt(Properties.CreateIterator()); PropIt; ++PropIt) {
				UProperty* Property = *PropIt;
				const void* Value = Property->ContainerPtrToValuePtr<uint8>(Behavior);
				FString Name = Property->GetName();
				msgpack_pack_str(&pk, Name.Len());
				msgpack_pack_str_body(&pk, TCHAR_TO_UTF8(*Name), Name.Len());
				if (UNumericProperty *NumericProperty = Cast<UNumericProperty>(Property)) {
					if (NumericProperty->IsFloatingPoint()) {
						msgpack_pack_double(&pk, NumericProperty->GetFloatingPointPropertyValue(Value));
					}
					else if (NumericProperty->IsInteger()) {
						msgpack_pack_int(&pk, NumericProperty->GetSignedIntPropertyValue(Value));
					}
				}
				else if (UBoolProperty *BoolProperty = Cast<UBoolProperty>(Property)) {
					if (BoolProperty->GetPropertyValue(Value)) {
						msgpack_pack_true(&pk);
					}
					else {
						msgpack_pack_false(&pk);
					}
				}
			}
		}
	}

	int32 BytesSent;
	TcpSocket->Send((uint8*)sbuf.data, sbuf.size, BytesSent);

	msgpack_sbuffer_destroy(&sbuf);
}