1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

packet id

This commit is contained in:
tg(x) 2011-12-28 23:45:16 +01:00
parent 07901658f3
commit 30351302bc
9 changed files with 137 additions and 1 deletions

1
.gitignore vendored
View file

@ -18,6 +18,7 @@ test/test_json_glib
test/test_strlen
test/test_text
test/test_table
test/test_packet_id
test/var_is_routing
test/var_type
test/uniform_parse

View file

@ -164,6 +164,13 @@ psyc_dict_lookup_int (const PsycDictInt * dict, size_t size,
inherit, tmp);
}
#include "psyc/syntax.h"
#include "psyc/method.h"
#include "psyc/packet.h"
#include "psyc/parse.h"
#include "psyc/render.h"
#include "psyc/text.h"
#include "psyc/uniform.h"
#include "psyc/variable.h"
#endif

View file

@ -26,6 +26,7 @@ typedef enum PsycMethod {
PSYC_MC_NOTICE,
PSYC_MC_NOTICE_CONTEXT_ENTER,
PSYC_MC_NOTICE_CONTEXT_LEAVE,
PSYC_MC_NOTICE_HELLO,
PSYC_MC_REQUEST,
PSYC_MC_REQUEST_CONTEXT_ENTER,
PSYC_MC_REQUEST_CONTEXT_LEAVE,

View file

@ -68,6 +68,15 @@ typedef enum {
PSYC_STATE_RESYNC = '?',
} PsycStateOp;
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;
/** Structure for a modifier. */
typedef struct {
char oper;
@ -209,6 +218,11 @@ psyc_packet_init_raw (PsycPacket *packet,
char *content, size_t contentlen,
PsycPacketFlag flag);
/** 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);
/** @} */ // end of packet group
#endif

View file

@ -63,6 +63,14 @@ psyc_render_list (PsycList *list, char *buffer, size_t buflen);
PsycRenderRC
psyc_render_table (PsycTable *table, char *buffer, size_t buflen);
PsycRenderRC
psyc_render_packet_id (char *context, size_t contextlen,
char *source, size_t sourcelen,
char *target, size_t targetlen,
char *counter, size_t counterlen,
char *fragment, size_t fragmentlen,
char *buffer, size_t buflen);
/** @} */ // end of render group
#endif

View file

@ -197,3 +197,10 @@ psyc_packet_init_raw (PsycPacket *p,
psyc_packet_length_set(p);
}
size_t
psyc_packet_id_length (size_t contextlen, size_t sourcelen, size_t targetlen,
size_t counterlen, size_t fragmentlen)
{
return contextlen + sourcelen + targetlen + counterlen + fragmentlen + 5;
}

View file

@ -146,3 +146,29 @@ psyc_render (PsycPacket * packet, char *buffer, size_t buflen)
assert(cur == packet->length);
return PSYC_RENDER_SUCCESS;
}
PsycRenderRC
psyc_render_packet_id (char *context, size_t contextlen,
char *source, size_t sourcelen,
char *target, size_t targetlen,
char *counter, size_t counterlen,
char *fragment, size_t fragmentlen,
char *buffer, size_t buflen)
{
PsycList list;
PsycString elems[PSYC_PACKET_ID_ELEMS] = {};
if (contextlen)
elems[PSYC_PACKET_ID_CONTEXT] = PSYC_STRING(context, contextlen);
if (sourcelen)
elems[PSYC_PACKET_ID_SOURCE] = PSYC_STRING(source, sourcelen);
if (targetlen)
elems[PSYC_PACKET_ID_TARGET] = PSYC_STRING(target, targetlen);
if (counterlen)
elems[PSYC_PACKET_ID_COUNTER] = PSYC_STRING(counter, counterlen);
if (fragmentlen)
elems[PSYC_PACKET_ID_FRAGMENT] = PSYC_STRING(fragment, fragmentlen);
psyc_list_init(&list, elems, PSYC_PACKET_ID_ELEMS, PSYC_LIST_NO_LENGTH);
return psyc_render_list(&list, buffer, buflen);
}

View file

@ -3,7 +3,7 @@ DEBUG = 2
CFLAGS = -I../include -I../src -Wall -std=c99 ${OPT}
LDFLAGS = -L../lib
LOADLIBES = -lpsyc -lm
TARGETS = test_psyc test_psyc_speed test_parser test_match test_render test_text var_is_routing var_type uniform_parse test_table
TARGETS = test_psyc test_psyc_speed test_parser test_match test_render test_text var_is_routing var_type uniform_parse test_list test_table test_packet_id
O = test.o
WRAPPER =
DIET = diet
@ -48,7 +48,9 @@ test: ${TARGETS}
./var_is_routing
./var_type
./uniform_parse
./test_list
./test_table
./test_packet_id
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -f $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x
x=0; for f in packets/[0-9]*; do echo ">> $$f"; ./test_psyc -rf $$f | ${DIFF} -u $$f -; x=$$((x+$$?)); done; exit $$x

70
test/test_packet_id.c Normal file
View file

@ -0,0 +1,70 @@
#include <stdlib.h>
#include <stdio.h>
#include <psyc.h>
#include <psyc/packet.h>
#include <psyc/render.h>
int
packet_id (char *context, size_t contextlen,
char *source, size_t sourcelen,
char *target, size_t targetlen,
char *counter, size_t counterlen,
char *fragment, size_t fragmentlen,
char *result, size_t resultlen)
{
size_t idlen = psyc_packet_id_length(contextlen, sourcelen, targetlen,
counterlen, fragmentlen);
char *id = malloc(idlen);
psyc_render_packet_id(context, contextlen,
source, sourcelen,
target, targetlen,
counter, counterlen,
fragment, fragmentlen,
id, idlen);
printf("%.*s\n", (int)idlen, id);
int ret = idlen == resultlen && memcmp(result, id, idlen) == 0;
free(id);
return ret;
}
int
main (int argc, char **argv)
{
if (!packet_id(PSYC_C2ARG(""),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG("psyc://example.net/~bob"),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("||psyc://example.net/~alice|psyc://example.net/~bob"
"|1337|42")))
return 1;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG(""),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("|psyc://example.net/@bar|psyc://example.net/~alice|"
"|1337|42")))
return 2;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG(""),
PSYC_C2ARG("psyc://example.net/~alice"),
PSYC_C2ARG("1337"),
PSYC_C2ARG("42"),
PSYC_C2ARG("|psyc://example.net/@bar||psyc://example.net/~alice"
"|1337|42")))
return 3;
if (!packet_id(PSYC_C2ARG("psyc://example.net/@bar"),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG(""),
PSYC_C2ARG("|psyc://example.net/@bar||||")))
return 4;
return 0;
}