Code

proto: Added support for marshaling attribute objects.
[sysdb.git] / src / utils / proto.c
index f543dd665cab71b56fe97f0f8f767ead5dfbc6e6..cd4e050969e6dab401742adb368ca59ec657c007 100644 (file)
@@ -69,8 +69,8 @@ marshal_int64(char *buf, size_t buf_len, int64_t v)
 {
        if (buf_len >= sizeof(v)) {
 #if __BYTE_ORDER != __BIG_ENDIAN
-               v = (((int64_t)ntohl((int32_t)v)) << 32)
-                       + ((int64_t)ntohl((int32_t)(v >> 32)));
+               v = (((int64_t)htonl((int32_t)v)) << 32)
+                       + ((int64_t)htonl((int32_t)(v >> 32)));
 #endif
                memcpy(buf, &v, sizeof(v));
        }
@@ -84,8 +84,8 @@ marshal_double(char *buf, size_t buf_len, double v)
        assert(sizeof(v) == sizeof(t));
        memcpy(&t, &v, sizeof(v));
 #if IEEE754_DOUBLE_BYTE_ORDER != IEEE754_DOUBLE_BIG_ENDIAN
-       t = (((int64_t)ntohl((int32_t)t)) << 32)
-               + ((int64_t)ntohl((int32_t)(t >> 32)));
+       t = (((int64_t)htonl((int32_t)t)) << 32)
+               + ((int64_t)htonl((int32_t)(t >> 32)));
 #endif
        if (buf_len >= sizeof(t))
                memcpy(buf, &t, sizeof(t));
@@ -162,7 +162,7 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
 } /* sdb_proto_marshal */
 
 ssize_t
-sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
+sdb_proto_marshal_data(char *buf, size_t buf_len, const sdb_data_t *datum)
 {
        ssize_t len = 0, n = 0;
        uint32_t tmp;
@@ -339,6 +339,46 @@ sdb_proto_marshal_metric(char *buf, size_t buf_len,
        return len;
 } /* sdb_proto_marshal_metric */
 
+ssize_t
+sdb_proto_marshal_attribute(char *buf, size_t buf_len,
+               const sdb_proto_attribute_t *attr)
+{
+       size_t len;
+       ssize_t n;
+
+       if ((! attr) || (! attr->parent) || (! attr->key) || (! attr->value)
+                       || ((attr->parent_type != SDB_HOST) && (! attr->hostname))
+                       || ((attr->parent_type != SDB_HOST)
+                               && (attr->parent_type != SDB_SERVICE)
+                               && (attr->parent_type != SDB_METRIC)))
+               return -1;
+
+       n = sdb_proto_marshal_data(NULL, 0, attr->value);
+       if (n < 0)
+               return -1;
+
+       len = OBJ_HEADER_LEN
+               + strlen(attr->parent) + strlen(attr->key) + 2 + (size_t)n;
+       if (attr->parent_type != SDB_HOST)
+               len += strlen(attr->hostname) + 1;
+       if (buf_len < len)
+               return len;
+
+       n = marshal_obj_header(buf, buf_len,
+                       attr->parent_type | SDB_ATTRIBUTE, attr->last_update);
+       buf += n; buf_len -= n;
+       if (attr->parent_type != SDB_HOST) {
+               n = marshal_string(buf, buf_len, attr->hostname);
+               buf += n; buf_len -= n;
+       }
+       n = marshal_string(buf, buf_len, attr->parent);
+       buf += n; buf_len -= n;
+       n = marshal_string(buf, buf_len, attr->key);
+       buf += n; buf_len -= n;
+       sdb_proto_marshal_data(buf, buf_len, attr->value);
+       return len;
+} /* sdb_proto_marshal_attribute */
+
 int
 sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
                uint32_t *code, uint32_t *msg_len)