fake_session.cc 7.12 KB
Newer Older
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
// Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// $Id: session.cc 1250 2010-03-09 22:52:15Z jinmei $

#include "config.h"

#include <stdint.h>

#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>

#ifdef HAVE_BOOSTLIB
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>
#endif

#include <boost/foreach.hpp>

#include <exceptions/exceptions.h>

#include <cc/data.h>
#include "fake_session.h"

using namespace std;
using namespace isc::cc;
using namespace isc::data;

#ifdef HAVE_BOOSTLIB
// some of the boost::asio names conflict with socket API system calls
// (e.g. write(2)) so we don't import the entire boost::asio namespace.
using boost::asio::io_service;
using boost::asio::ip::tcp;
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>


// ok i want these in cc/data 
static bool
listContains(ElementPtr list, ElementPtr el)
{
Jelte Jansen's avatar
Jelte Jansen committed
59
60
61
    if (!list) {
        return false;
    }
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
    BOOST_FOREACH(ElementPtr l_el, list->listValue()) {
        if (l_el == el) {
            return true;
        }
    }
    return false;
}

static void
listRemove(ElementPtr list, ElementPtr el) {
    int i = -1;
    BOOST_FOREACH(ElementPtr s_el, list->listValue()) {
        if (el == s_el) {
            i = 0;
        }
    }
    if (i >= 0) {
        list->remove(i);
    }
}
// endwant

ElementPtr
getFirstMessage(std::string& group, std::string& to)
{
    ElementPtr el;
    if (msg_queue && msg_queue->size() > 0) {
        el = msg_queue->get(0);
        msg_queue->remove(0);
        group = el->get(0)->stringValue();
        to = el->get(1)->stringValue();
        return el->get(2);
    } else {
        group = "";
        to = "";
        return ElementPtr();
    }
}

void
addMessage(ElementPtr msg, const std::string& group, const std::string& to)
{
    ElementPtr m_el = Element::createFromString("[]");
    m_el->add(Element::create(group));
    m_el->add(Element::create(to));
    m_el->add(msg);
    if (!msg_queue) {
        msg_queue = Element::createFromString("[]");
    }
    msg_queue->add(m_el);
}

bool
haveSubscription(const std::string& group, const std::string& instance)
{
    ElementPtr s1 = Element::createFromString("[]");
    ElementPtr s2 = Element::createFromString("[]");
    s1->add(Element::create(group));
    s1->add(Element::create(instance));
    s2->add(Element::create(group));
Jelte Jansen's avatar
Jelte Jansen committed
122
123
124
    s2->add(Element::create("*"));
    bool result = (listContains(subscriptions, s1) || listContains(subscriptions, s2));
    return result;
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
}

bool
haveSubscription(const ElementPtr group, const ElementPtr instance)
{
    return haveSubscription(group->stringValue(), instance->stringValue());
}

namespace isc {
namespace cc {

Session::Session()
{
}

#ifdef HAVE_BOOSTLIB
Session::Session(io_service& io_service UNUSED_PARAM)
{
}
#endif

Session::~Session() {
}

void
Session::disconnect() {
}

int
Session::getSocket() const {
    return 1;
}

void
Session::startRead(boost::function<void()> read_callback UNUSED_PARAM) {
}

void
Session::establish() {
}

//
// Convert to wire format and send this on the TCP stream with its length prefix
//
void
Session::sendmsg(ElementPtr& msg) {
    //cout << "[XX] client sends message: " << msg << endl;
    // err, to where?
    addMessage(msg, "*", "*");
}

void
Session::sendmsg(ElementPtr& env, ElementPtr& msg) {
    //cout << "[XX] client sends message: " << msg << endl;
    //cout << "[XX] env: " << env << endl;
    addMessage(msg, env->get("group")->stringValue(), env->get("to")->stringValue());
}

bool
Session::recvmsg(ElementPtr& msg, bool nonblock UNUSED_PARAM) {
    //cout << "[XX] client asks for message " << endl;
    if (initial_messages &&
        initial_messages->getType() == Element::list &&
        initial_messages->size() > 0) {
        msg = initial_messages->get(0);
        initial_messages->remove(0);
    } else {
        msg = ElementPtr();
    }
    return (true);
}

bool
Session::recvmsg(ElementPtr& env, ElementPtr& msg, bool nonblock UNUSED_PARAM) {
    //cout << "[XX] client asks for message and env" << endl;
    env = ElementPtr();
    if (initial_messages &&
        initial_messages->getType() == Element::list &&
        initial_messages->size() > 0) {
        // do we need initial message to have env[group] and [to] too?
        msg = initial_messages->get(0);
        initial_messages->remove(0);
        return true;
    } else {
        BOOST_FOREACH(ElementPtr c_m, msg_queue->listValue()) {
            ElementPtr to_remove = ElementPtr();
            if (haveSubscription(c_m->get(0), c_m->get(1))) {
                env = Element::createFromString("{}");
                env->set("group", c_m->get(0));
                env->set("to", c_m->get(1));
                msg = c_m->get(2);
                to_remove = c_m;
            }
            if (to_remove) {
                listRemove(msg_queue, to_remove);
                return true;
            }
        }
    }
    msg = ElementPtr();
    env = ElementPtr();
    return false;
}

void
Session::subscribe(std::string group, std::string instance) {
Jelte Jansen's avatar
Jelte Jansen committed
231
    //cout << "[XX] client subscribes to " << group << " . " << instance << endl;
232
233
234
    ElementPtr s_el = Element::createFromString("[]");
    s_el->add(Element::create(group));
    s_el->add(Element::create(instance));
Jelte Jansen's avatar
Jelte Jansen committed
235
236
237
    if (!subscriptions) {
        subscriptions = Element::createFromString("[]");
    }
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
    subscriptions->add(s_el);
}

void
Session::unsubscribe(std::string group, std::string instance) {
    //cout << "[XX] client unsubscribes from " << group << " . " << instance << endl;
    ElementPtr s_el = Element::createFromString("[]");
    s_el->add(Element::create(group));
    s_el->add(Element::create(instance));
    listRemove(subscriptions, s_el);
}

unsigned int
Session::group_sendmsg(ElementPtr msg, std::string group,
                       std::string to, std::string instance UNUSED_PARAM)
{
    //cout << "[XX] client sends message: " << msg << endl;
    //cout << "[XX] to: " << group << " . " << instance << "." << to << endl;
    addMessage(msg, group, to);
    return 1;
}

bool
Session::group_recvmsg(ElementPtr& envelope, ElementPtr& msg,
                       bool nonblock)
{
    return (recvmsg(envelope, msg, nonblock));
}

unsigned int
Session::reply(ElementPtr& envelope, ElementPtr& newmsg) {
    //cout << "[XX] client sends reply: " << newmsg << endl;
    //cout << "[XX] env: " << envelope << endl;
    addMessage(newmsg, envelope->get("group")->stringValue(), envelope->get("to")->stringValue());
    return 1;
}

}
}