blob: 00a5378cbc552648f380901ca7bf3aa2934c3d31 [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"
12#include "nfd-control-response.hpp"
13
14namespace ndn {
15namespace nfd {
16
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080017Controller::Controller(Face& face)
Alexander Afanasyeve289b532014-02-09 22:14:44 -080018 : m_face(face)
19 , m_faceId(0)
20{
21}
22
23void
24Controller::selfRegisterPrefix(const Name& prefixToRegister,
25 const SuccessCallback& onSuccess,
26 const FailCallback& onFail)
27{
28 startFibCommand("add-nexthop",
29 FibManagementOptions()
30 .setName(prefixToRegister)
31 .setFaceId(0) // self-registration
32 .setCost(0),
33 bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
34 onFail);
35}
36
37void
38Controller::selfDeregisterPrefix(const Name& prefixToRegister,
39 const SuccessCallback& onSuccess,
40 const FailCallback& onFail)
41{
42 if (m_faceId == 0)
43 {
44 if (static_cast<bool>(onFail))
45 onFail("Face ID is not set, should have been set after a successful prefix registration command");
46 return;
47 }
48
49 startFibCommand("remove-nexthop",
50 FibManagementOptions()
51 .setName(prefixToRegister)
52 .setFaceId(m_faceId),
53 bind(onSuccess), onFail);
54}
55
56void
57Controller::startFibCommand(const std::string& command,
58 const FibManagementOptions& options,
59 const FibCommandSucceedCallback& onSuccess,
60 const FailCallback& onFail)
61{
62 Name fibCommandInterestName("/localhost/nfd/fib");
63 fibCommandInterestName
64 .append(command)
65 .append(options.wireEncode());
66
67 Interest fibCommandInterest(fibCommandInterestName);
68 m_keyChain.sign(fibCommandInterest);
69
70 m_face.expressInterest(fibCommandInterest,
71 bind(&Controller::processFibCommandResponse, this, _2,
72 onSuccess, onFail),
73 bind(onFail, "Command Interest timed out"));
74}
75
76void
77Controller::recordSelfRegisteredFaceId(const FibManagementOptions& entry,
78 const SuccessCallback& onSuccess)
79{
80 m_faceId = entry.getFaceId();
81 onSuccess();
82}
83
84// void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080085// processFaceActionResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080086// const FaceOperationSucceedCallback& onSuccess,
87// const FailCallback& onFail);
88
89void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080090Controller::processFibCommandResponse(Data& data,
Alexander Afanasyeve289b532014-02-09 22:14:44 -080091 const FibCommandSucceedCallback& onSuccess,
92 const FailCallback& onFail)
93{
94 try
95 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080096 ControlResponse response(data.getContent().blockFromValue());
Alexander Afanasyeve289b532014-02-09 22:14:44 -080097
98 if (response.getCode() != 200)
99 return onFail(response.getText());
100
101 FibManagementOptions options(response.getBody());
102 return onSuccess(options);
103 }
104 catch(ndn::Tlv::Error& e)
105 {
106 if (static_cast<bool>(onFail))
107 return onFail(e.what());
108 }
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800109}
110
111} // namespace nfd
112} // namespace ndn