summaryrefslogtreecommitdiffstats
path: root/src/kiro-sb.c
blob: 6ec671c49973d40c48961f0fa02b13983d191cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/* Copyright (C) 2014-2015 Timo Dritschler <timo.dritschler@kit.edu>
   (Karlsruhe Institute of Technology)

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   This library is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public License along
   with this library; if not, write to the Free Software Foundation, Inc., 51
   Franklin St, Fifth Floor, Boston, MA 02110, USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "kiro-sb.h"
#include "kiro-trb.h"
#include "kiro-server.h"
#include "kiro-client.h"


/*
 * Definition of 'private' structures and members and macro to access them
 */

#define KIRO_SB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KIRO_TYPE_SB, KiroSbPrivate))

struct _KiroSbPrivate {

    /* Properties */
    // PLACEHOLDER //

    /* 'Real' private structures */
    /* (Not accessible by properties) */
    int         initialized;    // 0 if uninitialized, 1 if server, 2 if client
    KiroServer* server;         // KIRO Server component to serve
    KiroClient* client;         // KIRO Client component to clone
    KiroTrb* trb;               // KIRO Ring Buffer to hold and exchange data

    GThread     *main_thread;   // Main thread for the main_loop
    GMainLoop   *main_loop;     // main_loop *duh*
    guint       close_signal;   // Used to signal shutdown of the main_loop
    gboolean    freeze;         // Allows to prevent auto-sync

    GHookList   callbacks;      // List of registerd sync-callbacks
};


G_DEFINE_TYPE (KiroSb, kiro_sb, G_TYPE_OBJECT);


KiroSb *
kiro_sb_new (void)
{
    return g_object_new (KIRO_TYPE_SB, NULL);
}


void
kiro_sb_free (KiroSb *sb)
{
    g_return_if_fail (sb != NULL);
    if (KIRO_IS_SB (sb))
        g_object_unref (sb);
    else
        g_warning ("Trying to use kiro_sb_free on an object which is not a KIRO SB. Ignoring...");
}


static void
kiro_sb_init (KiroSb *self)
{
    g_return_if_fail (self != NULL);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    priv->initialized = 0;
    priv->trb = NULL;
    priv->server = NULL;
    priv->client = NULL;
    priv->freeze = FALSE;
    g_hook_list_init (&(priv->callbacks), sizeof (GHook));
}


static void
kiro_sb_finalize (GObject *object)
{
    g_return_if_fail (object != NULL);
    KiroSb *self = KIRO_SB (object);

    kiro_sb_stop (self);

    G_OBJECT_CLASS (kiro_sb_parent_class)->finalize (object);
}


static void
kiro_sb_class_init (KiroSbClass *klass)
{
    g_return_if_fail (klass != NULL);
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->finalize = kiro_sb_finalize;
    g_type_class_add_private (klass, sizeof (KiroSbPrivate));
}


void
kiro_sb_stop (KiroSb *self)
{
    g_return_if_fail (self != NULL);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    g_return_if_fail (priv->initialized != 0);

    if (priv->initialized == 1) {
        if (priv->server)
            kiro_server_free (priv->server);
    }

    if (priv->initialized == 2) {
        priv->close_signal = TRUE;
        while (g_main_loop_is_running (priv->main_loop)) {}
        g_thread_join (priv->main_thread);
        g_thread_unref (priv->main_thread);
        priv->main_thread = NULL;

        if (priv->client)
            kiro_client_free (priv->client);
    }

    g_hook_list_clear (&(priv->callbacks));

    if (priv->trb) {
        kiro_trb_purge (priv->trb, FALSE);
        kiro_trb_free (priv->trb);
    }

    priv->trb = NULL;
    priv->server = NULL;
    priv->client = NULL;
    priv->initialized = 0;
}


gpointer
start_main_loop (GMainLoop *loop)
{
    g_main_loop_run (loop);
    /* wait for mai loop to finish*/
    g_main_loop_unref (loop);
    return NULL;
}


gboolean
idle_func (KiroSbPrivate *priv)
{
    if (priv->close_signal) {
        g_main_loop_quit (priv->main_loop);
        /*main_thread will do the unref upon exit*/
        priv->main_loop = NULL;
        g_debug ("Main loop quit");
        return G_SOURCE_REMOVE;
    }

    if (TRUE == priv->freeze)
        return G_SOURCE_CONTINUE;

    struct KiroTrbInfo *header = (struct KiroTrbInfo *)kiro_trb_get_raw_buffer (priv->trb);
    gulong old_offset = header->offset;
    kiro_client_sync_partial (priv->client, 0, sizeof(struct KiroTrbInfo), 0);
    kiro_trb_refresh (priv->trb);
    if ((old_offset != header->offset) && 0 < header->offset) {
        gulong offset = (gulong) (kiro_trb_get_element (priv->trb, -1) - kiro_trb_get_raw_buffer (priv->trb));
        kiro_client_sync_partial (priv->client, offset, kiro_trb_get_element_size (priv->trb), offset);
        g_hook_list_invoke_check (&(priv->callbacks), FALSE);
    }

    return G_SOURCE_CONTINUE;
}


void
kiro_sb_freeze (KiroSb *self)
{
    g_return_if_fail (self != NULL);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    priv->freeze = TRUE;
}


void
kiro_sb_thaw (KiroSb *self)
{
    g_return_if_fail (self != NULL);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    priv->freeze = FALSE;
}


gboolean
kiro_sb_serve (KiroSb *self, gulong size, const gchar *addr, const gchar *port)
{
    g_return_val_if_fail (self != NULL, FALSE);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    g_return_val_if_fail (priv->initialized == 0, FALSE);

    g_return_val_if_fail ((priv->trb = kiro_trb_new ()), FALSE);

    if (0 > kiro_trb_reshape (priv->trb, size, 3)) {
        g_debug ("Failed to create KIRO ring buffer");
        kiro_trb_free (priv->trb);
        return FALSE;
    }

    void *buff = kiro_trb_get_raw_buffer (priv->trb);
    gulong b_size = kiro_trb_get_raw_size (priv->trb);

    const gchar *port_internal = port ? port : "60010";

    priv->server = kiro_server_new ();
    if (0 > kiro_server_start (priv->server, addr, port_internal, buff, b_size)) {
        g_debug ("Failed to start KIRO Server");
        kiro_server_free (priv->server);
        kiro_trb_free (priv->trb);
        return FALSE;
    }

    priv->initialized = 1;
    g_message ("SyncBuffer ready");

    return TRUE;
}


KiroContinueFlag
ready_callback (gboolean *ready)
{
    *ready = TRUE;
    return KIRO_CALLBACK_REMOVE;
}


void *
kiro_sb_get_data_blocking (KiroSb *self)
{
    g_return_val_if_fail (self != NULL, NULL);

    gboolean *ready = g_malloc(sizeof (gboolean));
    if (!ready)
        return NULL;
    *ready = FALSE;
    kiro_sb_add_sync_callback (self, (KiroSbSyncCallbackFunc)ready_callback, ready);

    while (!(*ready)) {}
    g_free (ready);
    return kiro_sb_get_data (self);
}


void *
kiro_sb_get_data (KiroSb *self)
{
    g_return_val_if_fail (self != NULL, NULL);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    struct KiroTrbInfo *header = kiro_trb_get_raw_buffer (priv->trb);
    switch (header->offset) {
        case 0:
            return kiro_trb_get_element (priv->trb, 0);
            break;
        case 1:
            return kiro_trb_get_element (priv->trb, 1);
            break;
        default:
            return kiro_trb_get_element (priv->trb, -1);
    }
}


gboolean
kiro_sb_push (KiroSb *self, void *data_in)
{
    g_return_val_if_fail (self != NULL, FALSE);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    g_return_val_if_fail (priv->initialized == 1, FALSE);

    return kiro_trb_push (priv->trb, data_in);
}


void *
kiro_sb_push_dma (KiroSb *self)
{
    g_return_val_if_fail (self != NULL, NULL);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    g_return_val_if_fail (priv->initialized == 1, NULL);

    return kiro_trb_dma_push (priv->trb);
}


gboolean
kiro_sb_clone (KiroSb *self, const gchar* address, const gchar* port)
{
    g_return_val_if_fail (self != NULL, FALSE);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    g_return_val_if_fail (priv->initialized == 0, FALSE);

    g_return_val_if_fail ((priv->trb = kiro_trb_new ()), FALSE);

    priv->client = kiro_client_new ();
    if (0 > kiro_client_connect (priv->client, address, port)) {
        g_debug ("Failed to connect to remote Sync Buffer");
        kiro_trb_free (priv->trb);
        kiro_client_free (priv->client);
        return FALSE;
    }

    kiro_client_sync (priv->client);
    kiro_trb_adopt (priv->trb, kiro_client_get_memory (priv->client));

    priv->main_loop = g_main_loop_new (NULL, FALSE);
    g_idle_add ((GSourceFunc)idle_func, priv);
    priv->main_thread = g_thread_new ("KIRO SB Main Loop", (GThreadFunc)start_main_loop, priv->main_loop);

    priv->initialized = 2;
    return TRUE;
}


gulong
kiro_sb_add_sync_callback (KiroSb *self, KiroSbSyncCallbackFunc func, void *user_data)
{
    g_return_val_if_fail (self != NULL, 0);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    GHook *new_hook = g_hook_alloc (&(priv->callbacks));
    new_hook->data = user_data;
    new_hook->func = (GHookCheckFunc)func;
    g_hook_append (&(priv->callbacks), new_hook);
    return new_hook->hook_id;
}


gboolean
kiro_sb_remove_sync_callback (KiroSb *self, gulong hook_id)
{
    g_return_val_if_fail (self != NULL, FALSE);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    return g_hook_destroy (&(priv->callbacks), hook_id);
}


void
kiro_sb_clear_sync_callbacks (KiroSb *self)
{
    g_return_val_if_fail (self != NULL, FALSE);
    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);

    g_hook_list_clear (&(priv->callbacks));
}


gulong
kiro_sb_get_size (KiroSb *self)
{
    g_return_val_if_fail (self != NULL, 0);

    KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
    g_return_val_if_fail (priv->initialized != 0, 0);

    return kiro_trb_get_element_size (priv->trb);
}