IBR-DTN  1.0.0
EMailConvergenceLayer.cpp
Go to the documentation of this file.
1 /*
2  * EMailConvergenceLayer.cpp
3  *
4  * Copyright (C) 2013 IBR, TU Braunschweig
5  *
6  * Written-by: Björn Gernert <mail@bjoern-gernert.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
23 #include "net/EMailSmtpService.h"
25 #include "core/BundleCore.h"
26 #include "core/EventDispatcher.h"
27 
28 #include <ibrdtn/utils/Utils.h>
29 #include <ibrcommon/Logger.h>
30 
31 namespace dtn
32 {
33  namespace net
34  {
36  _config(daemon::Configuration::getInstance().getEMail()),
37  _smtp(EMailSmtpService::getInstance()),
38  _imap(EMailImapService::getInstance()),
39  _lastSmtpTaskTime(0),
40  _lastImapTaskTime(0)
41  {
43  }
44 
46  {
48  }
49 
51  {
52  if (time.getAction() == dtn::core::TIME_SECOND_TICK)
53  {
54  if(_lastSmtpTaskTime + _config.getSmtpSubmitInterval() < time.getTimestamp().get<size_t>()
55  && _config.getSmtpSubmitInterval() > 0)
56  {
57  _smtp.submitQueue();
58  _lastSmtpTaskTime = time.getTimestamp().get<size_t>();
59  }
60 
61  if(_lastImapTaskTime + _config.getImapLookupInterval() < time.getTimestamp().get<size_t>()
62  && _config.getImapLookupInterval() > 0)
63  {
64  _imap.fetchMails();
65  _lastImapTaskTime = time.getTimestamp().get<size_t>();
66  }
67  }
68  }
69 
70  void EMailConvergenceLayer::onUpdateBeacon(const ibrcommon::vinterface&, DiscoveryBeacon &beacon)
72  {
73  beacon.addService(DiscoveryService(getDiscoveryProtocol(), "email=" + _config.getOwnAddress()));
74  }
75 
77  {
79  }
80 
82  const dtn::net::BundleTransfer &job)
83  {
84  // Check if node supports email convergence layer
85  const std::list<dtn::core::Node::URI> uri_list = node.get(dtn::core::Node::CONN_EMAIL);
86  if (uri_list.empty())
87  {
90  return;
91  }
92 
93  // Get recipient
94  std::string recipient = dtn::utils::Utils::tokenize("//", node.getEID().getString())[1];
95 
96  // Create new Task
97  EMailSmtpService::Task *t = new EMailSmtpService::Task(node, job, recipient);
98 
99  // Submit Task
100  if(_config.getSmtpSubmitInterval() <= 0)
101  {
102  _smtp.submitNow(t);
103  }else{
104  _smtp.queueTask(t);
105  }
106 
107  IBRCOMMON_LOGGER(info) << "EMail Convergence Layer: Bundle " << t->getJob().getBundle().toString() << " stored in submit queue" << IBRCOMMON_LOGGER_ENDL;
108 
109  }
110 
111  const std::string EMailConvergenceLayer::getName() const
112  {
113  return "EMailConvergenceLayer";
114  }
115 
117 
119  {
120  // register as discovery beacon handler
122  }
123 
125  {
126  // un-register as discovery beacon handler
128  }
129  }
130 }
virtual const std::string getName() const
void raiseEvent(const dtn::core::TimeEvent &event)
static void add(EventReceiver< E > *receiver)
static void raise(const dtn::data::EID &peer, const dtn::data::BundleID &id, const AbortReason reason=REASON_UNDEFINED)
static void remove(const EventReceiver< E > *receiver)
dtn::net::DiscoveryAgent & getDiscoveryAgent()
Definition: BundleCore.cpp:265
void onUpdateBeacon(const ibrcommon::vinterface &iface, DiscoveryBeacon &beacon)
void unregisterService(const ibrcommon::vinterface &iface, const dtn::net::DiscoveryBeaconHandler *handler)
void queue(const dtn::core::Node &node, const dtn::net::BundleTransfer &job)
std::list< URI > get(Node::Protocol proto) const
Definition: Node.cpp:325
const dtn::data::MetaBundle & getBundle() const
std::string getString() const
Definition: EID.cpp:374
dtn::core::Node::Protocol getDiscoveryProtocol() const
const dtn::data::EID & getEID() const
Definition: Node.cpp:406
void registerService(const ibrcommon::vinterface &iface, dtn::net::DiscoveryBeaconHandler *handler)
static std::vector< std::string > tokenize(const std::string &token, const std::string &data, const std::string::size_type max=std::string::npos)
Definition: Utils.cpp:60
static BundleCore & getInstance()
Definition: BundleCore.cpp:82