blob: 723c984f5ec5f845f45da520355afaf23e728708 [file] [log] [blame]
Alexander Afanasyeve289b532014-02-09 22:14:44 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "common.hpp"
Alexander Afanasyev0222fba2014-02-09 23:16:02 -08008#include "../face.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -08009
10#include "nfd-controller.hpp"
11#include "nfd-fib-management-options.hpp"
hilataa99e37e2014-02-15 23:52:46 -060012#include "nfd-face-management-options.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080013#include "nfd-control-response.hpp"
14
15namespace ndn {
16namespace nfd {
17
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080018Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080019 : m_face(face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020{
21}
22
23void
24Controller::selfRegisterPrefix(const Name& prefixToRegister,
25 const SuccessCallback& onSuccess,
26 const FailCallback& onFail)
27{
Alexander Afanasyev884280c2014-03-07 09:40:39 +000028 fibAddNextHop(prefixToRegister, 0, 0, onSuccess, onFail);
Alexander Afanasyev21abc102014-02-18 18:59:02 -080029}
30
31void
Alexander Afanasyev884280c2014-03-07 09:40:39 +000032Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080033 const SuccessCallback& onSuccess,
34 const FailCallback& onFail)
35{
Alexander Afanasyev884280c2014-03-07 09:40:39 +000036 fibRemoveNextHop(prefixToDeRegister, 0, onSuccess, onFail);
37}
hilataa99e37e2014-02-15 23:52:46 -060038
Alexander Afanasyev884280c2014-03-07 09:40:39 +000039void
40Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
41 const SuccessCallback& onSuccess,
42 const FailCallback& onFail)
43{
44 startFibCommand("add-nexthop",
45 FibManagementOptions()
46 .setName(prefix)
47 .setFaceId(faceId)
48 .setCost(cost),
49 bind(onSuccess), onFail);
50}
51
52void
53Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
54 const SuccessCallback& onSuccess,
55 const FailCallback& onFail)
56{
Alexander Afanasyeve289b532014-02-09 22:14:44 -080057 startFibCommand("remove-nexthop",
58 FibManagementOptions()
Alexander Afanasyev884280c2014-03-07 09:40:39 +000059 .setName(prefix)
60 .setFaceId(faceId),
Alexander Afanasyeve289b532014-02-09 22:14:44 -080061 bind(onSuccess), onFail);
62}
63
64void
65Controller::startFibCommand(const std::string& command,
66 const FibManagementOptions& options,
67 const FibCommandSucceedCallback& onSuccess,
68 const FailCallback& onFail)
69{
70 Name fibCommandInterestName("/localhost/nfd/fib");
71 fibCommandInterestName
72 .append(command)
73 .append(options.wireEncode());
74
75 Interest fibCommandInterest(fibCommandInterestName);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000076 m_commandInterestGenerator.generate(fibCommandInterest);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080077
78 m_face.expressInterest(fibCommandInterest,
79 bind(&Controller::processFibCommandResponse, this, _2,
80 onSuccess, onFail),
81 bind(onFail, "Command Interest timed out"));
82}
83
84void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080085Controller::processFibCommandResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080086 const FibCommandSucceedCallback& onSuccess,
87 const FailCallback& onFail)
88{
89 try
90 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080091 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -080092 if (response.getCode() != 200)
93 return onFail(response.getText());
94
95 FibManagementOptions options(response.getBody());
96 return onSuccess(options);
97 }
98 catch(ndn::Tlv::Error& e)
99 {
100 if (static_cast<bool>(onFail))
101 return onFail(e.what());
102 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800103}
104
hilataa99e37e2014-02-15 23:52:46 -0600105void
106Controller::startFaceCommand(const std::string& command,
107 const FaceManagementOptions& options,
108 const FaceCommandSucceedCallback& onSuccess,
109 const FailCallback& onFail)
110{
111 Name faceCommandInterestName("/localhost/nfd/faces");
112 faceCommandInterestName
113 .append(command)
114 .append(options.wireEncode());
115
116 Interest faceCommandInterest(faceCommandInterestName);
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000117 m_commandInterestGenerator.generate(faceCommandInterest);
hilataa99e37e2014-02-15 23:52:46 -0600118
119 m_face.expressInterest(faceCommandInterest,
120 bind(&Controller::processFaceCommandResponse, this, _2,
121 onSuccess, onFail),
122 bind(onFail, "Command Interest timed out"));
123}
124
125void
126Controller::processFaceCommandResponse(Data& data,
127 const FaceCommandSucceedCallback& onSuccess,
128 const FailCallback& onFail)
129{
130 try
131 {
132 ControlResponse response(data.getContent().blockFromValue());
133 if (response.getCode() != 200)
134 return onFail(response.getText());
135
136 FaceManagementOptions options(response.getBody());
137 return onSuccess(options);
138 }
139 catch(ndn::Tlv::Error& e)
140 {
141 if (static_cast<bool>(onFail))
142 return onFail(e.what());
143 }
144}
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000145
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800146} // namespace nfd
147} // namespace ndn