blob: 7a99693ba136340be67dbcc74113d06a893227a2 [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{
Obaid6e7f5f12014-03-11 14:46:10 -050028 fibAddNextHop(prefixToRegister, 0, 0, bind(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{
Obaid6e7f5f12014-03-11 14:46:10 -050036 fibRemoveNextHop(prefixToDeRegister, 0, bind(onSuccess), onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000037}
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,
Obaid6e7f5f12014-03-11 14:46:10 -050041 const FibCommandSucceedCallback& onSuccess,
42 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +000043{
44 startFibCommand("add-nexthop",
45 FibManagementOptions()
46 .setName(prefix)
47 .setFaceId(faceId)
48 .setCost(cost),
Obaid6e7f5f12014-03-11 14:46:10 -050049 onSuccess, onFail);
Alexander Afanasyev884280c2014-03-07 09:40:39 +000050}
51
52void
53Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
Obaid6e7f5f12014-03-11 14:46:10 -050054 const FibCommandSucceedCallback& onSuccess,
55 const FailCallback& onFail)
Alexander Afanasyev884280c2014-03-07 09:40:39 +000056{
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),
Obaid6e7f5f12014-03-11 14:46:10 -050061 onSuccess, onFail);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080062}
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{
Obaid6e7f5f12014-03-11 14:46:10 -050089 /// \todo Add validation of incoming Data
90
Alexander Afanasyeve289b532014-02-09 22:14:44 -080091 try
92 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080093 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -080094 if (response.getCode() != 200)
95 return onFail(response.getText());
96
97 FibManagementOptions options(response.getBody());
98 return onSuccess(options);
99 }
100 catch(ndn::Tlv::Error& e)
101 {
102 if (static_cast<bool>(onFail))
103 return onFail(e.what());
104 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800105}
106
hilataa99e37e2014-02-15 23:52:46 -0600107void
108Controller::startFaceCommand(const std::string& command,
109 const FaceManagementOptions& options,
110 const FaceCommandSucceedCallback& onSuccess,
111 const FailCallback& onFail)
112{
113 Name faceCommandInterestName("/localhost/nfd/faces");
114 faceCommandInterestName
115 .append(command)
116 .append(options.wireEncode());
117
118 Interest faceCommandInterest(faceCommandInterestName);
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000119 m_commandInterestGenerator.generate(faceCommandInterest);
hilataa99e37e2014-02-15 23:52:46 -0600120
121 m_face.expressInterest(faceCommandInterest,
122 bind(&Controller::processFaceCommandResponse, this, _2,
123 onSuccess, onFail),
124 bind(onFail, "Command Interest timed out"));
125}
126
127void
128Controller::processFaceCommandResponse(Data& data,
129 const FaceCommandSucceedCallback& onSuccess,
130 const FailCallback& onFail)
131{
Obaid6e7f5f12014-03-11 14:46:10 -0500132 /// \todo Add validation of incoming Data
133
hilataa99e37e2014-02-15 23:52:46 -0600134 try
135 {
136 ControlResponse response(data.getContent().blockFromValue());
137 if (response.getCode() != 200)
138 return onFail(response.getText());
139
140 FaceManagementOptions options(response.getBody());
141 return onSuccess(options);
142 }
143 catch(ndn::Tlv::Error& e)
144 {
145 if (static_cast<bool>(onFail))
146 return onFail(e.what());
147 }
148}
Alexander Afanasyev884280c2014-03-07 09:40:39 +0000149
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800150} // namespace nfd
151} // namespace ndn