summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 1ad37108c931c581328531741bb6ba39262e5013 (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
/* Tidbit
 * Copyright (C) 2010 Charlie Brej tidbit@brej.org
 *
 * 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 3.0 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <glib.h>
#include "tidbit.h"
#include "tidbit-database-fork.h"
#include "tidbit-database-http.h"
#include "tidbit-database-mem.h"
#include "tidbit-database-dbus.h"
#include "tidbit-database.h"
#include "tidbit-record.h"
#include "tidbit-key.h"
#include "tidbit-dbus.h"
#include "tidbit-http.h"


void print_guid (PtrTidbitGuid guid, gpointer database)
{
 PtrTidbitRecord record = tidbit_database_fetch_record (database, guid);
 g_print("Got: %s\n", record->raw);
 tidbit_record_unref (record);
}


int main(int argc, char* argv[])
{

 gboolean enable_http = FALSE;
 gboolean enable_dbus = FALSE;

 GOptionEntry option_entries[] = 
    {
        { "enable-http", 'h', 0, G_OPTION_ARG_NONE, &enable_http, "Enable HTTP fontend", NULL },
        { "enable-dbus", 'd', 0, G_OPTION_ARG_NONE, &enable_dbus, "Enable D-BUS fontend", NULL },
        { NULL }
    };
 
 GError *error = NULL;
 GOptionContext *context = g_option_context_new ("- Server for exchanging tidbit packets");
 g_option_context_add_main_entries (context, option_entries, NULL);
 if (!g_option_context_parse (context, &argc, &argv, &error)){
    g_print ("option parsing failed: %s\n", error->message);
    exit (1);
    }
 
 
 
 
 g_type_init ();
 if (!g_thread_supported ())
	 g_thread_init (NULL);

 dbus_g_thread_init ();
 GMainLoop *loop = g_main_loop_new (NULL, FALSE);
 
 PtrTidbitDatabase database;
 
 if (enable_http || enable_dbus)
  database = tidbit_database_fork_new(tidbit_database_mem_new (), tidbit_database_mem_new ());
 else
  database = tidbit_database_dbus_new ();

 if (enable_http)
    tidbit_http_setup (database);
 if (enable_dbus)
    tidbit_dbus_setup (database);
    
 
 
 PtrTidbitRecord record = tidbit_record_new ("test/tablename");
 tidbit_record_add_element (record, "key", "value");
 tidbit_record_add_element (record, "another_key", "value");
 PtrTidbitKey key = tidbit_key_get ("testapp", "Tidbit Test App v0.1");
 
 tidbit_record_sign (record, key);
 
 tidbit_database_insert_record (database, record);
 g_print("Insert: %s\n", record->raw);
 tidbit_record_unref (record);
 
 record = tidbit_database_fetch_record (database, record->guid);
 g_print("Fetch: %s\n", record->raw);
 tidbit_record_unref (record);
 
 PtrTidbitQuery query = tidbit_query_new ("test/tablename");
 tidbit_query_add_exp (query, "key", "value", TIDBIT_QUERY_EXP_TyPE_EQ);
 
 PtrTidbitGuidSet guid_set = tidbit_database_query (database, query);
 tidbit_guid_set_foreach (guid_set, print_guid, database);

 
 
 
 
 
 g_main_loop_run (loop);
 return 0;
}