libktorrent  2.1.1
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  class DHT;
38 
39  enum Type
40  {
41  REQ_MSG,
42  RSP_MSG,
43  ERR_MSG,
44  INVALID
45  };
46 
47  enum Method
48  {
49  PING,
50  FIND_NODE,
51  GET_PEERS,
52  ANNOUNCE_PEER,
53  NONE
54  };
55 
56  const QByteArray TID = QByteArrayLiteral("t");
57  const QByteArray REQ = "q";
58  const QByteArray RSP = "r";
59  const QByteArray TYP = "y";
60  const QByteArray ARG = "a";
61  const QByteArray ERR_DHT = "e";
62 
66  class KTORRENT_EXPORT RPCMsg
67  {
68  public:
69  RPCMsg();
70  RPCMsg(const QByteArray & mtid, Method m, Type type, const Key & id);
71  virtual ~RPCMsg();
72 
73  typedef QSharedPointer<RPCMsg> Ptr;
74 
80  virtual void apply(DHT* dh_table) = 0;
81 
85  virtual void print() = 0;
86 
91  virtual void encode(QByteArray & arr) const = 0;
92 
98  virtual void parse(bt::BDictNode* dict);
99 
101  void setOrigin(const net::Address & o) {origin = o;}
102 
104  const net::Address & getOrigin() const {return origin;}
105 
107  void setDestination(const net::Address & o) {origin = o;}
108 
110  const net::Address & getDestination() const {return origin;}
111 
113  const QByteArray & getMTID() const {return mtid;}
114 
116  void setMTID(const QByteArray & m) {mtid = m;}
117 
119  const Key & getID() const {return id;}
120 
122  Type getType() const {return type;}
123 
125  Method getMethod() const {return method;}
126 
127  protected:
128  QByteArray mtid;
129  Method method;
130  Type type;
131  Key id;
132  net::Address origin;
133  };
134 
135 
136 }
137 
138 #endif
dht::RPCMsg::getType
Type getType() const
Get the type of the message.
Definition: rpcmsg.h:122
net::Address
Definition: address.h:59
dht::RPCMsg::getMTID
const QByteArray & getMTID() const
Get the MTID.
Definition: rpcmsg.h:113
dht::RPCMsg::getMethod
Method getMethod() const
Get the message it's method.
Definition: rpcmsg.h:125
dht::RPCMsg::getDestination
const net::Address & getDestination() const
Get the origin.
Definition: rpcmsg.h:110
dht::Key
Key in the distributed hash table.
Definition: key.h:57
dht::RPCMsg
Definition: rpcmsg.h:67
dht::RPCMsg::getOrigin
const net::Address & getOrigin() const
Get the origin.
Definition: rpcmsg.h:104
dht::RPCMsg::setMTID
void setMTID(const QByteArray &m)
Set the MTID.
Definition: rpcmsg.h:116
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:101
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:120
dht::RPCMsg::getID
const Key & getID() const
Get the id of the sender.
Definition: rpcmsg.h:119
dht::RPCMsg::setDestination
void setDestination(const net::Address &o)
Set the origin (i.e. where the message came from)
Definition: rpcmsg.h:107
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