mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
added support for state sync/reset packets
This commit is contained in:
parent
8a9ef74707
commit
d2780c0dbf
8 changed files with 47 additions and 27 deletions
|
@ -109,7 +109,6 @@ void * psyc_dict_lookup (const PsycDict *dict, size_t size,
|
||||||
{
|
{
|
||||||
size_t cursor = 1;
|
size_t cursor = 1;
|
||||||
uint8_t i, m = 0;
|
uint8_t i, m = 0;
|
||||||
//memset(&matching, -1, sizeof matching);
|
|
||||||
|
|
||||||
if (keylen < 2 || key[0] != '_')
|
if (keylen < 2 || key[0] != '_')
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -63,9 +63,9 @@ void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
|
||||||
inline
|
inline
|
||||||
size_t psyc_modifier_length (PsycModifier *m)
|
size_t psyc_modifier_length (PsycModifier *m)
|
||||||
{
|
{
|
||||||
size_t length = 1 + // oper
|
size_t length = 2; // oper\n
|
||||||
m->name.length + 1 + // name\t
|
if (m->name.length > 0)
|
||||||
m->value.length + 1; // value\n
|
length += m->name.length + 1 + m->value.length; // name\tvalue
|
||||||
|
|
||||||
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed
|
||||||
length += psyc_num_length(m->value.length) + 1; // SP length
|
length += psyc_num_length(m->value.length) + 1; // SP length
|
||||||
|
|
18
src/parse.c
18
src/parse.c
|
@ -21,7 +21,7 @@ typedef enum {
|
||||||
PARSE_INSUFFICIENT = 1,
|
PARSE_INSUFFICIENT = 1,
|
||||||
PARSE_COMPLETE = 100,
|
PARSE_COMPLETE = 100,
|
||||||
PARSE_INCOMPLETE = 101,
|
PARSE_INCOMPLETE = 101,
|
||||||
} parseRC;
|
} ParseRC;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse variable name or method name.
|
* Parse variable name or method name.
|
||||||
|
@ -29,7 +29,7 @@ typedef enum {
|
||||||
* @return PARSE_ERROR or PARSE_SUCCESS
|
* @return PARSE_ERROR or PARSE_SUCCESS
|
||||||
*/
|
*/
|
||||||
static inline
|
static inline
|
||||||
parseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
|
ParseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
|
||||||
{
|
{
|
||||||
name->data = state->buffer.data + state->cursor;
|
name->data = state->buffer.data + state->cursor;
|
||||||
name->length = 0;
|
name->length = 0;
|
||||||
|
@ -54,7 +54,7 @@ parseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
|
||||||
* @return PARSE_COMPLETE or PARSE_INCOMPLETE
|
* @return PARSE_COMPLETE or PARSE_INCOMPLETE
|
||||||
*/
|
*/
|
||||||
static inline
|
static inline
|
||||||
parseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
ParseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
||||||
size_t *length, size_t *parsed)
|
size_t *length, size_t *parsed)
|
||||||
{
|
{
|
||||||
size_t remaining = *length - *parsed;
|
size_t remaining = *length - *parsed;
|
||||||
|
@ -81,13 +81,16 @@ parseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
||||||
* @return PARSE_ERROR or PARSE_SUCCESS
|
* @return PARSE_ERROR or PARSE_SUCCESS
|
||||||
*/
|
*/
|
||||||
static inline
|
static inline
|
||||||
parseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
||||||
PsycString *name, PsycString *value)
|
PsycString *name, PsycString *value)
|
||||||
{
|
{
|
||||||
*oper = *(state->buffer.data + state->cursor);
|
*oper = *(state->buffer.data + state->cursor);
|
||||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||||
|
|
||||||
parseRC ret = psyc_parse_keyword(state, name);
|
if (state->part == PSYC_PART_CONTENT && state->buffer.data[state->cursor] == '\n')
|
||||||
|
return PARSE_SUCCESS; // only oper is present, used for state sync/reset
|
||||||
|
|
||||||
|
ParseRC ret = psyc_parse_keyword(state, name);
|
||||||
if (ret == PARSE_ERROR)
|
if (ret == PARSE_ERROR)
|
||||||
return PSYC_PARSE_ERROR_MOD_NAME;
|
return PSYC_PARSE_ERROR_MOD_NAME;
|
||||||
else if (ret != PARSE_SUCCESS)
|
else if (ret != PARSE_SUCCESS)
|
||||||
|
@ -162,7 +165,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
||||||
PP(("Invalid flag combination"))
|
PP(("Invalid flag combination"))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
parseRC ret; // a return value
|
ParseRC ret; // a return value
|
||||||
size_t pos = state->cursor; // a cursor position
|
size_t pos = state->cursor; // a cursor position
|
||||||
|
|
||||||
// Start position of the current line in the buffer
|
// Start position of the current line in the buffer
|
||||||
|
@ -326,8 +329,6 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
||||||
{
|
{
|
||||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fall thru
|
|
||||||
}
|
}
|
||||||
else // No method, which means the packet should end now.
|
else // No method, which means the packet should end now.
|
||||||
{
|
{
|
||||||
|
@ -335,6 +336,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
||||||
state->startc = state->cursor;
|
state->startc = state->cursor;
|
||||||
goto PSYC_PART_END;
|
goto PSYC_PART_END;
|
||||||
}
|
}
|
||||||
|
// fall thru
|
||||||
|
|
||||||
case PSYC_PART_DATA:
|
case PSYC_PART_DATA:
|
||||||
PSYC_PART_DATA:
|
PSYC_PART_DATA:
|
||||||
|
|
|
@ -46,12 +46,12 @@ static inline
|
||||||
size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
|
size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
|
||||||
{
|
{
|
||||||
size_t cur = 0;
|
size_t cur = 0;
|
||||||
|
|
||||||
buffer[cur++] = mod->oper;
|
buffer[cur++] = mod->oper;
|
||||||
|
|
||||||
|
if (mod->name.length > 0)
|
||||||
|
{
|
||||||
memcpy(buffer + cur, mod->name.data, mod->name.length);
|
memcpy(buffer + cur, mod->name.data, mod->name.length);
|
||||||
cur += mod->name.length;
|
cur += mod->name.length;
|
||||||
if (cur <= 1)
|
|
||||||
return cur; // error, name can't be empty
|
|
||||||
|
|
||||||
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||||
{
|
{
|
||||||
|
@ -62,8 +62,9 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
|
||||||
buffer[cur++] = '\t';
|
buffer[cur++] = '\t';
|
||||||
memcpy(buffer + cur, mod->value.data, mod->value.length);
|
memcpy(buffer + cur, mod->value.data, mod->value.length);
|
||||||
cur += mod->value.length;
|
cur += mod->value.length;
|
||||||
buffer[cur++] = '\n';
|
}
|
||||||
|
|
||||||
|
buffer[cur++] = '\n';
|
||||||
return cur;
|
return cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PSYC_UNIFORM_PORT:
|
case PSYC_UNIFORM_PORT:
|
||||||
if (psyc_is_numeric(c)) {
|
if (psyc_is_numeric(c) || (uni->port.length == 0 && c == '-')) {
|
||||||
uni->port.length++;
|
uni->port.length++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
6
test/packets/00-no-body
Normal file
6
test/packets/00-no-body
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
:_source psyc://foo.example.com/
|
||||||
|
:_target psyc://bar.example.com/
|
||||||
|
|
||||||
|
:_foo bar
|
||||||
|
:_baz qux
|
||||||
|
|
|
7
test/packets/00-state-reset
Normal file
7
test/packets/00-state-reset
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
:_source psyc://foo.example.com/
|
||||||
|
:_target psyc://bar.example.com/
|
||||||
|
|
||||||
|
=
|
||||||
|
=_foo bar
|
||||||
|
=_baz qux
|
||||||
|
|
|
5
test/packets/00-state-sync
Normal file
5
test/packets/00-state-sync
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
:_source psyc://foo.example.com/
|
||||||
|
:_target psyc://bar.example.com/
|
||||||
|
|
||||||
|
?
|
||||||
|
|
|
Loading…
Reference in a new issue