blob: c6fa7e7f10c6c71b8d80833cbdb5d7e6e622fa6d [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{
29 startFibCommand("add-nexthop",
30 FibManagementOptions()
31 .setName(prefixToRegister)
32 .setFaceId(0) // self-registration
33 .setCost(0),
34 bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
35 onFail);
36}
37
38void
39Controller::selfDeregisterPrefix(const Name& prefixToRegister,
40 const SuccessCallback& onSuccess,
41 const FailCallback& onFail)
42{
43 if (m_faceId == 0)
44 {
45 if (static_cast<bool>(onFail))
46 onFail("Face ID is not set, should have been set after a successful prefix registration command");
47 return;
48 }
hilataa99e37e2014-02-15 23:52:46 -060049
Alexander Afanasyeve289b532014-02-09 22:14:44 -080050 startFibCommand("remove-nexthop",
51 FibManagementOptions()
52 .setName(prefixToRegister)
53 .setFaceId(m_faceId),
54 bind(onSuccess), onFail);
55}
56
57void
58Controller::startFibCommand(const std::string& command,
59 const FibManagementOptions& options,
60 const FibCommandSucceedCallback& onSuccess,
61 const FailCallback& onFail)
62{
63 Name fibCommandInterestName("/localhost/nfd/fib");
64 fibCommandInterestName
65 .append(command)
66 .append(options.wireEncode());
67
68 Interest fibCommandInterest(fibCommandInterestName);
hilataa99e37e2014-02-15 23:52:46 -060069 // m_keyChain.sign(fibCommandInterest);
Alexander Afanasyeve289b532014-02-09 22:14:44 -080070
71 m_face.expressInterest(fibCommandInterest,
72 bind(&Controller::processFibCommandResponse, this, _2,
73 onSuccess, onFail),
74 bind(onFail, "Command Interest timed out"));
75}
76
77void
78Controller::recordSelfRegisteredFaceId(const FibManagementOptions& entry,
79 const SuccessCallback& onSuccess)
80{
81 m_faceId = entry.getFaceId();
82 onSuccess();
83}
84
Alexander Afanasyeve289b532014-02-09 22:14:44 -080085void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080086Controller::processFibCommandResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080087 const FibCommandSucceedCallback& onSuccess,
88 const FailCallback& onFail)
89{
90 try
91 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -080093 if (response.getCode() != 200)
94 return onFail(response.getText());
95
96 FibManagementOptions options(response.getBody());
97 return onSuccess(options);
98 }
99 catch(ndn::Tlv::Error& e)
100 {
101 if (static_cast<bool>(onFail))
102 return onFail(e.what());
103 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800104}
105
hilataa99e37e2014-02-15 23:52:46 -0600106void
107Controller::startFaceCommand(const std::string& command,
108 const FaceManagementOptions& options,
109 const FaceCommandSucceedCallback& onSuccess,
110 const FailCallback& onFail)
111{
112 Name faceCommandInterestName("/localhost/nfd/faces");
113 faceCommandInterestName
114 .append(command)
115 .append(options.wireEncode());
116
117 Interest faceCommandInterest(faceCommandInterestName);
118 // m_keyChain.sign(fibCommandInterest);
119
120 m_face.expressInterest(faceCommandInterest,
121 bind(&Controller::processFaceCommandResponse, this, _2,
122 onSuccess, onFail),
123 bind(onFail, "Command Interest timed out"));
124}
125
126void
127Controller::processFaceCommandResponse(Data& data,
128 const FaceCommandSucceedCallback& onSuccess,
129 const FailCallback& onFail)
130{
131 try
132 {
133 ControlResponse response(data.getContent().blockFromValue());
134 if (response.getCode() != 200)
135 return onFail(response.getText());
136
137 FaceManagementOptions options(response.getBody());
138 return onSuccess(options);
139 }
140 catch(ndn::Tlv::Error& e)
141 {
142 if (static_cast<bool>(onFail))
143 return onFail(e.what());
144 }
145}
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800146} // namespace nfd
147} // namespace ndn