[svn] commit: r214 - in /experiments/jelte-configuration: Makefile.in data.cc data.h session.cc session.h test.cc
BIND 10 source code commits
bind10-changes at lists.isc.org
Fri Oct 30 00:45:29 UTC 2009
Author: mgraff
Date: Fri Oct 30 00:45:29 2009
New Revision: 214
Log:
session stuff might actually work
Modified:
experiments/jelte-configuration/Makefile.in
experiments/jelte-configuration/data.cc
experiments/jelte-configuration/data.h
experiments/jelte-configuration/session.cc
experiments/jelte-configuration/session.h
experiments/jelte-configuration/test.cc
Modified: experiments/jelte-configuration/Makefile.in
==============================================================================
--- experiments/jelte-configuration/Makefile.in (original)
+++ experiments/jelte-configuration/Makefile.in Fri Oct 30 00:45:29 2009
@@ -22,6 +22,10 @@
test: test.o data.o session.o data.h
$(CXX) $(CFLAGS) -o test test.o data.o session.o
+data.o: data.cc data.h
+session.o: session.cc session.h data.h
+test.o: test.cc data.h
+
clean:
rm -f *.o test
Modified: experiments/jelte-configuration/data.cc
==============================================================================
--- experiments/jelte-configuration/data.cc (original)
+++ experiments/jelte-configuration/data.cc Fri Oct 30 00:45:29 2009
@@ -640,6 +640,14 @@
}
ElementPtr
+Element::from_wire(const std::string& s)
+{
+ std::stringstream ss;
+ ss << s;
+ return from_wire(ss, s.length());
+}
+
+ElementPtr
Element::from_wire(std::stringstream& in, int length)
{
//
Modified: experiments/jelte-configuration/data.h
==============================================================================
--- experiments/jelte-configuration/data.h (original)
+++ experiments/jelte-configuration/data.h Fri Oct 30 00:45:29 2009
@@ -128,10 +128,11 @@
// compound factory functions
// return a NULL ElementPtr if there is a parse error or
// the memory could not be allocated
- static ElementPtr create_from_string(std::stringstream &in);
- //static ElementPtr create_from_xml(std::stringstream &in);
-
- static ElementPtr from_wire(std::stringstream &in, int length);
+ static ElementPtr create_from_string(std::stringstream& in);
+ //static ElementPtr create_from_xml(std::stringstream& in);
+
+ static ElementPtr from_wire(std::stringstream& in, int length);
+ static ElementPtr from_wire(const std::string& s);
};
class IntElement : public Element {
Modified: experiments/jelte-configuration/session.cc
==============================================================================
--- experiments/jelte-configuration/session.cc (original)
+++ experiments/jelte-configuration/session.cc Fri Oct 30 00:45:29 2009
@@ -1,10 +1,176 @@
#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_len = sizeof(struct sockaddr_in);
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(9912);
+ sin.sin_addr.s_addr = INADDR_ANY;
+ 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;
+ 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;
+ 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;
+ 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());
+ return (true);
+}
+
+unsigned int
+Session::reply(ElementPtr& envelope, ElementPtr& newmsg)
+{
+ ElementPtr env;
+ 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++);
+}
Modified: experiments/jelte-configuration/session.h
==============================================================================
--- experiments/jelte-configuration/session.h (original)
+++ experiments/jelte-configuration/session.h Fri Oct 30 00:45:29 2009
@@ -7,6 +7,50 @@
#include <iostream>
-namespace ISC { namespace Session {
+#include "data.h"
+
+namespace ISC {
+ namespace CC {
+ class SessionError : public std::exception {
+ public:
+ SessionError(std::string m = "CC Session Error") : msg(m) {}
+ ~SessionError() throw() {}
+ const char* what() const throw() { return msg.c_str(); }
+ private:
+ std::string msg;
+ };
+
+ class Session {
+ private:
+ int sock;
+ int sequence; // the next sequence number to use
+
+ public:
+ std::string lname;
+
+ Session();
+
+ void establish();
+ void disconnect();
+ void sendmsg(ISC::Data::ElementPtr& msg);
+ bool recvmsg(ISC::Data::ElementPtr& msg,
+ bool nonblock = true);
+ void subscribe(std::string group,
+ std::string instance = "*",
+ std::string subtype = "normal");
+ void unsubscribe(std::string group,
+ std::string instance = "*");
+ unsigned int group_sendmsg(ISC::Data::ElementPtr& msg,
+ std::string group,
+ std::string instance = "*",
+ std::string to = "*");
+ bool group_recvmsg(ISC::Data::ElementPtr& envelope,
+ ISC::Data::ElementPtr& msg,
+ bool nonblock = true);
+ unsigned int reply(ISC::Data::ElementPtr& envelope,
+ ISC::Data::ElementPtr& newmsg);
+ };
+ } // namespace CC
+} // namespace ISC
#endif // _ISC_SESSION_H
Modified: experiments/jelte-configuration/test.cc
==============================================================================
--- experiments/jelte-configuration/test.cc (original)
+++ experiments/jelte-configuration/test.cc Fri Oct 30 00:45:29 2009
@@ -1,4 +1,5 @@
#include "data.h"
+#include "session.h"
#include <cstdio>
#include <iostream>
@@ -111,5 +112,13 @@
ElementPtr decoded = Element::from_wire(ss_skan, ss_skan.str().length());
cout << decoded << endl;
+
+ //
+ // Test the session stuff
+ //
+
+ ISC::CC::Session session;
+ session.establish();
+
return 0;
}
More information about the bind10-changes
mailing list