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
|
/* 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
*/
/**
* SECTION: kiro-sb
* @Short_description: KIRO 'Synchronizing Buffer'
* @Title: KiroSb
*
* KiroSb implements a 'Synchronizing Buffer' that automatically keeps the local
* memory content up to date by mirroring the remote SyncBuffers memory content
* automatically without any required user interaction
*/
#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
};
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;
}
static void
kiro_sb_finalize (GObject *object)
{
g_return_if_fail (object != NULL);
KiroSb *self = KIRO_SB (object);
KiroSbPrivate *priv = KIRO_SB_GET_PRIVATE (self);
if (priv->trb) {
kiro_trb_purge (priv->trb, FALSE);
kiro_trb_free (priv->trb);
}
if (priv->server)
kiro_server_free (priv->server);
if (priv->client)
kiro_client_free (priv->client);
priv->trb = NULL;
priv->server = NULL;
priv->client = NULL;
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));
}
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;
}
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) {
g_debug ("Fetching new element");
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);
/*INVOKE callback*/
}
return G_SOURCE_CONTINUE;
}
gboolean
kiro_sb_serve (KiroSb *self, gulong size)
{
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);
priv->server = kiro_server_new ();
if (0 > kiro_server_start (priv->server, NULL, "60010", 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;
}
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);
return TRUE;
}
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);
}
/* Privat functions */
|