data.h
1 
21 #ifndef NDN_DATA_H
22 #define NDN_DATA_H
23 
24 #include <math.h>
25 #include <ndn-cpp/c/data-types.h>
26 #include "name.h"
27 #include "key-locator.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
39 static __inline void ndn_Signature_initialize(struct ndn_Signature *self, struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents) {
40  self->type = (ndn_SignatureType)-1;
41  ndn_Blob_initialize(&self->signature, 0, 0);
42  ndn_KeyLocator_initialize(&self->keyLocator, keyNameComponents, maxKeyNameComponents);
43 }
44 
50 static __inline void ndn_Signature_clear(struct ndn_Signature *self)
51 {
52  ndn_Signature_initialize
53  (self, self->keyLocator.keyName.components,
54  self->keyLocator.keyName.maxComponents);
55 }
56 
64 static __inline ndn_Error
65 ndn_Signature_setFromSignature
66  (struct ndn_Signature *self, const struct ndn_Signature *other)
67 {
68  ndn_Error error;
69 
70  if (other == self)
71  // Setting to itself. Do nothing.
72  return NDN_ERROR_success;
73 
74  ndn_Blob_setFromBlob(&self->signature, &other->signature);
75  if ((error = ndn_KeyLocator_setFromKeyLocator
76  (&self->keyLocator, &other->keyLocator)))
77  return error;
78 
79  return NDN_ERROR_success;
80 }
81 
86 static __inline void ndn_MetaInfo_initialize(struct ndn_MetaInfo *self)
87 {
88  self->timestampMilliseconds = -1;
89  self->type = ndn_ContentType_BLOB;
90  self->freshnessPeriod = -1;
91  ndn_NameComponent_initialize(&self->finalBlockId, 0, 0);
92 }
93 
97 static __inline int ndn_MetaInfo_getFreshnessSeconds(const struct ndn_MetaInfo *self)
98 {
99  return self->freshnessPeriod < 0 ? -1 : (int)round(self->freshnessPeriod / 1000.0);
100 }
101 
105 static __inline void ndn_MetaInfo_setFreshnessSeconds(struct ndn_MetaInfo *self, int freshnessSeconds)
106 {
107  self->freshnessPeriod = freshnessSeconds < 0 ? -1.0 : (double)freshnessSeconds * 1000.0;
108 }
109 
117 static __inline ndn_Error
118 ndn_MetaInfo_setFromMetaInfo
119  (struct ndn_MetaInfo *self, const struct ndn_MetaInfo *other)
120 {
121  if (other == self)
122  // Setting to itself. Do nothing.
123  return NDN_ERROR_success;
124 
125  self->timestampMilliseconds = other->timestampMilliseconds;
126  self->type = other->type;
127  self->freshnessPeriod = other->freshnessPeriod;
128  ndn_NameComponent_setFromNameComponent(&self->finalBlockId, &other->finalBlockId);
129 
130  return NDN_ERROR_success;
131 }
132 
142 static __inline void ndn_Data_initialize
143  (struct ndn_Data *self, struct ndn_NameComponent *nameComponents, size_t maxNameComponents,
144  struct ndn_NameComponent *keyNameComponents, size_t maxKeyNameComponents)
145 {
146  ndn_Signature_initialize(&self->signature, keyNameComponents, maxKeyNameComponents);
147  ndn_Name_initialize(&self->name, nameComponents, maxNameComponents);
148  ndn_MetaInfo_initialize(&self->metaInfo);
149  ndn_Blob_initialize(&self->content, 0, 0);
150 }
151 
159 static __inline ndn_Error
160 ndn_Data_setFromData(struct ndn_Data *self, const struct ndn_Data *other)
161 {
162  ndn_Error error;
163 
164  if (other == self)
165  // Setting to itself. Do nothing.
166  return NDN_ERROR_success;
167 
168  if ((error = ndn_Signature_setFromSignature
169  (&self->signature, &other->signature)))
170  return error;
171  if ((error = ndn_Name_setFromName(&self->name, &other->name)))
172  return error;
173  if ((error = ndn_MetaInfo_setFromMetaInfo
174  (&self->metaInfo, &other->metaInfo)))
175  return error;
176  ndn_Blob_setFromBlob(&self->content, &other->content);
177 
178  return NDN_ERROR_success;
179 }
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif
ndn_MillisecondsSince1970 timestampMilliseconds
milliseconds since 1/1/1970.
Definition: data-types.h:68
ndn_ContentType type
default is ndn_ContentType_DATA.
Definition: data-types.h:69
Definition: data-types.h:74
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:78
struct ndn_NameComponent finalBlockId
has a pointer to a pre-allocated buffer.
Definition: data-types.h:71
Copyright (C) 2015-2016 Regents of the University of California.
Definition: name-types.h:33
An ndn_MetaInfo struct holds the meta info which is signed inside the data packet.
Definition: data-types.h:67
ndn_Milliseconds freshnessPeriod
-1 for none
Definition: data-types.h:70
An ndn_Signature struct holds the signature bits and other info representing the signature in a data ...
Definition: data-types.h:58