diff --git a/.gitignore b/.gitignore index 83138ed..abeb59a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/include/psyc.h b/include/psyc.h index 9a1a7bb..41ace37 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -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 diff --git a/include/psyc/method.h b/include/psyc/method.h index cb41300..9bbac06 100644 --- a/include/psyc/method.h +++ b/include/psyc/method.h @@ -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, diff --git a/include/psyc/packet.h b/include/psyc/packet.h index 7eecd0c..4f37750 100644 --- a/include/psyc/packet.h +++ b/include/psyc/packet.h @@ -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 diff --git a/include/psyc/render.h b/include/psyc/render.h index 4a826db..7563ece 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -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 diff --git a/src/packet.c b/src/packet.c index c61ee8d..17bd207 100644 --- a/src/packet.c +++ b/src/packet.c @@ -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; +} diff --git a/src/render.c b/src/render.c index 0db289d..f0a93fe 100644 --- a/src/render.c +++ b/src/render.c @@ -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); +} diff --git a/test/Makefile b/test/Makefile index ba570c3..123de1b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 diff --git a/test/test_packet_id.c b/test/test_packet_id.c new file mode 100644 index 0000000..c42de6e --- /dev/null +++ b/test/test_packet_id.c @@ -0,0 +1,70 @@ +#include +#include + +#include +#include +#include + +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; +}