~michael-owens/ltnet/trunk

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
#include <stdlib.h>
#include <event.h>

#include "client.h"
#include "modules/dbi/client.h"

void test_client();
void test_dbi_client();

using namespace linterra;
using namespace linterra::server;

int main(int argc, const char *argv[])
{
    linterra::apr::init();
	event_init();

    //test_client();
    test_dbi_client();

    return EXIT_SUCCESS;
}

void test_dbi_client()
{
    client c;

    if(c.connect("localhost", 4747) == false)
    {
        exit(1);
    }

    if(c.login("owensmk", "test") == false)
    {
        printf("Login FAILED\n");

        exit(1);
    }
    
    linterra::net::dbi::connection cnx(&c);

    if(cnx.connect("beastie", "session", "root", "frobenious") == false)
    {
        printf("Error connecting to database: %s\n", cnx.error());

        return;
    }

    linterra::dbi::recordset* rs = cnx.query("select * from pg_class limit 10");

    if(rs != NULL)
    {
        linterra::dbi::recordset& set = *rs;

        do
        {
            for(unsigned int j = 0; j < set.num_cols(); j++)
            {
                const char* value = set[j];
                printf("%.*s, ", 25, value ? value : "NULL");
            }
            
            printf("\n");
            
        }
        while(set.next());

        delete rs;
    }

    //cnx.execute("insert into log(message) values ('test')");    
    //i32 id = cnx.insert_id("messagelog", "messagelogid");
    //printf("insert id: %i\n", id);

    cnx.disconnect();

    c.disconnect();
}

void test_client()
{
    client c;

    if(c.connect("localhost", 4747) == false)
    {
        exit(1);
    }

    // Send message

    message m(1);
    m.url  << "http://www.linterra.org/mod/dbi";
    m.data << "send data";
    
    if(c.login("owensmk", "test") == false)
    {
        printf("Login FAILED\n");

        exit(1);
    }
        
    //m.data.read_file("Makefile.in");

    int i = 1;
    while(1)
    {
        m.id  = i;
        m.seq = 10;
        printf("sending request %i\n", i);
        c.send(m);

        message response;

        while(1)
        {
            if(c.receive(response) == false)
            {
                printf("ERROR\n");

                break;
            }

            printf("id/seq: %i/%i\n", response.id, response.seq);
            //printf("url:  %s\n", response.url.string());
            //printf("hash: %s\n", response.data.sha1().string());

            // Check for end of response
            if(response.end == true)
            {
                break;
            }
        }

        if(i++ >= 4)
        {
            break;
        }
    }

    c.disconnect();
}