blob: f59ff14b72567950e7a06702d4e2e8bcd53c7c54 [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;
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070039 NFD_LOG_INFO("Added face id=" << faceId << " uri=" << face->getUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070040
41 face->onReceiveInterest += bind(&Forwarder::onInterest,
Junxiao Shic542b2b2014-03-16 21:45:52 -070042 &m_forwarder, boost::ref(*face), _1);
Junxiao Shia4f2be82014-03-02 22:56:41 -070043 face->onReceiveData += bind(&Forwarder::onData,
Junxiao Shic542b2b2014-03-16 21:45:52 -070044 &m_forwarder, boost::ref(*face), _1);
45 face->onFail += bind(&FaceTable::remove,
46 this, face);
Junxiao Shibd392bf2014-03-17 15:54:11 -070047
48 this->onAdd(face);
Junxiao Shia4f2be82014-03-02 22:56:41 -070049}
50
51void
52FaceTable::remove(shared_ptr<Face> face)
53{
Junxiao Shibd392bf2014-03-17 15:54:11 -070054 this->onRemove(face);
55
Junxiao Shia4f2be82014-03-02 22:56:41 -070056 FaceId faceId = face->getId();
57 m_faces.erase(faceId);
58 face->setId(INVALID_FACEID);
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070059 NFD_LOG_INFO("Removed face id=" << faceId << " uri=" << face->getUri());
Junxiao Shia4f2be82014-03-02 22:56:41 -070060
61 // XXX This clears all subscriptions, because EventEmitter
62 // does not support only removing Forwarder's subscription
63 face->onReceiveInterest.clear();
64 face->onReceiveData .clear();
Junxiao Shic542b2b2014-03-16 21:45:52 -070065 // don't clear onFail because other functions may need to execute
Junxiao Shia4f2be82014-03-02 22:56:41 -070066
67 m_forwarder.getFib().removeNextHopFromAllEntries(face);
68}
69
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070070
71
Junxiao Shia4f2be82014-03-02 22:56:41 -070072} // namespace nfd