2011-05-03 23:00:35 +00:00
|
|
|
#ifndef PSYC_PACKET_H
|
|
|
|
|
2011-05-09 12:37:57 +00:00
|
|
|
/**
|
|
|
|
* @file psyc/packet.h
|
|
|
|
* @brief Interface for PSYC packet data structures.
|
|
|
|
*
|
|
|
|
* Packet data structures and functions for creating them are defined here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @defgroup packet Packet data structures
|
|
|
|
*
|
|
|
|
* This module contains definitions of packet data structures and functions for
|
|
|
|
* creating them.
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
#include <psyc.h>
|
|
|
|
#include <psyc/syntax.h>
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Modifier flags. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Modifier needs to be checked if it needs length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_MODIFIER_CHECK_LENGTH = 0,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Modifier needs length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_MODIFIER_NEED_LENGTH = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Modifier doesn't need length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_MODIFIER_NO_LENGTH = 2,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Routing modifier, which implies that it doesn't need length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_MODIFIER_ROUTING = 3,
|
|
|
|
} psycModifierFlag;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** List flags. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
/// List needs to be checked if it needs length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_LIST_CHECK_LENGTH = 0,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// List needs length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_LIST_NEED_LENGTH = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// List doesn't need length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_LIST_NO_LENGTH = 2,
|
|
|
|
} psycListFlag;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Packet flags. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Packet needs to be checked if it needs content length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_PACKET_CHECK_LENGTH = 0,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Packet needs content length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_PACKET_NEED_LENGTH = 1,
|
2011-05-09 14:32:39 +00:00
|
|
|
/// Packet doesn't need content length.
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_PACKET_NO_LENGTH = 2,
|
|
|
|
} psycPacketFlag;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for a modifier. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef struct
|
2011-05-09 15:06:15 +00:00
|
|
|
{
|
2011-05-03 23:00:35 +00:00
|
|
|
char oper;
|
|
|
|
psycString name;
|
|
|
|
psycString value;
|
|
|
|
psycModifierFlag flag;
|
|
|
|
} psycModifier;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for an entity or routing header. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t lines;
|
|
|
|
psycModifier *modifiers;
|
|
|
|
} psycHeader;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for a list. */
|
2011-05-03 23:00:35 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
size_t num_elems;
|
|
|
|
psycString *elems;
|
|
|
|
size_t length;
|
|
|
|
psycListFlag flag;
|
|
|
|
} psycList;
|
|
|
|
|
|
|
|
/** intermediate struct for a PSYC packet */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
psycHeader routing; ///< Routing header.
|
|
|
|
psycHeader entity; ///< Entity header.
|
2011-05-09 14:32:39 +00:00
|
|
|
psycString method; ///< Contains the method.
|
|
|
|
psycString data; ///< Contains the data.
|
|
|
|
psycString content; ///< Contains the whole content.
|
2011-05-03 23:00:35 +00:00
|
|
|
size_t routingLength; ///< Length of routing part.
|
|
|
|
size_t contentLength; ///< Length of content part.
|
|
|
|
size_t length; ///< Total length of packet.
|
2011-05-09 14:32:39 +00:00
|
|
|
psycPacketFlag flag; ///< Packet flag.
|
2011-05-03 23:00:35 +00:00
|
|
|
} psycPacket;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Check if a modifier needs length. */
|
2011-05-03 23:00:35 +00:00
|
|
|
static inline
|
|
|
|
psycModifierFlag psyc_checkModifierLength (psycModifier *m)
|
|
|
|
{
|
|
|
|
psycModifierFlag flag;
|
|
|
|
|
|
|
|
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
|
|
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
|
|
else if (memchr(m->value.ptr, (int)'\n', m->value.length))
|
|
|
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
|
|
else
|
|
|
|
flag = PSYC_MODIFIER_NO_LENGTH;
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new modifier. */
|
2011-05-03 23:00:35 +00:00
|
|
|
static inline
|
|
|
|
psycModifier psyc_newModifier (char oper, psycString *name, psycString *value,
|
|
|
|
psycModifierFlag flag)
|
|
|
|
{
|
|
|
|
psycModifier m = {oper, *name, *value, flag};
|
|
|
|
|
|
|
|
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
|
|
|
m.flag = psyc_checkModifierLength(&m);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create new modifier */
|
|
|
|
static inline
|
|
|
|
psycModifier psyc_newModifier2 (char oper,
|
|
|
|
const char *name, size_t namelen,
|
|
|
|
const char *value, size_t valuelen,
|
|
|
|
psycModifierFlag flag)
|
|
|
|
{
|
|
|
|
psycString n = {namelen, name};
|
|
|
|
psycString v = {valuelen, value};
|
|
|
|
|
|
|
|
return psyc_newModifier(oper, &n, &v, flag);
|
|
|
|
}
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Get the total length of a modifier when rendered. */
|
2011-05-03 23:00:35 +00:00
|
|
|
size_t psyc_getModifierLength (psycModifier *m);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Check if a list needs length. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycListFlag psyc_checkListLength (psycList *list);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Get the total length of a list when rendered. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycListFlag psyc_getListLength (psycList *list);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Check if a packet needs length. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycPacketFlag psyc_checkPacketLength (psycPacket *p);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Calculate and set the rendered length of packet parts and total packet length. */
|
2011-05-03 23:00:35 +00:00
|
|
|
size_t psyc_setPacketLength (psycPacket *p);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new list. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycList psyc_newList (psycString *elems, size_t num_elems, psycListFlag flag);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new packet. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycPacket psyc_newPacket (psycHeader *routing,
|
|
|
|
psycHeader *entity,
|
|
|
|
psycString *method, psycString *data,
|
|
|
|
psycPacketFlag flag);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new packet. */
|
2011-05-03 23:00:35 +00:00
|
|
|
psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen,
|
|
|
|
psycModifier *entity, size_t entitylen,
|
|
|
|
const char *method, size_t methodlen,
|
|
|
|
const char *data, size_t datalen,
|
|
|
|
psycPacketFlag flag);
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new packet with raw content. */
|
2011-05-05 22:18:25 +00:00
|
|
|
psycPacket psyc_newRawPacket (psycHeader *routing, psycString *content,
|
|
|
|
psycPacketFlag flag);
|
2011-05-05 22:13:37 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Create new packet with raw content. */
|
2011-05-05 22:18:25 +00:00
|
|
|
psycPacket psyc_newRawPacket2 (psycModifier *routing, size_t routinglen,
|
|
|
|
const char *content, size_t contentlen,
|
|
|
|
psycPacketFlag flag);
|
2011-05-05 22:13:37 +00:00
|
|
|
|
2011-05-09 12:37:57 +00:00
|
|
|
/** @} */ // end of packet group
|
|
|
|
|
2011-05-09 07:02:15 +00:00
|
|
|
#define PSYC_PACKET_H
|
|
|
|
#endif
|