~timkross/cjdns/cjdns

« back to all changes in this revision

Viewing changes to dht/SerializationModule.c

  • Committer: cjdelisle
  • Date: 2011-02-16 23:03:00 UTC
  • Revision ID: git-v1:d475c9c10adc25590bea5e7dc5383b32f66d5eb8
First commit for cjdns.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or
 
3
 * modify it under the terms of the GNU General Public License as
 
4
 * published by the Free Software Foundation; either version 3 of the
 
5
 * License, or (at your option) any later version.
 
6
 *
 
7
 * This program is distributed in the hope that it will be useful,
 
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
10
 * General Public License for more details.
 
11
 *
 
12
 * You should have received a copy of the GNU General Public License
 
13
 * along with this program; if not, write to the Free Software
 
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
15
 * 02111-1307 USA
 
16
 */
 
17
 
 
18
#include "DHTModules.h"
 
19
 
 
20
#include <libbenc/benc.h>
 
21
#include <memory/MemAllocator.h>
 
22
#include <memory/BufferAllocator.h>
 
23
#include <io/Reader.h>
 
24
#include <io/ArrayReader.h>
 
25
#include <io/Writer.h>
 
26
#include <io/ArrayWriter.h>
 
27
 
 
28
#include <string.h>
 
29
 
 
30
/* The number of bytes for storing deserialized packets. */
 
31
#define WORKSPACE_SIZE 4096
 
32
 
 
33
struct SerializationModule_context {
 
34
    char workspace[WORKSPACE_SIZE];
 
35
    struct MemAllocator* allocator;
 
36
    struct DHTModule module;
 
37
};
 
38
 
 
39
/*--------------------Prototypes--------------------*/
 
40
static void freeModule(struct DHTModule* module);
 
41
static int handleOutgoing(struct DHTMessage* message,
 
42
                          void* vcontext);
 
43
static int handleIncoming(struct DHTMessage* message,
 
44
                          void* vcontext);
 
45
 
 
46
/*--------------------Interface--------------------*/
 
47
 
 
48
/**
 
49
 * Get a new SerializationModule.
 
50
 *
 
51
 * @return a new SerializationModule or NULL if there is no space to allocate required memory.
 
52
 */
 
53
struct DHTModule* SerializationModule_new()
 
54
{
 
55
    struct SerializationModule_context* context =
 
56
        malloc(sizeof(struct SerializationModule_context));
 
57
 
 
58
    if (context == NULL) {
 
59
        return NULL;
 
60
    }
 
61
 
 
62
    context->allocator = BufferAllocator_new(context->workspace, WORKSPACE_SIZE);
 
63
 
 
64
    if (context->allocator == NULL) {
 
65
        return NULL;
 
66
    }
 
67
 
 
68
    struct DHTModule module = {
 
69
        .name = "SerializationModule",
 
70
        .context = context,
 
71
        .free = freeModule,
 
72
        .serialize = NULL,
 
73
        .deserialize = NULL,
 
74
        .compareNodes = NULL,
 
75
        .handleIncoming = handleIncoming,
 
76
        .handleOutgoing = handleOutgoing
 
77
    };
 
78
 
 
79
    memcpy(&context->module, &module, sizeof(struct DHTModule));
 
80
 
 
81
    return &context->module;
 
82
}
 
83
 
 
84
/*--------------------Internals--------------------*/
 
85
 
 
86
/** @see DHTModule->freeContext in DHTModules.h */
 
87
static void freeModule(struct DHTModule* module)
 
88
{
 
89
    if (module == NULL) {
 
90
        return;
 
91
    }
 
92
 
 
93
    struct SerializationModule_context* context =
 
94
        (struct SerializationModule_context*) module->context;
 
95
 
 
96
    free(context);
 
97
}
 
98
 
 
99
/**
 
100
 * Take an outgoing message and serialize the bencoded message.
 
101
 *
 
102
 * @see DHTModule->handleOutgoing in DHTModules.h
 
103
 */
 
104
static int handleOutgoing(struct DHTMessage* message,
 
105
                          void* vcontext)
 
106
{
 
107
    struct MemAllocator* allocator =
 
108
        ((struct SerializationModule_context*) vcontext)->allocator;
 
109
 
 
110
    allocator->free(allocator);
 
111
    struct Writer* writer = ArrayWriter_new(message->bytes, MAX_MESSAGE_SIZE, allocator);
 
112
    if (writer == NULL) {
 
113
        return -1;
 
114
    }
 
115
 
 
116
    bobj_serialize(writer, message->bencoded);
 
117
    message->length = writer->bytesWritten(writer);
 
118
 
 
119
    return 0;
 
120
}
 
121
 
 
122
/**
 
123
 * Take an incoming message and deserialize the bencoded message.
 
124
 *
 
125
 * @see DHTModule->handleIncoming in DHTModules.h
 
126
 */
 
127
static int handleIncoming(struct DHTMessage* message,
 
128
                          void* vcontext)
 
129
{
 
130
    struct MemAllocator* allocator =
 
131
        ((struct SerializationModule_context*) vcontext)->allocator;
 
132
 
 
133
    allocator->free(allocator);
 
134
    struct Reader* reader = ArrayReader_new(message->bytes, MAX_MESSAGE_SIZE, allocator);
 
135
    if (reader == NULL) {
 
136
        return -1;
 
137
    }
 
138
 
 
139
    bobj_parse(reader, allocator, &message->bencoded);
 
140
 
 
141
    return 0;
 
142
}
 
143
 
 
144
#undef WORKSPACE_SIZE