IBR-DTNSuite  0.12
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 #include "core/TimeEvent.h"
28 
29 #include <ibrdtn/utils/Utils.h>
30 #include <ibrcommon/Logger.h>
31 
32 namespace dtn
33 {
34  namespace net
35  {
37  _config(daemon::Configuration::getInstance().getEMail()),
38  _smtp(EMailSmtpService::getInstance()),
39  _imap(EMailImapService::getInstance()),
40  _lastSmtpTaskTime(0),
41  _lastImapTaskTime(0)
42  {
44  }
45 
47  {
49  }
50 
52  {
53  try {
54  const dtn::core::TimeEvent &time =
55  dynamic_cast<const dtn::core::TimeEvent&>(*event);
57  {
58  if(_lastSmtpTaskTime + _config.getSmtpSubmitInterval() < time.getTimestamp().get<size_t>()
59  && _config.getSmtpSubmitInterval() > 0)
60  {
61  _smtp.submitQueue();
62  _lastSmtpTaskTime = time.getTimestamp().get<size_t>();
63  }
64 
65  if(_lastImapTaskTime + _config.getImapLookupInterval() < time.getTimestamp().get<size_t>()
66  && _config.getImapLookupInterval() > 0)
67  {
68  _imap.fetchMails();
69  _lastImapTaskTime = time.getTimestamp().get<size_t>();
70  }
71  }
72  } catch (const std::bad_cast&) { };
73  }
74 
77  {
78  beacon.addService(DiscoveryService(getDiscoveryProtocol(), "email=" + _config.getOwnAddress()));
79  }
80 
82  {
84  }
85 
87  const dtn::net::BundleTransfer &job)
88  {
89  // Check if node supports email convergence layer
90  const std::list<dtn::core::Node::URI> uri_list = node.get(dtn::core::Node::CONN_EMAIL);
91  if (uri_list.empty())
92  {
95  return;
96  }
97 
98  // Get recipient
99  std::string recipient = dtn::utils::Utils::tokenize("//", node.getEID().getString())[1];
100 
101  // Create new Task
102  EMailSmtpService::Task *t = new EMailSmtpService::Task(node, job, recipient);
103 
104  // Submit Task
105  if(_config.getSmtpSubmitInterval() <= 0)
106  {
107  _smtp.submitNow(t);
108  }else{
109  _smtp.queueTask(t);
110  }
111 
112  IBRCOMMON_LOGGER(info) << "EMail Convergence Layer: Bundle " << t->getJob().getBundle().toString() << " stored in submit queue" << IBRCOMMON_LOGGER_ENDL;
113 
114  }
115 
116  const std::string EMailConvergenceLayer::getName() const
117  {
118  return "EMailConvergenceLayer";
119  }
120 
122 
124  {
125  // register as discovery beacon handler
127  }
128 
130  {
131  // un-register as discovery beacon handler
133  }
134  }
135 }