Commit 14f0c667 authored by Evan Hunt's avatar Evan Hunt
Browse files

parkinglot now works with command channel


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@265 e5f2f494-b856-4b98-b285-d166d9295462
parent 858bd245
......@@ -25,6 +25,7 @@
#include <isc/util.h>
#include "msgbuf.h"
#include "cc.h"
msgbuf_t *
msgbuf_create(isc_mem_t *mctx, unsigned int length)
......
AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_srcdir)/ext
bin_PROGRAMS = parkinglot
parkinglot_SOURCES = common.cc common.h main.cc zoneset.h parkinglot.cc parkinglot.h
parkinglot_SOURCES = common.cc common.h zoneset.h parkinglot.cc parkinglot.h ccsession.cc ccsession.h main.cc
parkinglot_LDADD = $(top_srcdir)/src/lib/dns/libdns.a $(top_srcdir)/src/lib/cc/cpp/libcc.a
......@@ -15,48 +15,46 @@
// $Id$
#include <stdexcept>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <cc/cpp/data.h>
#include <cc/cpp/session.h>
using std::string;
#include "common.h"
#include "ccsession.h"
class SessionManager {
public:
SessionManager();
private:
ISC::CC::Session session_;
};
using namespace std;
SessionManager::SessionManager() :
session_(ISC::CC::Session())
{
CommandSession::CommandSession() : session_(ISC::CC::Session()) {
try {
session_.establish();
session_.subscribe("ParkingLot");
session_.subscribe("Boss");
} catch (...) {
throw std::runtime_error("SessionManager: failed to open sessions");
throw std::runtime_error("CommandSession: failed to open sessions");
}
}
std::pair<string, string>
getCommand(ISC::CC::Session& session)
{
ISC::Data::ElementPtr ep, routing, data;
std::pair<std::string, std::string>
CommandSession::getCommand() {
ISC::Data::ElementPtr cmd, routing, data, ep;
string s;
session.group_recvmsg(routing, data, false);
ep = data->get("zone_added");
if (ep != NULL) {
return std::pair<string, string>("zone_added", ep->string_value());
}
ep = data->get("zone_deleted");
if (ep != NULL) {
return std::pair<string, string>("zone_deleted", ep->string_value());
session_.group_recvmsg(routing, data, false);
cmd = data->get("command");
ep = cmd->get(0);
s = ep->string_value();
if (s == "addzone" || s == "delzone") {
return std::pair<string, string>(s, cmd->get(1)->string_value());
}
ep = data->get("shutdown");
if (ep != NULL) {
return std::pair<string, string>("shutdown", "");
return std::pair<string, string>(s, "");
}
return std::pair<string, string>("unknown", "");
}
......@@ -32,6 +32,7 @@
#include "zoneset.h"
#include "parkinglot.h"
#include "ccsession.h"
#include "common.h"
......@@ -69,17 +70,15 @@ main(int argc, char* argv[]) {
ParkingLot plot(port);
// initialize command channel
ISC::CC::Session session;
session.establish();
session.subscribe("parkinglot");
CommandSession cs;
// main server loop
fd_set fds;
int ps = plot.getSocket();
int ss = session.getSocket();
int ss = cs.getSocket();
int nfds = max(ps, ss) + 1;
cout << "server running" << endl;
cout << "Server started." << endl;
while (true) {
FD_ZERO(&fds);
FD_SET(ps, &fds);
......@@ -89,8 +88,13 @@ main(int argc, char* argv[]) {
if (n < 0)
throw FatalError("select error");
if (n != 0 && FD_ISSET(ps, &fds))
if (FD_ISSET(ps, &fds))
plot.processMessage();
if (FD_ISSET(ss, &fds)) {
pair<string,string> cmd = cs.getCommand();
plot.command(cmd);
}
}
return (0);
......
......@@ -156,3 +156,13 @@ ParkingLot::processMessage() {
msg.getBuffer().sendTo(s, *sa, sa_len);
}
}
void
ParkingLot::command(pair<string,string> cmd) {
if (cmd.first == "addzone")
zones.serve(cmd.second);
else if (cmd.first == "delzone")
zones.forget(cmd.second);
else if (cmd.first == "shutdown")
exit(0);
}
......@@ -25,6 +25,7 @@ class ParkingLot {
virtual ~ParkingLot() {};
int getSocket() { return(sock); }
void processMessage();
void command(std::pair<std::string,std::string>);
private:
isc::dns::Rdata::RdataPtr ns1, ns2, ns3, a, aaaa, soa;
......
......@@ -22,6 +22,7 @@
class ZoneSet : std::set<std::string> {
public:
void serve(std::string s) { this->insert(s); }
void forget(std::string s) { this->erase(s); }
bool contains(std::string s) {
return (this->find(s) != this->end());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment