Mark destructors in module classes as virtual.
[tairent.git] / src / azdht / azdhtmodule.h
blob7b2eb0dbc870bee4865dc41c1d3913b2071a8e25
1 /***************************************************************************
2 * *
3 * Copyright (C) 2007 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
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 GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _azdht_azdhtmodule_h
18 #define _azdht_azdhtmodule_h
20 #include <map>
22 #include <tairon/core/module.h>
23 #include <tairon/core/signals.h>
25 namespace Tairon
28 namespace Net
31 class Socket;
32 class Timer;
34 }; // namespace Net
36 }; // namespace Tairon
38 namespace Tairent
41 namespace Core
44 class BEncode;
46 }; // namespace Core
48 namespace AzDHT
51 /** \brief Module for communicating with AzDHT program.
53 class AzDHTModule : public Tairon::Core::Module
55 public:
56 enum Flags {
57 SINGLE_VALUE = 0,
58 DOWNLOADING = 1,
59 SEEDING = 2,
60 MULTI_VALUE = 4,
61 STATS = 8
64 /** Constructs an AzDHTModule object.
66 AzDHTModule();
68 /** Destroys the object.
70 virtual ~AzDHTModule();
72 /** Queries the DHT for value.
74 void get(const String &key, char flags);
76 /** Returns true if we are connected to the DHT; otherwise returns
77 * false.
79 bool isConnected();
81 /** Puts new value in the DHT.
83 void put(const String &key, const String &value, char flags);
85 /** Registers new handler for incoming values. If there is already
86 * associated handler for the given key then the old handler is
87 * deleted. This module takes ownership of the handler and all
88 * registered handlers are deleted when this module is unloaded.
90 * \param key Request's key.
91 * \param handler Functor that will be called when values arrive.
93 void registerHandler(const String &key, Tairon::Core::Functor2<void, const String &, const Tairent::Core::BEncode &> *handler);
95 /** Removes value for given key from the DHT.
97 void remove(const String &key);
99 /** Returns pointer to the instance of this class.
101 static AzDHTModule *self() {
102 return azdhtmodule;
105 private:
106 /** Called when we are connected to the AzDHT server.
108 void connected(Tairon::Net::Socket *);
110 /** Closes the socket and tries to reconnect in 1 minute.
112 void connectionError();
114 /** Ensures that the internal buffer is large enough.
116 * \param size Desired size of the buffer.
118 void enlargeBuffer(uint32_t size);
120 /** Processes message incoming from the server.
122 * \param b Message sent by the server.
124 void processMessage(const Tairent::Core::BEncode &b);
126 /** Reads body of the message.
128 void readMessage();
130 /** Reads length of the message.
132 void readMessageLength();
134 /** Called when there is something to read from the socket.
136 void readyRead(Tairon::Net::Socket *);
138 /** Reconnects to the AzDHT program.
140 void reconnect();
142 /** Sends query to the server.
144 void sendQuery(const Tairent::Core::BEncode &query);
146 /** Called when an error occurs.
148 void socketError(Tairon::Net::Socket *, int);
150 private:
151 /** Address on which AzDHT is listening.
153 String address;
155 /** Holds pointer to the instance of this class.
157 static AzDHTModule *azdhtmodule;
159 /** Message buffer.
161 char *buffer;
163 /** Size of the message buffer.
165 uint32_t bufSize;
167 std::map<String, Tairon::Core::Functor2<void, const String &, const Tairent::Core::BEncode &> *> handlers;
169 /** Method that reads data from the socket.
171 void (AzDHTModule::*readMethod)();
173 /** Length of the message.
175 uint32_t msgLength;
177 /** Number of bytes read so far.
179 uint32_t offset;
181 /** Port on which AzDHT is listening.
183 uint16_t port;
185 /** Connection with the AzDHT program.
187 Tairon::Net::Socket *socket;
189 /** Timer for reconnecting to the AzDHT program.
191 Tairon::Net::Timer *timer;
194 }; // namespace AzDHT
196 }; // namespace Tairent
198 #endif
200 // vim: ai sw=4 ts=4 noet fdm=marker