blob: 7b096fa6e24b8da39867c294099c1f1a0be65d63 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "face-table.hpp"
8#include "forwarder.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -06009#include "core/logger.hpp"
Junxiao Shia4f2be82014-03-02 22:56:41 -070010
11namespace nfd {
12
13NFD_LOG_INIT("FaceTable");
14
15FaceTable::FaceTable(Forwarder& forwarder)
16 : m_forwarder(forwarder)
17 , m_lastFaceId(0)
18{
19}
20
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070021FaceTable::~FaceTable()
22{
23
24}
25
Junxiao Shia4f2be82014-03-02 22:56:41 -070026void
27FaceTable::add(shared_ptr<Face> face)
28{
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070029 if (face->getId() != INVALID_FACEID &&
30 m_faces.count(face->getId()) > 0)
31 {
32 NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table");
33 return;
34 }
35
Junxiao Shia4f2be82014-03-02 22:56:41 -070036 FaceId faceId = ++m_lastFaceId;
37 face->setId(faceId);
38 m_faces[faceId] = face;
Junxiao Shi6e694322014-04-03 10:27:13 -070039 NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() <<
40 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070041
42 face->onReceiveInterest += bind(&Forwarder::onInterest,
Junxiao Shic542b2b2014-03-16 21:45:52 -070043 &m_forwarder, boost::ref(*face), _1);
Junxiao Shia4f2be82014-03-02 22:56:41 -070044 face->onReceiveData += bind(&Forwarder::onData,
Junxiao Shic542b2b2014-03-16 21:45:52 -070045 &m_forwarder, boost::ref(*face), _1);
46 face->onFail += bind(&FaceTable::remove,
47 this, face);
Junxiao Shibd392bf2014-03-17 15:54:11 -070048
49 this->onAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070050}
51
52void
53FaceTable::remove(shared_ptr<Face> face)
54{
Junxiao Shibd392bf2014-03-17 15:54:11 -070055 this->onRemove(face);
56
Junxiao Shia4f2be82014-03-02 22:56:41 -070057 FaceId faceId = face->getId();
58 m_faces.erase(faceId);
59 face->setId(INVALID_FACEID);
Junxiao Shi6e694322014-04-03 10:27:13 -070060 NFD_LOG_INFO("Removed face id=" << faceId << " remote=" << face->getRemoteUri() <<
61 " local=" << face->getLocalUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070062
63 // XXX This clears all subscriptions, because EventEmitter
64 // does not support only removing Forwarder's subscription
65 face->onReceiveInterest.clear();
66 face->onReceiveData .clear();
Junxiao Shic542b2b2014-03-16 21:45:52 -070067 // don't clear onFail because other functions may need to execute
Junxiao Shia4f2be82014-03-02 22:56:41 -070068
69 m_forwarder.getFib().removeNextHopFromAllEntries(face);
70}
71
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070072
73
Junxiao Shia4f2be82014-03-02 22:56:41 -070074} // namespace nfd