session.cc 4.68 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

#include "data.h"
#include "session.h"

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

using namespace std;
using namespace ISC::CC;
using namespace ISC::Data;

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

Session::Session()
{
    sock = -1;
}

void
Session::disconnect()
{
    close(sock);
    sock = -1;
}

void
Session::establish()
{
    int ret;
    struct sockaddr_in sin;

    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < -1)
        throw SessionError("socket() failed");

    sin.sin_family = AF_INET;
    sin.sin_port = htons(9912);
    sin.sin_addr.s_addr = INADDR_ANY;
42
43
44
45
46

#ifdef HAVE_SIN_LEN
    sin.sin_len = sizeof(struct sockaddr_in);
#endif

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
    ret = connect(sock, (struct sockaddr *)&sin, sizeof(sin));
    if (ret < 0)
        throw SessionError("connect() failed");

    //
    // send a request for our local name, and wait for a response
    //
    std::string get_lname_str = "{ \"type\": \"getlname\" }";
    std::stringstream get_lname_stream;
    get_lname_stream.str(get_lname_str);
    ElementPtr get_lname_msg = Element::create_from_string(get_lname_stream);
    sendmsg(get_lname_msg);

    ElementPtr msg;
    recvmsg(msg, false);

    lname = msg->get("lname")->string_value();
    cout << "My local name is:  " << lname << endl;
}

//
// Convert to wire format and send this on the TCP stream with its length prefix
//
void
Session::sendmsg(ElementPtr& msg)
{
    std::string wire = msg->to_wire();
    unsigned int length = wire.length();
    unsigned int length_net = htonl(length);
    unsigned int ret;

    ret = write(sock, &length_net, 4);
    if (ret != 4)
        throw SessionError("Short write");

    ret = write(sock, wire.c_str(), length);
    if (ret != length)
        throw SessionError("Short write");
}

bool
Session::recvmsg(ElementPtr& msg, bool nonblock)
{
    unsigned int length_net;
    unsigned int ret;

    ret = read(sock, &length_net, 4);
    if (ret != 4)
        throw SessionError("Short read");

    unsigned int length = ntohl(length_net);
    char *buffer = new char[length];
    ret = read(sock, buffer, length);
    if (ret != length)
        throw SessionError("Short read");

    std::string wire = std::string(buffer, length);
    delete [] buffer;

    std::stringstream wire_stream;
    wire_stream <<wire;

    msg = Element::from_wire(wire_stream, length);

    return (true);
    // XXXMLG handle non-block here, and return false for short reads
}

void
Session::subscribe(std::string group, std::string instance, std::string subtype)
{
    ElementPtr env = Element::create(std::map<std::string, ElementPtr>());

    env->set("type", Element::create("subscribe"));
    env->set("group", Element::create(group));
    env->set("instance", Element::create(instance));
    env->set("subtype", Element::create(subtype));

    sendmsg(env);
}

void
Session::unsubscribe(std::string group, std::string instance)
{
    ElementPtr env = Element::create(std::map<std::string, ElementPtr>());

    env->set("type", Element::create("unsubscribe"));
    env->set("group", Element::create(group));
    env->set("instance", Element::create(instance));

    sendmsg(env);
}

unsigned int
Session::group_sendmsg(ElementPtr& msg, std::string group, std::string instance, std::string to)
{
    ElementPtr env = Element::create(std::map<std::string, ElementPtr>());

    env->set("type", Element::create("send"));
    env->set("from", Element::create(lname));
    env->set("to", Element::create(to));
    env->set("group", Element::create(group));
    env->set("instance", Element::create(instance));
    env->set("seq", Element::create(sequence));
    env->set("msg", Element::create(msg->to_wire()));

    sendmsg(env);

    return (sequence++);
}

bool
Session::group_recvmsg(ElementPtr& envelope, ElementPtr& msg, bool nonblock)
{
    bool got_message = recvmsg(envelope, nonblock);
    if (!got_message) {
        return false;
    }

    msg = Element::from_wire(envelope->get("msg")->string_value());
    envelope->remove("msg");

    return (true);
}

unsigned int
Session::reply(ElementPtr& envelope, ElementPtr& newmsg)
{
    ElementPtr env = Element::create(std::map<std::string, ElementPtr>());

    env->set("type", Element::create("send"));
    env->set("from", Element::create(lname));
    env->set("to", Element::create(envelope->get("from")->string_value()));
    env->set("group", Element::create(envelope->get("group")->string_value()));
    env->set("instance", Element::create(envelope->get("instance")->string_value()));
    env->set("seq", Element::create(sequence));
    env->set("msg", Element::create(newmsg->to_wire()));
    env->set("reply", Element::create(envelope->get("seq")->string_value()));

    sendmsg(env);

    return (sequence++);
}