libktorrent  2.2.0
rpcmsg.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef DHTRPCMSG_H
21 #define DHTRPCMSG_H
22 
23 #include <QString>
24 #include <QSharedPointer>
25 #include <ktorrent_export.h>
26 #include <util/constants.h>
27 #include "key.h"
28 #include "database.h"
29 
30 namespace bt
31 {
32  class BDictNode;
33 }
34 
35 namespace dht
36 {
37 
38 #define MAX_TOKEN_SIZE 40
39 
40  class DHT;
41 
42  enum Type
43  {
44  REQ_MSG,
45  RSP_MSG,
46  ERR_MSG,
47  INVALID
48  };
49 
50  enum Method
51  {
52  PING,
53  FIND_NODE,
54  GET_PEERS,
55  ANNOUNCE_PEER,
56  NONE
57  };
58 
59  const QByteArray TID = QByteArrayLiteral("t");
60  const QByteArray REQ = "q";
61  const QByteArray RSP = "r";
62  const QByteArray TYP = "y";
63  const QByteArray ARG = "a";
64  const QByteArray ERR_DHT = "e";
65 
69  class KTORRENT_EXPORT RPCMsg
70  {
71  public:
72  RPCMsg();
73  RPCMsg(const QByteArray & mtid, Method m, Type type, const Key & id);
74  virtual ~RPCMsg();
75 
76  typedef QSharedPointer<RPCMsg> Ptr;
77 
83  virtual void apply(DHT* dh_table) = 0;
84 
88  virtual void print() = 0;
89 
94  virtual void encode(QByteArray & arr) const = 0;
95 
101  virtual void parse(bt::BDictNode* dict);
102 
104  void setOrigin(const net::Address & o) {origin = o;}
105 
107  const net::Address & getOrigin() const {return origin;}
108 
110  void setDestination(const net::Address & o) {origin = o;}
111 
113  const net::Address & getDestination() const {return origin;}
114 
116  const QByteArray & getMTID() const {return mtid;}
117 
119  void setMTID(const QByteArray & m) {mtid = m;}
120 
122  const Key & getID() const {return id;}
123 
125  Type getType() const {return type;}
126 
128  Method getMethod() const {return method;}
129 
130  protected:
131  QByteArray mtid;
132  Method method;
133  Type type;
134  Key id;
135  net::Address origin;
136  };
137 
138 
139 }
140 
141 #endif
dht::RPCMsg::getType
Type getType() const
Get the type of the message.
Definition: rpcmsg.h:125
net::Address
Definition: address.h:41
dht::RPCMsg::getMTID
const QByteArray & getMTID() const
Get the MTID.
Definition: rpcmsg.h:116
dht::RPCMsg::getMethod
Method getMethod() const
Get the message it's method.
Definition: rpcmsg.h:128
dht::RPCMsg::getDestination
const net::Address & getDestination() const
Get the origin.
Definition: rpcmsg.h:113
dht::Key
Key in the distributed hash table.
Definition: key.h:39
dht::RPCMsg
Definition: rpcmsg.h:70
dht::RPCMsg::getOrigin
const net::Address & getOrigin() const
Get the origin.
Definition: rpcmsg.h:107
dht::RPCMsg::setMTID
void setMTID(const QByteArray &m)
Set the MTID.
Definition: rpcmsg.h:119
dht::RPCMsg::encode
virtual void encode(QByteArray &arr) const =0
dht::RPCMsg::setOrigin
void setOrigin(const net::Address &o)
Set the origin (i.e. where the message came from)
Definition: rpcmsg.h:104
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:101
dht::RPCMsg::getID
const Key & getID() const
Get the id of the sender.
Definition: rpcmsg.h:122
dht::RPCMsg::setDestination
void setDestination(const net::Address &o)
Set the origin (i.e. where the message came from)
Definition: rpcmsg.h:110
dht::RPCMsg::print
virtual void print()=0
dht::RPCMsg::parse
virtual void parse(bt::BDictNode *dict)
dht::RPCMsg::apply
virtual void apply(DHT *dh_table)=0
dht::DHT
Definition: dht.h:63