blob: ca7b56fb32288ed7761a4e44e231594073e9742b [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"
9
10namespace nfd {
11
12NFD_LOG_INIT("FaceTable");
13
14FaceTable::FaceTable(Forwarder& forwarder)
15 : m_forwarder(forwarder)
16 , m_lastFaceId(0)
17{
18}
19
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070020FaceTable::~FaceTable()
21{
22
23}
24
Junxiao Shia4f2be82014-03-02 22:56:41 -070025void
26FaceTable::add(shared_ptr<Face> face)
27{
Alexander Afanasyevbc521a52014-03-26 23:31:55 -070028 if (face->getId() != INVALID_FACEID &&
29 m_faces.count(face->getId()) > 0)
30 {
31 NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table");
32 return;
33 }
34
Junxiao Shia4f2be82014-03-02 22:56:41 -070035 FaceId faceId = ++m_lastFaceId;
36 face->setId(faceId);
37 m_faces[faceId] = face;
38 NFD_LOG_INFO("addFace id=" << faceId);
39
40 face->onReceiveInterest += bind(&Forwarder::onInterest,
Junxiao Shic542b2b2014-03-16 21:45:52 -070041 &m_forwarder, boost::ref(*face), _1);
Junxiao Shia4f2be82014-03-02 22:56:41 -070042 face->onReceiveData += bind(&Forwarder::onData,
Junxiao Shic542b2b2014-03-16 21:45:52 -070043 &m_forwarder, boost::ref(*face), _1);
44 face->onFail += bind(&FaceTable::remove,
45 this, face);
Junxiao Shibd392bf2014-03-17 15:54:11 -070046
47 this->onAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070048}
49
50void
51FaceTable::remove(shared_ptr<Face> face)
52{
Junxiao Shibd392bf2014-03-17 15:54:11 -070053 this->onRemove(face);
54
Junxiao Shia4f2be82014-03-02 22:56:41 -070055 FaceId faceId = face->getId();
56 m_faces.erase(faceId);
57 face->setId(INVALID_FACEID);
58 NFD_LOG_INFO("removeFace id=" << faceId);
59
60 // XXX This clears all subscriptions, because EventEmitter
61 // does not support only removing Forwarder's subscription
62 face->onReceiveInterest.clear();
63 face->onReceiveData .clear();
Junxiao Shic542b2b2014-03-16 21:45:52 -070064 // don't clear onFail because other functions may need to execute
Junxiao Shia4f2be82014-03-02 22:56:41 -070065
66 m_forwarder.getFib().removeNextHopFromAllEntries(face);
67}
68
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070069
70
Junxiao Shia4f2be82014-03-02 22:56:41 -070071} // namespace nfd