| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 | /* <!-- 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 "PeerMessageFactory.h"#include "PeerInteraction.h"#include "PeerMessageUtil.h"#include "ChokeMessage.h"#include "UnchokeMessage.h"#include "InterestedMessage.h"#include "NotInterestedMessage.h"#include "HaveMessage.h"#include "BitfieldMessage.h"#include "RequestMessage.h"#include "CancelMessage.h"#include "PieceMessage.h"#include "HandshakeMessage.h"#include "KeepAliveMessage.h"#include "PortMessage.h"#include "HaveAllMessage.h"#include "HaveNoneMessage.h"#include "RejectMessage.h"#include "AllowedFastMessage.h"#include "SuggestPieceMessage.h"#include "RequestSlot.h"#include "DlAbortEx.h"PeerMessageFactory::PeerMessageFactory(int cuid,				       PeerInteraction* peerInteraction,				       const PeerHandle& peer)  :cuid(cuid),   peerInteraction(peerInteraction),   peer(peer) {}PeerMessageFactory::~PeerMessageFactory() {}PeerMessageHandle PeerMessageFactory::createPeerMessage(const char* msg, int msgLength) const {  PeerMessage* peerMessage;  if(msgLength == 0) {    // keep-alive    peerMessage = new KeepAliveMessage();  } else {    int id = PeerMessageUtil::getId(msg);    switch(id) {    case ChokeMessage::ID:      peerMessage = ChokeMessage::create(msg, msgLength);      break;    case UnchokeMessage::ID:      peerMessage = UnchokeMessage::create(msg, msgLength);      break;    case InterestedMessage::ID:      peerMessage = InterestedMessage::create(msg, msgLength);      break;    case NotInterestedMessage::ID:      peerMessage = NotInterestedMessage::create(msg, msgLength);      break;    case HaveMessage::ID:      peerMessage = HaveMessage::create(msg, msgLength);      ((HaveMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->					     pieces);      break;    case BitfieldMessage::ID:      peerMessage = BitfieldMessage::create(msg, msgLength);      ((BitfieldMessage*)peerMessage)->setPieces(peerInteraction->						 getTorrentMan()->pieces);      break;    case RequestMessage::ID:      peerMessage = RequestMessage::create(msg, msgLength);      ((RequestMessage*)peerMessage)->setPieces(peerInteraction->						getTorrentMan()->pieces);      ((RequestMessage*)peerMessage)->setPieceLength(peerInteraction->getTorrentMan()->getPieceLength(((RequestMessage*)peerMessage)->getIndex()));      break;    case CancelMessage::ID:      peerMessage = CancelMessage::create(msg, msgLength);      ((CancelMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->pieces);      ((CancelMessage*)peerMessage)->setPieceLength(peerInteraction->getTorrentMan()->getPieceLength(((CancelMessage*)peerMessage)->getIndex()));      break;    case PieceMessage::ID:      peerMessage = PieceMessage::create(msg, msgLength);      ((PieceMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->pieces);      ((PieceMessage*)peerMessage)->setPieceLength(peerInteraction->getTorrentMan()->getPieceLength(((PieceMessage*)peerMessage)->getIndex()));      break;    case PortMessage::ID:      peerMessage = PortMessage::create(msg, msgLength);      break;    case HaveAllMessage::ID:      peerMessage = HaveAllMessage::create(msg, msgLength);      break;    case HaveNoneMessage::ID:      peerMessage = HaveNoneMessage::create(msg, msgLength);      break;    case RejectMessage::ID:      peerMessage = RejectMessage::create(msg, msgLength);      ((RejectMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->pieces);      ((RejectMessage*)peerMessage)->setPieceLength(peerInteraction->getTorrentMan()->getPieceLength(((RejectMessage*)peerMessage)->getIndex()));      break;    case SuggestPieceMessage::ID:      peerMessage = SuggestPieceMessage::create(msg, msgLength);      ((SuggestPieceMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->pieces);      break;    case AllowedFastMessage::ID:      peerMessage = AllowedFastMessage::create(msg, msgLength);      ((AllowedFastMessage*)peerMessage)->setPieces(peerInteraction->getTorrentMan()->pieces);      break;    default:      throw new DlAbortEx("Invalid message id. id = %d", id);    }  }  PeerMessageHandle handle = PeerMessageHandle(peerMessage);  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandlePeerMessageFactory::createHandshakeMessage(const char* msg, int msgLength) const{  PeerMessageHandle handle =    PeerMessageHandle(HandshakeMessage::create(msg, msgLength));    setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandlePeerMessageFactory::createHandshakeMessage(const unsigned char* infoHash,					  const char* peerId) const{  PeerMessageHandle handle =    PeerMessageHandle(new HandshakeMessage(infoHash, peerId));  setPeerMessageCommonProperty(handle);  return handle;}voidPeerMessageFactory::setPeerMessageCommonProperty(PeerMessageHandle& peerMessage) const{  peerMessage->setPeer(peer);  peerMessage->setCuid(cuid);  peerMessage->setPeerInteraction(peerInteraction);}PeerMessageHandle PeerMessageFactory::createRequestMessage(const Piece& piece,							   int blockIndex) const {  PeerMessageHandle handle =    PeerMessageHandle(new RequestMessage(piece.getIndex(),					 blockIndex*piece.getBlockLength(),					 piece.getBlockLength(blockIndex),					 blockIndex));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createCancelMessage(int index,							  int begin,							  int length) const {  PeerMessageHandle handle =    PeerMessageHandle(new CancelMessage(index, begin, length));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createPieceMessage(int index,							 int begin,							 int length) const {  PeerMessageHandle handle =    PeerMessageHandle(new PieceMessage(index, begin, length));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createHaveMessage(int index) const {  PeerMessageHandle handle =    PeerMessageHandle(new HaveMessage(index));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createChokeMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new ChokeMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createUnchokeMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new UnchokeMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createInterestedMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new InterestedMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createNotInterestedMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new NotInterestedMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createBitfieldMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new BitfieldMessage(peerInteraction->getTorrentMan()->					  getBitfield(),					  peerInteraction->getTorrentMan()->					  getBitfieldLength()));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createKeepAliveMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new KeepAliveMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createHaveAllMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new HaveAllMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createHaveNoneMessage() const {  PeerMessageHandle handle =    PeerMessageHandle(new HaveNoneMessage());  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createRejectMessage(int index,							  int begin,							  int length) const {  PeerMessageHandle handle =    PeerMessageHandle(new RejectMessage(index, begin, length));  setPeerMessageCommonProperty(handle);  return handle;}PeerMessageHandle PeerMessageFactory::createAllowedFastMessage(int index) const {  PeerMessageHandle handle =    PeerMessageHandle(new AllowedFastMessage(index));  setPeerMessageCommonProperty(handle);  return handle;}
 |