Ejemplo n.º 1
0
/** Unmarshals the next content of an MBuffer into an OmlValue with
 * type-checking.
 *
 * \param mbuf MBuffer to read from
 * \param type OmlValueT specifying the type to unmarshall
 * \param value pointer to OmlValue to unmarshall the read data into
 * \return 1 if successful, 0 otherwise (e.g., type mismatch)
 */
int
unmarshal_typed_value (MBuffer* mbuf, const char* name, OmlValueT type, OmlValue* value)
{
  if (unmarshal_value (mbuf, value) != 1) {
      logerror("Error reading %s from binary packet\n", name);
      return -1;
  }

  if (oml_value_get_type(value) != type) {
      logerror("Expected type '%s' for %s, but got type '%s' instead\n",
             oml_type_to_s (type), name, oml_type_to_s (oml_value_get_type(value)));
      return -1;
  }
  return 0;
}
Ejemplo n.º 2
0
static int
sample(
    OmlFilter* f,
    OmlValue*  value  //! values of sample
) {
    InstanceData* self = (InstanceData*)f->instance_data;
    OmlValueU* v = oml_value_get_value(value);;
    OmlValueT type = oml_value_get_type(value);;
    double val;

    switch (type) {
    case OML_LONG_VALUE:
        val = v->longValue;
        break;
    case OML_DOUBLE_VALUE:
        val = v->doubleValue;
        break;
    default:
        // raise error;
        return -1;
    }
    if (isnan(self->sample_sum)) {
        self->sample_sum = val;
    } else {
        self->sample_sum += val;
    }
    if (val < self->sample_min || isnan(self->sample_min)) self->sample_min = val;
    if (val > self->sample_max || isnan(self->sample_max)) self->sample_max = val;
    self->sample_count++;
    return 0;
}
Ejemplo n.º 3
0
/**
 * \brief write the result inside the file
 * \param writer pointer to writer instance
 * \param values type of sample
 * \param value_count size of above array
 * \return 1 if successful, 0 otherwise
 */
static int
row_cols(OmlWriter* writer, OmlValue* values, int value_count)
{
  OmlTextWriter* self = (OmlTextWriter*)writer;
  MBuffer* mbuf;
  if ((mbuf = self->mbuf) == NULL) return 0; /* previous use of mbuf failed */

  int i;
  OmlValue* v = values;
  for (i = 0; i < value_count; i++, v++) {
    int res;
    switch (oml_value_get_type(v)) {
    case OML_LONG_VALUE: {
      res = mbuf_print(mbuf, "\t%" PRId32, oml_value_clamp_long (v->value.longValue));
      break;
    }
    case OML_INT32_VALUE:  res = mbuf_print(mbuf, "\t%" PRId32,  v->value.int32Value);  break;
    case OML_UINT32_VALUE: res = mbuf_print(mbuf, "\t%" PRIu32,  v->value.uint32Value); break;
    case OML_INT64_VALUE:  res = mbuf_print(mbuf, "\t%" PRId64,  v->value.int64Value);  break;
    case OML_UINT64_VALUE: res = mbuf_print(mbuf, "\t%" PRIu64,  v->value.uint64Value); break;
    case OML_DOUBLE_VALUE: res = mbuf_print(mbuf, "\t%f",  v->value.doubleValue); break;
    case OML_STRING_VALUE: res = mbuf_print(mbuf, "\t%s",  omlc_get_string_ptr(*oml_value_get_value(v))); break;
    case OML_BLOB_VALUE: {
      const unsigned int max_bytes = 6;
      int bytes = v->value.blobValue.length < max_bytes ? v->value.blobValue.length : max_bytes;
      int i = 0;
      res = mbuf_print(mbuf, "blob ");
      for (i = 0; i < bytes; i++) {
        res = mbuf_print(mbuf, "%02x", ((uint8_t*)v->value.blobValue.ptr)[i]);
      }
      res = mbuf_print (mbuf, " ...");
      break;
    }
    default:
      res = -1;
      logerror( "Unsupported value type '%d'\n", oml_value_get_type(v));
      return 0;
    }
    if (res < 0) {
      mbuf_reset_write(mbuf);
      self->mbuf = NULL;
      return 0;
    }
  }
  return 1;
}
Ejemplo n.º 4
0
/** Convert data stored in an OmlValue to a string representation.
 *
 * The given buffer will contain a nul-terminated C string.
 *
 * \param value pointer to OmlValue containing data
 * \param buf buffer to put the textual representation of the value in
 * \param size size of the buffer
 * \return a pointer to buf, or NULL on error
 * \see oml_value_ut_to_s
 */
char*
oml_value_to_s(OmlValue *value, char *buf, size_t size)
{
    OmlValueU* v = oml_value_get_value(value);
    OmlValueT type = oml_value_get_type(value);

    return oml_value_ut_to_s(v, type, buf, size);
}
Ejemplo n.º 5
0
/** Marshal the array of values into an MBuffer.
 *
 * Metadata of the measurement stream should already have been written
 * with marshal_measurements(). Each element of values is written with
 * marshal_value().  Finally, the number of elements in the message is
 * updated in its header, by incrementing the relevant field
 * (depending on its OmlBinMsgType) by value_count.
 *
 * If the returned number is negative, marshalling didn't finish as
 * the provided buffer was short of the number of bytes returned (when
 * multiplied by -1); the entire message has been reset (by
 * marshal_value()), and marshalling should restart with
 * marshal_init(), after the MBuffer has been adequately resized or
 * repacked.
 *
 * Once all data has been marshalled, marshal_finalize() should be
 * called to finish preparing the message.
 *
 * \param mbuf MBuffer to write marshalled data to
 * \param values array of OmlValue of length value_count
 * \param value_count length  the values array
 * \return 1 on success, or -1 otherwise (marshalling should then restart from marshal_init())
 * \see marshal_init, marshal_measurements, marshal_value, marshal_finalize, mbuf_repack_message, mbuf_repack_message2, mbuf_resize
 */
int marshal_values(MBuffer* mbuf, OmlValue* values, int value_count)
{
  OmlValue* val = values;
  int i;

  for (i = 0; i < value_count; i++, val++) {
    if(!marshal_value(mbuf, oml_value_get_type(val), oml_value_get_value(val)))
      return -1;
  }

  uint8_t* buf = mbuf_message (mbuf);
  OmlBinMsgType type = marshal_get_msgtype (mbuf);
  switch (type) {
  case OMB_DATA_P: buf[5] += value_count; break;
  case OMB_LDATA_P: buf[7] += value_count; break;
  }
  return 1;
}
Ejemplo n.º 6
0
static int
sample(
    OmlFilter* f,
    OmlValue * value  //! values of sample
) {
  InstanceData* self = (InstanceData*)f->instance_data;
  OmlValueU* v = oml_value_get_value(value);;
  OmlValueT type = oml_value_get_type(value);;

  if (type != self->result[0].type) {
    logwarn ("%s filter: Discarding sample type (%s) different from initial definition (%s)\n",
        FILTER_NAME, oml_type_to_s(type), oml_type_to_s(self->result[0].type));
    return 0;
  }
  self->sample_count++;
  /* Overwrite previously stored value */
  return oml_value_set(&self->result[0], v, type);
}
Ejemplo n.º 7
0
/** Cast the data contained in an OmlValue to an int.
 *
 * \param value pointer to OmlValue containing the data to cast
 * \return an integer, which defaults to 0 on error
 */
int
oml_value_to_int (OmlValue *value)
{
    OmlValueU *v = oml_value_get_value(value);
    OmlValueT type = oml_value_get_type(value);
    switch (type) {
    case OML_LONG_VALUE:
        logwarn("%s(): OML_LONG_VALUE is deprecated, please use OML_INT32_VALUE instead\n", __FUNCTION__);
        return (int) omlc_get_long(*v);
    case OML_INT32_VALUE:
        return (int) omlc_get_int32(*v);
    case OML_UINT32_VALUE:
        return (int) omlc_get_uint32(*v);
    case OML_INT64_VALUE:
        return (int) omlc_get_int64(*v);
    case OML_UINT64_VALUE:
        return (int) omlc_get_uint64(*v);
    case OML_DOUBLE_VALUE:
        return (int) omlc_get_double(*v);
    default:
        logerror("%s() for type '%d' not implemented'\n", __FUNCTION__, type);
        return 0;
    }
}
Ejemplo n.º 8
0
/** Insert value in the PostgreSQL database.
 *
 * \param db Database that links to the PostgreSQL db
 * \param table DbTable to insert data in
 * \param sender_id sender ID
 * \param seq_no sequence number
 * \param time_stamp timestamp of the receiving data
 * \param values OmlValue array to insert
 * \param value_count number of values
 * \return 0 if successful, -1 otherwise
 */
static int
psql_insert(Database* db,
            DbTable*  table,
            int       sender_id,
            int       seq_no,
            double    time_stamp,
            OmlValue* values,
            int       value_count)
{
  PsqlDB* psqldb = (PsqlDB*)db->handle;
  PsqlTable* psqltable = (PsqlTable*)table->handle;
  PGresult* res;
  int i;
  double time_stamp_server;
  const char* insert_stmt = mstring_buf (psqltable->insert_stmt);
  unsigned char *escaped_blob;
  size_t eblob_len=-1;

  char * paramValues[4+value_count];
  for (i=0;i<4+value_count;i++) {
    paramValues[i] = xmalloc(512*sizeof(char));
  }

  int paramLength[4+value_count];
  int paramFormat[4+value_count];

  sprintf(paramValues[0],"%i",sender_id);
  paramLength[0] = 0;
  paramFormat[0] = 0;

  sprintf(paramValues[1],"%i",seq_no);
  paramLength[1] = 0;
  paramFormat[1] = 0;

  sprintf(paramValues[2],"%.8f",time_stamp);
  paramLength[2] = 0;
  paramFormat[2] = 0;

  struct timeval tv;
  gettimeofday(&tv, NULL);
  time_stamp_server = tv.tv_sec - db->start_time + 0.000001 * tv.tv_usec;

  if (tv.tv_sec > psqldb->last_commit) {
    if (reopen_transaction (psqldb) == -1)
      return -1;
    psqldb->last_commit = tv.tv_sec;
  }

  sprintf(paramValues[3],"%.8f",time_stamp_server);
  paramLength[3] = 0;
  paramFormat[3] = 0;

  OmlValue* v = values;
  for (i = 0; i < value_count; i++, v++) {
    struct schema_field *field = &table->schema->fields[i];
    if (oml_value_get_type(v) != field->type) {
      logerror("psql:%s: Value %d type mismatch for table '%s'\n", db->name, i, table->schema->name);
      return -1;
    }
    switch (field->type) {
    case OML_LONG_VALUE: sprintf(paramValues[4+i],"%i",(int)v->value.longValue); break;
    case OML_INT32_VALUE:  sprintf(paramValues[4+i],"%" PRId32,v->value.int32Value); break;
    case OML_UINT32_VALUE: sprintf(paramValues[4+i],"%" PRIu32,v->value.uint32Value); break;
    case OML_INT64_VALUE:  sprintf(paramValues[4+i],"%" PRId64,v->value.int64Value); break;
    case OML_UINT64_VALUE: sprintf(paramValues[4+i],"%" PRIu64,v->value.uint64Value); break;
    case OML_DOUBLE_VALUE: sprintf(paramValues[4+i],"%.8f",v->value.doubleValue); break;
    case OML_STRING_VALUE: sprintf(paramValues[4+i],"%s", omlc_get_string_ptr(*oml_value_get_value(v))); break;
    case OML_BLOB_VALUE:
                           escaped_blob = PQescapeByteaConn(psqldb->conn,
                               v->value.blobValue.ptr, v->value.blobValue.length, &eblob_len);
                           if (!escaped_blob) {
                             logerror("psql:%s: Error escaping blob in field %d of table '%s': %s\n",
                                 db->name, i, table->schema->name, PQerrorMessage(psqldb->conn));
                           }
                           /* XXX: 512 char is the size allocated above. Nasty. */
                           if (eblob_len > 512) {
                             logdebug("psql:%s: Reallocating %d bytes for big blob\n", db->name, eblob_len);
                             paramValues[4+i] = xrealloc(paramValues[4+i], eblob_len);
                             if (!paramValues[4+i]) {
                               logerror("psql:%s: Could not realloc()at memory for escaped blob in field %d of table '%s'\n",
                                   db->name, i, table->schema->name);
                               return -1;
                             }
                           }
                           snprintf(paramValues[4+i], eblob_len, "%s", escaped_blob);
                           PQfreemem(escaped_blob);
                           break;
    default:
      logerror("psql:%s: Unknown type %d in col '%s' of table '%s'; this is probably a bug\n",
          db->name, field->type, field->name, table->schema->name);
      return -1;
    }
    paramLength[4+i] = 0;
    paramFormat[4+i] = 0;
  }
  /* Use stuff from http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING */

  res = PQexecPrepared(psqldb->conn, insert_stmt,
                       4+value_count, (const char**)paramValues,
                       (int*) &paramLength, (int*) &paramFormat, 0 );

  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
    logerror("psql:%s: INSERT INTO '%s' failed: %s\n",
        db->name, table->schema->name, PQerrorMessage(psqldb->conn));
    PQclear(res);
    return -1;
  }
  PQclear(res);

  for (i=0;i<4+value_count;i++) {
    xfree(paramValues[i]);
  }

  return 0;
}
Ejemplo n.º 9
0
/** Try to convert a string to the current type of OmlValue, and store it there.
 *
 * \param value pointer to output OmlValue
 * \param value_s input string
 * \return  0 on success, -1 otherwise
 * \see oml_value_init, oml_value_ut_from_s
 */
int
oml_value_from_s (OmlValue *value, const char *value_s)
{
    return oml_value_ut_from_s(oml_value_get_value(value), oml_value_get_type(value), value_s);
}
Ejemplo n.º 10
0
/** Deep copy an OmlValue.
 *
 * \param dst OmlValue to copy to
 * \param src OmlValue to copy
 * \return 0 on success, -1 otherwise
 * \see oml_value_init, oml_value_set, oml_value_get_value, oml_value_get_type
 */
int
oml_value_duplicate(OmlValue* dst, OmlValue* src)
{
    return oml_value_set(dst, oml_value_get_value(src), oml_value_get_type(src));
}