Get first inbox item to parse completely

master
teknomunk 1 year ago
parent 65756c5795
commit 04e1ef6e32

@ -3,7 +3,8 @@
#include "http_server/http_request.h"
#include "json/json.h"
#include "model/activity.h"
#include "model/ap/activity.h"
#include "model/ap/inbox_envelope.h"
#include <stdio.h>
#include <stdint.h>
@ -11,6 +12,8 @@
#include <time.h>
#include <unistd.h>
extern bool terminate;
static void io_copy( FILE* in, FILE* out )
{
char buffer[512];
@ -89,11 +92,21 @@ void process_inbox()
{
int tail_pos = read_list_end("data/inbox/TAIL");
for(;;) {
while( !terminate ) {
int head_pos = read_list_end("data/inbox/HEAD");
if( head_pos > tail_pos ) {
// We have data to process
printf( "Inbox has %d items pending processing...\n", (head_pos - tail_pos) );
int i = tail_pos + 1;
printf( "Processing %d\n", i );
struct ap_envelope* env = ap_envelope_from_id(i);
if( env ) {
printf( "When: %s\n", env->when );
ap_envelope_free(env);
printf( "done\n" );
}
}
sleep(30);

@ -1 +1 @@
Subproject commit 9baefa441da46e353691c0e12ac0b008afd4802d
Subproject commit a2b0ead640581ff9c7bfeeb2025bb62ff9ee6a06

@ -68,6 +68,14 @@ int main( int argc, char* argv[] )
return 1;
}
if( argc > 1 ) {
int section = 0;
sscanf(argv[1],"--section=%d",&section);
switch( section ) {
case 1: process_inbox(); return 0;
}
}
int inbox_handler_pid = -1;
// Process inbox

@ -3,6 +3,8 @@
#include "json/json.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
static struct json_enum ap_signature_type_enum[] = {
{ "RsaSignature2017", 1 },
@ -23,6 +25,62 @@ static struct json_enum ap_activity_type_enum[] = {
{ NULL, 0 },
};
struct ap_activity* ap_activity_new()
{
struct ap_activity* act = malloc(sizeof(struct ap_activity));
memset(act,0,sizeof(struct ap_activity));
return act;
}
static void* ap_activity_alloc()
{
return (void*)ap_activity_new();
}
void ap_activity_free( struct ap_activity* act )
{
if( !act ) { return; }
free(act->id);
free(act->actor);
for( int i = 0; i < act->to.count; ++i ) {
free(act->to.items[i]);
}
free(act->to.items);
for( int i = 0; i < act->cc.count; ++i ) {
free(act->cc.items[i]);
}
free(act->cc.items);
for( int i = 0; i < act->bcc.count; ++i ) {
free(act->bcc.items[i]);
}
free(act->bcc.items);
free( act->object.ref );
ap_activity_free( act->object.ptr );
free( act->signature.creator );
free( act->signature.created );
free( act->signature.value );
free(act);
}
static void ap_activity_free2( void* ptr )
{
ap_activity_free(ptr);
}
struct json_field_type ap_activity_type = {
.reader = json_field_object_type_reader,
.writer = json_field_object_type_writer,
.size = sizeof(struct ap_activity),
.layout = ap_activity_layout,
.alloc = ap_activity_alloc,
.free = ap_activity_free2,
};
struct json_object_field ap_activity_layout[] = {
{ "id", offsetof( struct ap_activity, id ), true, &json_field_string },
{ "type", offsetof( struct ap_activity, type ), true, &json_field_enum, ap_activity_type_enum },
@ -30,7 +88,8 @@ struct json_object_field ap_activity_layout[] = {
{ "to", offsetof( struct ap_activity, to ), true, &json_field_array_of, &json_field_string },
{ "cc", offsetof( struct ap_activity, cc ), false, &json_field_array_of, &json_field_string },
{ "bcc", offsetof( struct ap_activity, bcc ), false, &json_field_array_of, &json_field_string },
{ "object", offsetof( struct ap_activity, object ), false, &json_field_string },
{ "signature", offsetof( struct ap_activity, signature ), true, &json_field_object_composite, ap_signature_layout },
{ NULL },
{ "object", offsetof( struct ap_activity, object.ref ), false, &json_field_string },
{ "object", offsetof( struct ap_activity, object.ptr ), false, &json_field_object_pointer, ap_activity_layout },
{ "signature", offsetof( struct ap_activity, signature ), false, &json_field_object_composite, ap_signature_layout },
{ NULL, 0, true, NULL, &ap_activity_type },
};

@ -30,10 +30,14 @@ struct ap_activity
char** items;
int count;
} to, cc, bcc;
char* object;
struct {
char* ref;
struct ap_activity* ptr;
} object;
struct ap_signature signature;
};
struct json_object_field;
extern struct json_field_type ap_activity_type;
extern struct json_object_field ap_activity_layout[];

@ -0,0 +1,41 @@
#include "inbox_envelope.h"
#include "json/json.h"
#include "json/layout.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
static struct json_object_field envelope_layout[] = {
{ "signature", offsetof(struct ap_envelope, signature), true, &json_field_string },
{ "date", offsetof(struct ap_envelope, date), true, &json_field_string },
{ "when", offsetof(struct ap_envelope, when), true, &json_field_string },
{ "body", offsetof(struct ap_envelope, activity), true, &json_field_object_composite, ap_activity_layout },
{ NULL },
};
struct ap_envelope* ap_envelope_from_id( int id )
{
char filename[512];
snprintf( filename, 512, "data/inbox/%d.json", id );
struct ap_envelope* env = malloc(sizeof(struct ap_envelope));
if( !json_read_object_layout_from_file( filename, envelope_layout, env ) ) {
ap_envelope_free(env);
return NULL;
}
return env;
}
void ap_envelope_free( struct ap_envelope* env )
{
if( !env ) { return; }
free(env->signature);
free(env->date);
free(env->when);
free(env);
}

@ -0,0 +1,16 @@
#pragma once
#include "activity.h"
struct ap_envelope
{
char* signature;
char* date;
char* when;
struct ap_activity activity;
};
struct ap_envelope* ap_envelope_from_id( int id );
void ap_envelope_free( struct ap_envelope* env );
Loading…
Cancel
Save