1 /***************************************************************************
3 * Copyright (C) 2007 David Brodsky *
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. *
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. *
15 ***************************************************************************/
17 #ifndef _azdht_azdhtmodule_h
18 #define _azdht_azdhtmodule_h
22 #include <tairon/core/module.h>
23 #include <tairon/core/signals.h>
36 }; // namespace Tairon
51 /** \brief Module for communicating with AzDHT program.
53 class AzDHTModule
: public Tairon::Core::Module
64 /** Constructs an AzDHTModule object.
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
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() {
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.
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.
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);
151 /** Address on which AzDHT is listening.
155 /** Holds pointer to the instance of this class.
157 static AzDHTModule
*azdhtmodule
;
163 /** Size of the message buffer.
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.
177 /** Number of bytes read so far.
181 /** Port on which AzDHT is listening.
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
200 // vim: ai sw=4 ts=4 noet fdm=marker