2011-05-03 23:00:35 +00:00
|
|
|
#ifndef PSYC_PACKET_H
|
2011-11-11 21:18:24 +00:00
|
|
|
#define PSYC_PACKET_H
|
2011-05-03 23:00:35 +00:00
|
|
|
|
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-10-31 19:04:16 +00:00
|
|
|
#include <math.h>
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-30 12:51:50 +00:00
|
|
|
#include "syntax.h"
|
|
|
|
#include "method.h"
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Modifier flags. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef enum {
|
|
|
|
/// Modifier needs to be checked if it needs length.
|
|
|
|
PSYC_MODIFIER_CHECK_LENGTH = 0,
|
|
|
|
/// Modifier needs length.
|
|
|
|
PSYC_MODIFIER_NEED_LENGTH = 1,
|
|
|
|
/// Modifier doesn't need length.
|
|
|
|
PSYC_MODIFIER_NO_LENGTH = 2,
|
|
|
|
/// Routing modifier, which implies that it doesn't need length.
|
|
|
|
PSYC_MODIFIER_ROUTING = 3,
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycModifierFlag;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** List flags. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef enum {
|
|
|
|
/// List needs to be checked if it needs length.
|
|
|
|
PSYC_LIST_CHECK_LENGTH = 0,
|
|
|
|
/// List needs length.
|
|
|
|
PSYC_LIST_NEED_LENGTH = 1,
|
|
|
|
/// List doesn't need length.
|
|
|
|
PSYC_LIST_NO_LENGTH = 2,
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycListFlag;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Packet flags. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef enum {
|
|
|
|
/// Packet needs to be checked if it needs content length.
|
|
|
|
PSYC_PACKET_CHECK_LENGTH = 0,
|
|
|
|
/// Packet needs content length.
|
|
|
|
PSYC_PACKET_NEED_LENGTH = 1,
|
|
|
|
/// Packet doesn't need content length.
|
|
|
|
PSYC_PACKET_NO_LENGTH = 2,
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycPacketFlag;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef enum {
|
|
|
|
PSYC_OPERATOR_SET = ':',
|
|
|
|
PSYC_OPERATOR_ASSIGN = '=',
|
|
|
|
PSYC_OPERATOR_AUGMENT = '+',
|
|
|
|
PSYC_OPERATOR_DIMINISH = '-',
|
2011-11-21 15:00:46 +00:00
|
|
|
PSYC_OPERATOR_UPDATE = '@',
|
2011-11-11 21:18:24 +00:00
|
|
|
PSYC_OPERATOR_QUERY = '?',
|
2011-11-09 18:01:05 +00:00
|
|
|
} PsycOperator;
|
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef enum {
|
|
|
|
PSYC_STATE_NOOP = 0,
|
|
|
|
PSYC_STATE_RESET = '=',
|
|
|
|
PSYC_STATE_RESYNC = '?',
|
2011-11-09 20:22:58 +00:00
|
|
|
} PsycStateOp;
|
|
|
|
|
2011-12-28 22:45:16 +00:00
|
|
|
typedef enum {
|
|
|
|
PSYC_PACKET_ID_CONTEXT = 0,
|
|
|
|
PSYC_PACKET_ID_SOURCE = 1,
|
|
|
|
PSYC_PACKET_ID_TARGET = 2,
|
|
|
|
PSYC_PACKET_ID_COUNTER = 3,
|
|
|
|
PSYC_PACKET_ID_FRAGMENT = 4,
|
|
|
|
PSYC_PACKET_ID_ELEMS = 5,
|
|
|
|
} PsycPacketId;
|
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for a modifier. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef struct {
|
|
|
|
char oper;
|
|
|
|
PsycString name;
|
|
|
|
PsycString value;
|
|
|
|
PsycModifierFlag flag;
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycModifier;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for an entity or routing header. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef struct {
|
|
|
|
size_t lines;
|
|
|
|
PsycModifier *modifiers;
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycHeader;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-09 14:32:39 +00:00
|
|
|
/** Structure for a list. */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef struct {
|
|
|
|
size_t num_elems;
|
|
|
|
PsycString *elems;
|
|
|
|
size_t length;
|
|
|
|
PsycListFlag flag;
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycList;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-26 14:03:10 +00:00
|
|
|
typedef struct {
|
|
|
|
PsycList *list;
|
|
|
|
size_t width;
|
|
|
|
size_t length;
|
|
|
|
} PsycTable;
|
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
/** Intermediate struct for a PSYC packet */
|
2011-11-11 21:18:24 +00:00
|
|
|
typedef struct {
|
|
|
|
PsycHeader routing; ///< Routing header.
|
|
|
|
PsycHeader entity; ///< Entity header.
|
|
|
|
PsycString method; ///< Contains the method.
|
|
|
|
PsycString data; ///< Contains the data.
|
|
|
|
PsycString content; ///< Contains the whole content.
|
2011-11-21 15:00:46 +00:00
|
|
|
size_t routinglen; ///< Length of routing part.
|
|
|
|
size_t contentlen; ///< Length of content part.
|
2011-11-11 21:18:24 +00:00
|
|
|
size_t length; ///< Total length of packet.
|
2011-11-21 15:00:46 +00:00
|
|
|
PsycStateOp stateop; ///< State operation. @see PsycStateOp
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycPacketFlag flag; ///< Packet flag.
|
2011-10-31 19:26:47 +00:00
|
|
|
} PsycPacket;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
/**
|
|
|
|
* Return the number of digits a number has in its base 10 representation.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
static inline size_t
|
|
|
|
psyc_num_length (size_t n)
|
2011-10-31 19:04:16 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
return n < 10 ? 1 : log10(n) + 1;
|
2011-10-31 19:04:16 +00:00
|
|
|
}
|
|
|
|
|
2011-05-28 18:24:36 +00:00
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Check if a modifier needs length.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
static inline PsycModifierFlag
|
|
|
|
psyc_modifier_length_check (PsycModifier *m)
|
2011-05-03 23:00:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycModifierFlag flag;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
|
|
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
|
|
else if (memchr(m->value.data, (int) '\n', m->value.length))
|
|
|
|
flag = PSYC_MODIFIER_NEED_LENGTH;
|
|
|
|
else
|
|
|
|
flag = PSYC_MODIFIER_NO_LENGTH;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
return flag;
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 13:27:01 +00:00
|
|
|
/** Initialize modifier */
|
2011-11-11 21:18:24 +00:00
|
|
|
static inline void
|
2011-12-01 13:12:41 +00:00
|
|
|
psyc_modifier_init (PsycModifier *m, PsycOperator oper,
|
2011-11-11 21:18:24 +00:00
|
|
|
char *name, size_t namelen,
|
|
|
|
char *value, size_t valuelen, PsycModifierFlag flag)
|
2011-11-01 21:24:35 +00:00
|
|
|
{
|
2011-11-11 21:18:24 +00:00
|
|
|
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-11 21:18:24 +00:00
|
|
|
if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length
|
|
|
|
m->flag = psyc_modifier_length_check(m);
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
|
2011-05-28 18:29:19 +00:00
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Get the total length of a modifier when rendered.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
size_t
|
|
|
|
psyc_modifier_length (PsycModifier *m);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-28 18:24:36 +00:00
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Check if a list needs length.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycListFlag
|
|
|
|
psyc_list_length_check (PsycList *list);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-28 18:29:19 +00:00
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Get the total length of a list when rendered.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycListFlag
|
|
|
|
psyc_list_length (PsycList *list);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-28 18:24:36 +00:00
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Check if a packet needs length.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
PsycPacketFlag
|
|
|
|
psyc_packet_length_check (PsycPacket *p);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-28 18:29:19 +00:00
|
|
|
/**
|
|
|
|
* Calculate and set the rendered length of packet parts and total packet length.
|
|
|
|
*/
|
2011-11-11 21:18:24 +00:00
|
|
|
size_t
|
|
|
|
psyc_packet_length_set (PsycPacket *p);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-11-26 14:03:10 +00:00
|
|
|
/** Initialize a list. */
|
2011-11-11 21:18:24 +00:00
|
|
|
void
|
|
|
|
psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
|
|
|
|
PsycListFlag flag);
|
2011-11-03 13:27:01 +00:00
|
|
|
|
2011-11-26 14:03:10 +00:00
|
|
|
/** Initialize a table. */
|
|
|
|
void
|
|
|
|
psyc_table_init (PsycTable *table, size_t width, PsycList *list);
|
|
|
|
|
|
|
|
/** Initialize a packet. */
|
2011-11-11 21:18:24 +00:00
|
|
|
void
|
|
|
|
psyc_packet_init (PsycPacket *packet,
|
|
|
|
PsycModifier *routing, size_t routinglen,
|
|
|
|
PsycModifier *entity, size_t entitylen,
|
|
|
|
char *method, size_t methodlen,
|
|
|
|
char *data, size_t datalen,
|
|
|
|
char stateop, PsycPacketFlag flag);
|
2011-11-03 13:27:01 +00:00
|
|
|
|
|
|
|
/** Initialize packet with raw content. */
|
2011-11-11 21:18:24 +00:00
|
|
|
void
|
|
|
|
psyc_packet_init_raw (PsycPacket *packet,
|
|
|
|
PsycModifier *routing, size_t routinglen,
|
|
|
|
char *content, size_t contentlen,
|
|
|
|
PsycPacketFlag flag);
|
2011-05-05 22:13:37 +00:00
|
|
|
|
2011-12-28 22:45:16 +00:00
|
|
|
/** Get the total length of a packet ID when rendered. */
|
|
|
|
size_t
|
|
|
|
psyc_packet_id_length (size_t contextlen, size_t sourcelen, size_t targetelen,
|
|
|
|
size_t counterlen, size_t fragmentlen);
|
|
|
|
|
2011-05-09 12:37:57 +00:00
|
|
|
/** @} */ // end of packet group
|
|
|
|
|
2011-05-09 07:02:15 +00:00
|
|
|
#endif
|