Distributed File System written in C
1/* lib/dnode.h
2 *
3 * These are the data nodes that the DFS uses.
4 *
5 */
6#ifndef DNODE_H_
7#define DNODE_H_
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/* ----- Includes ----- */
14
15#include "lib.h"
16#include "list.h"
17#include "uuid.h"
18#include "enchufe.h"
19
20/* ---- Macros ----- */
21
22#define DNODE_TABLE "data/dnode.bin"
23
24/* ----- Types ----- */
25
26/// Saves the connection data.
27typedef struct {
28 UUID nid; ///< DNode ID (primary key)
29 IPv4 address; ///< Address of where the connection came from
30 Port port; ///< Port of where the connection came from
31} DNode;
32
33
34/// This carries all the necessary information that a chunk should have.
35typedef struct {
36 DNode dnode; ///< the DNode of the chunk
37 UUID cid; ///< The UUID of the chunk
38 size_t seq; ///< The place in the sequence of chunks where this chunk should be
39} DNodeInfo;
40
41/* ----- Functions ----- */
42
43
44// Define List for data nodes
45DefList(DNode)
46
47// Define List for data node infos
48DefList(DNodeInfo)
49
50/// Serialize a DNode into a Buffer.
51/// Caller must free memory in Buffer.buf.
52///
53/// @param dnode the DNode you want to serialize
54/// @return the Buffer that the DNode represents
55Buffer DNode_serialize(DNode dnode);
56
57/// Deserialize a Buffer into a DNode.
58/// Caller must free memory in DNode.address.
59///
60/// @param buffer The Buffer you want to deserialize
61/// @return The DNode that the Buffer represents
62DNode DNode_deserialize(Buffer buf);
63
64/// Serialize a List of DNode's into a Buffer.
65/// Caller must free memory in Buffer.buf.
66///
67/// @param list the List of DNode's that you want to serialize
68/// @return the Buffer that the ListDNode represents
69Buffer ListDNode_serialize(ListDNode list);
70
71/// Deserialize a Buffer into a List of DNode's.
72/// Caller must free memory in DNode.address.
73///
74/// @param buffer The Buffer you want to deserialize
75/// @return The List of DNode's that the Buffer represents
76ListDNode ListDNode_deserialize(Buffer buf);
77
78/// Serialize a DNodeInfo to a buffer
79///
80/// @param dinfo the DNodeInfo you want to serialize
81/// @return the Buffer thet tha DNodeInfo represents
82Buffer DNodeInfo_serialize(DNodeInfo dinfo);
83
84/// Deserialize a Buffer into a List of DNodeInfo's.
85/// Caller must free memory in DNodeInfo.address.
86///
87/// @param buffer The Buffer you want to deserialize
88/// @return The List of DNodeInfo's that the Buffer represents
89DNodeInfo DNodeInfo_deserialize(Buffer buf);
90
91/// Serialize a ListDNodeInfo to a buffer
92///
93/// @param dinfo the ListDNodeInfo you want to serialize
94/// @return the Buffer thet tha ListDNodeInfo represents
95Buffer ListDNodeInfo_serialize(ListDNodeInfo list);
96
97/// Deserialize a Buffer into a List of DNodeInfo's.
98/// Caller must free memory in DNodeInfo.address.
99///
100/// @param buffer The Buffer you want to deserialize
101/// @return The List of DNodeInfo's that the Buffer represents
102ListDNodeInfo ListDNodeInfo_deserialize(Buffer buf);
103
104/// INSERT into the DNode table.
105///
106/// @param address the URI address
107/// @param port the port of the connection
108void insert_into_dnode(IPv4 address, Port port);
109
110/// SELECT from the DNode table.
111///
112/// @param nid the DNode's ID or NULL
113/// @param address the host's address or PULL
114/// @param port the host's Port or NULL
115/// @return a ListDNode of the possible matches
116ListDNode select_from_dnode(UUID* nid, IPv4* address, Port* port);
117
118/// Get the DNode table from the database.
119///
120/// @return a List of DNode's
121ListDNode DNode_get_db(void);
122
123
124/// Write the database with the List of DNode's.
125void DNode_set_db(ListDNode dnodes);
126
127/// Print a DNode's data as a tuple.
128///
129/// @param dnode the DNode to be printed
130void print_dnode(DNode dnode);
131
132
133/// Insert a DNodeInfo into a ListDNodeInfo in a sorted fashion. This is really
134/// just a helper function for sort(ListDNodeInfo l). Here is a Haskell
135/// implementation of it:
136/// \code{.hs}
137/// insert :: [Int] -> Int -> [Int]
138/// insert [] a = [a]
139/// insert (x:xs) a = if x > a then a:x:xs else x:insert xs a
140/// \endcode
141///
142/// @param l the ListDNodeInfo that you want to insert into
143/// @param a the DNodeInfo that you want to insert
144/// @return a ListDNodeInfo with a placed depending on its sequence number
145ListDNodeInfo insert(ListDNodeInfo l, DNodeInfo a);
146
147/// Sort a ListDNodeInfo depending on the sequence numbers of its entries.
148/// \code{.hs}
149/// sort :: [Int] -> [Int]
150/// sort = foldl insert []
151/// \endcode
152///
153/// @param l the ListDNodeInfo you want to sort
154/// @return a sorted version of l
155ListDNodeInfo sort(ListDNodeInfo l);
156
157#ifdef __cplusplus
158}
159#endif
160
161#endif // DNODE_H_