aria2/src/DHTAbstractMessage.cc

99 lines
3.2 KiB
C++
Raw Normal View History

/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "DHTAbstractMessage.h"
#include <cassert>
#include "DHTNode.h"
#include "DHTConnection.h"
#include "DHTMessageDispatcher.h"
#include "DHTMessageFactory.h"
#include "DHTRoutingTable.h"
#include "DHTMessageCallback.h"
#include "bencode.h"
namespace aria2 {
DHTAbstractMessage::DHTAbstractMessage(const SharedHandle<DHTNode>& localNode,
const SharedHandle<DHTNode>& remoteNode,
const std::string& transactionID):
DHTMessage(localNode, remoteNode, transactionID) {}
DHTAbstractMessage::~DHTAbstractMessage() {}
std::string DHTAbstractMessage::getBencodedMessage()
{
2009-04-27 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> Put BDE in aria2 namespace. BDE will also be used to store xml-rpc request. * src/AnnounceList.cc * src/AnnounceList.h * src/BDE.cc: Added * src/BDE.h: Added * src/DHTAbstractMessage.cc * src/DHTAbstractMessage.h * src/DHTAnnouncePeerMessage.cc * src/DHTAnnouncePeerMessage.h * src/DHTAnnouncePeerReplyMessage.cc * src/DHTAnnouncePeerReplyMessage.h * src/DHTFindNodeMessage.cc * src/DHTFindNodeMessage.h * src/DHTFindNodeReplyMessage.cc * src/DHTFindNodeReplyMessage.h * src/DHTGetPeersMessage.cc * src/DHTGetPeersMessage.h * src/DHTGetPeersReplyMessage.cc * src/DHTGetPeersReplyMessage.h * src/DHTMessageFactory.h * src/DHTMessageFactoryImpl.cc * src/DHTMessageFactoryImpl.h * src/DHTMessageReceiver.cc * src/DHTMessageTracker.cc * src/DHTMessageTracker.h * src/DHTPingMessage.cc * src/DHTPingMessage.h * src/DHTPingReplyMessage.cc * src/DHTPingReplyMessage.h * src/DHTQueryMessage.cc * src/DHTQueryMessage.h * src/DHTResponseMessage.cc * src/DHTResponseMessage.h * src/DefaultBtAnnounce.cc * src/DefaultBtContext.cc * src/DefaultBtContext.h * src/HandshakeExtensionMessage.cc * src/Makefile.am * src/PeerListProcessor.h * src/UTPexExtensionMessage.cc * src/bencode.cc * src/bencode.h * test/AnnounceListTest.cc * test/BDETest.cc: Added * test/BencodeTest.cc * test/DHTAnnouncePeerMessageTest.cc * test/DHTAnnouncePeerReplyMessageTest.cc * test/DHTFindNodeMessageTest.cc * test/DHTFindNodeReplyMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/DHTGetPeersReplyMessageTest.cc * test/DHTMessageFactoryImplTest.cc * test/DHTMessageTrackerTest.cc * test/DHTPingMessageTest.cc * test/DHTPingReplyMessageTest.cc * test/Makefile.am * test/MockDHTMessageFactory.h * test/PeerListProcessorTest.cc
2009-04-27 11:45:22 +00:00
BDE msgDict = BDE::dict();
msgDict[T] = getTransactionID();
msgDict[Y] = getType();
msgDict[V] = getVersion();
fillMessage(msgDict);
return bencode::encode(msgDict);
}
bool DHTAbstractMessage::send()
{
std::string message = getBencodedMessage();
ssize_t r = _connection->sendMessage
(reinterpret_cast<const unsigned char*>(message.c_str()),
message.size(),
getRemoteNode()->getIPAddress(),
getRemoteNode()->getPort());
assert(r >= 0);
return r == static_cast<ssize_t>(message.size());
}
void DHTAbstractMessage::setConnection(const WeakHandle<DHTConnection>& connection)
{
_connection = connection;
}
void DHTAbstractMessage::setMessageDispatcher(const WeakHandle<DHTMessageDispatcher>& dispatcher)
{
_dispatcher = dispatcher;
}
void DHTAbstractMessage::setMessageFactory(const WeakHandle<DHTMessageFactory>& factory)
{
_factory = factory;
}
void DHTAbstractMessage::setRoutingTable(const WeakHandle<DHTRoutingTable>& routingTable)
{
_routingTable = routingTable;
}
} // namespace aria2