blob: 735c927761a8e317ac95bec7d72043aa9cd5ea12 [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)
20 , m_faceId(0)
21{
22}
23
24void
25Controller::selfRegisterPrefix(const Name& prefixToRegister,
26 const SuccessCallback& onSuccess,
27 const FailCallback& onFail)
28{
Alexander Afanasyev21abc102014-02-18 18:59:02 -080029 // two stage process:
30 // 1. insert FIB entry
31 // 2. add-nexthop <self> to the FIB entry
32
33 // Step 1.
34 startFibCommand("insert",
Alexander Afanasyeve289b532014-02-09 22:14:44 -080035 FibManagementOptions()
Alexander Afanasyev21abc102014-02-18 18:59:02 -080036 .setName(prefixToRegister),
37 bind(&Controller::selfRegisterPrefixAddNextop, this, _1, onSuccess, onFail),
38 onFail);
39}
40
41void
42Controller::selfRegisterPrefixAddNextop(const FibManagementOptions& entry,
43 const SuccessCallback& onSuccess,
44 const FailCallback& onFail)
45{
46 // Step 2.
47 startFibCommand("add-nexthop",
48 FibManagementOptions(entry) // prefixToRegister should be inside the entry
49 .setFaceId(0) // self-registration
50 .setCost(0),
Alexander Afanasyeve289b532014-02-09 22:14:44 -080051 bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
52 onFail);
53}
54
Alexander Afanasyev21abc102014-02-18 18:59:02 -080055
Alexander Afanasyeve289b532014-02-09 22:14:44 -080056void
57Controller::selfDeregisterPrefix(const Name& prefixToRegister,
58 const SuccessCallback& onSuccess,
59 const FailCallback& onFail)
60{
61 if (m_faceId == 0)
62 {
63 if (static_cast<bool>(onFail))
64 onFail("Face ID is not set, should have been set after a successful prefix registration command");
65 return;
66 }
hilataa99e37e2014-02-15 23:52:46 -060067
Alexander Afanasyeve289b532014-02-09 22:14:44 -080068 startFibCommand("remove-nexthop",
69 FibManagementOptions()
Alexander Afanasyev21abc102014-02-18 18:59:02 -080070 .setName(prefixToRegister)
71 .setFaceId(m_faceId),
Alexander Afanasyeve289b532014-02-09 22:14:44 -080072 bind(onSuccess), onFail);
73}
74
75void
76Controller::startFibCommand(const std::string& command,
77 const FibManagementOptions& options,
78 const FibCommandSucceedCallback& onSuccess,
79 const FailCallback& onFail)
80{
81 Name fibCommandInterestName("/localhost/nfd/fib");
82 fibCommandInterestName
83 .append(command)
84 .append(options.wireEncode());
85
86 Interest fibCommandInterest(fibCommandInterestName);
hilataa99e37e2014-02-15 23:52:46 -060087 // m_keyChain.sign(fibCommandInterest);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080088
89 m_face.expressInterest(fibCommandInterest,
90 bind(&Controller::processFibCommandResponse, this, _2,
91 onSuccess, onFail),
92 bind(onFail, "Command Interest timed out"));
93}
94
95void
96Controller::recordSelfRegisteredFaceId(const FibManagementOptions& entry,
97 const SuccessCallback& onSuccess)
98{
99 m_faceId = entry.getFaceId();
100 onSuccess();
101}
102
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800103void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800104Controller::processFibCommandResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800105 const FibCommandSucceedCallback& onSuccess,
106 const FailCallback& onFail)
107{
108 try
109 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800110 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800111 if (response.getCode() != 200)
112 return onFail(response.getText());
113
114 FibManagementOptions options(response.getBody());
115 return onSuccess(options);
116 }
117 catch(ndn::Tlv::Error& e)
118 {
119 if (static_cast<bool>(onFail))
120 return onFail(e.what());
121 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800122}
123
hilataa99e37e2014-02-15 23:52:46 -0600124void
125Controller::startFaceCommand(const std::string& command,
126 const FaceManagementOptions& options,
127 const FaceCommandSucceedCallback& onSuccess,
128 const FailCallback& onFail)
129{
130 Name faceCommandInterestName("/localhost/nfd/faces");
131 faceCommandInterestName
132 .append(command)
133 .append(options.wireEncode());
134
135 Interest faceCommandInterest(faceCommandInterestName);
136 // m_keyChain.sign(fibCommandInterest);
137
138 m_face.expressInterest(faceCommandInterest,
139 bind(&Controller::processFaceCommandResponse, this, _2,
140 onSuccess, onFail),
141 bind(onFail, "Command Interest timed out"));
142}
143
144void
145Controller::processFaceCommandResponse(Data& data,
146 const FaceCommandSucceedCallback& onSuccess,
147 const FailCallback& onFail)
148{
149 try
150 {
151 ControlResponse response(data.getContent().blockFromValue());
152 if (response.getCode() != 200)
153 return onFail(response.getText());
154
155 FaceManagementOptions options(response.getBody());
156 return onSuccess(options);
157 }
158 catch(ndn::Tlv::Error& e)
159 {
160 if (static_cast<bool>(onFail))
161 return onFail(e.what());
162 }
163}
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800164} // namespace nfd
165} // namespace ndn