/* lib/dnode.h * * These are the data nodes that the DFS uses. * */ #ifndef DNODE_H_ #define DNODE_H_ #ifdef __cplusplus extern "C" { #endif /* ----- Includes ----- */ #include "lib.h" #include "list.h" #include "uuid.h" #include "enchufe.h" /* ---- Macros ----- */ #define DNODE_TABLE "data/dnode.bin" /* ----- Types ----- */ /// Saves the connection data. typedef struct { UUID nid; ///< DNode ID (primary key) IPv4 address; ///< Address of where the connection came from Port port; ///< Port of where the connection came from } DNode; /// This carries all the necessary information that a chunk should have. typedef struct { DNode dnode; ///< the DNode of the chunk UUID cid; ///< The UUID of the chunk size_t seq; ///< The place in the sequence of chunks where this chunk should be } DNodeInfo; /* ----- Functions ----- */ // Define List for data nodes DefList(DNode) // Define List for data node infos DefList(DNodeInfo) /// Serialize a DNode into a Buffer. /// Caller must free memory in Buffer.buf. /// /// @param dnode the DNode you want to serialize /// @return the Buffer that the DNode represents Buffer DNode_serialize(DNode dnode); /// Deserialize a Buffer into a DNode. /// Caller must free memory in DNode.address. /// /// @param buffer The Buffer you want to deserialize /// @return The DNode that the Buffer represents DNode DNode_deserialize(Buffer buf); /// Serialize a List of DNode's into a Buffer. /// Caller must free memory in Buffer.buf. /// /// @param list the List of DNode's that you want to serialize /// @return the Buffer that the ListDNode represents Buffer ListDNode_serialize(ListDNode list); /// Deserialize a Buffer into a List of DNode's. /// Caller must free memory in DNode.address. /// /// @param buffer The Buffer you want to deserialize /// @return The List of DNode's that the Buffer represents ListDNode ListDNode_deserialize(Buffer buf); /// Serialize a DNodeInfo to a buffer /// /// @param dinfo the DNodeInfo you want to serialize /// @return the Buffer thet tha DNodeInfo represents Buffer DNodeInfo_serialize(DNodeInfo dinfo); /// Deserialize a Buffer into a List of DNodeInfo's. /// Caller must free memory in DNodeInfo.address. /// /// @param buffer The Buffer you want to deserialize /// @return The List of DNodeInfo's that the Buffer represents DNodeInfo DNodeInfo_deserialize(Buffer buf); /// Serialize a ListDNodeInfo to a buffer /// /// @param dinfo the ListDNodeInfo you want to serialize /// @return the Buffer thet tha ListDNodeInfo represents Buffer ListDNodeInfo_serialize(ListDNodeInfo list); /// Deserialize a Buffer into a List of DNodeInfo's. /// Caller must free memory in DNodeInfo.address. /// /// @param buffer The Buffer you want to deserialize /// @return The List of DNodeInfo's that the Buffer represents ListDNodeInfo ListDNodeInfo_deserialize(Buffer buf); /// INSERT into the DNode table. /// /// @param address the URI address /// @param port the port of the connection void insert_into_dnode(IPv4 address, Port port); /// SELECT from the DNode table. /// /// @param nid the DNode's ID or NULL /// @param address the host's address or PULL /// @param port the host's Port or NULL /// @return a ListDNode of the possible matches ListDNode select_from_dnode(UUID* nid, IPv4* address, Port* port); /// Get the DNode table from the database. /// /// @return a List of DNode's ListDNode DNode_get_db(void); /// Write the database with the List of DNode's. void DNode_set_db(ListDNode dnodes); /// Print a DNode's data as a tuple. /// /// @param dnode the DNode to be printed void print_dnode(DNode dnode); /// Insert a DNodeInfo into a ListDNodeInfo in a sorted fashion. This is really /// just a helper function for sort(ListDNodeInfo l). Here is a Haskell /// implementation of it: /// \code{.hs} /// insert :: [Int] -> Int -> [Int] /// insert [] a = [a] /// insert (x:xs) a = if x > a then a:x:xs else x:insert xs a /// \endcode /// /// @param l the ListDNodeInfo that you want to insert into /// @param a the DNodeInfo that you want to insert /// @return a ListDNodeInfo with a placed depending on its sequence number ListDNodeInfo insert(ListDNodeInfo l, DNodeInfo a); /// Sort a ListDNodeInfo depending on the sequence numbers of its entries. /// \code{.hs} /// sort :: [Int] -> [Int] /// sort = foldl insert [] /// \endcode /// /// @param l the ListDNodeInfo you want to sort /// @return a sorted version of l ListDNodeInfo sort(ListDNodeInfo l); #ifdef __cplusplus } #endif #endif // DNODE_H_