Send correct informations to a http tracker.
[tairent.git] / src / main / trackermanager.cpp
blob1aecbf96cce7b667a1f66c2c8f8a35256bd1b14c
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 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 #include <tairon/core/exceptions.h>
18 #include <tairon/core/log.h>
19 #include <tairon/util/regexp.h>
21 #include "trackermanager.h"
23 #include "core/bencode.h"
24 #include "torrentclient.h"
25 #include "trackerclientfactory.h"
27 namespace Tairent
30 namespace Main
33 TrackerManager *TrackerManager::trackerManager = 0;
35 /* {{{ TrackerManager::TrackerManager() */
36 TrackerManager::TrackerManager()
38 if (trackerManager)
39 throw Tairon::Core::SingletonException("Cannot make another instance of Tairent::Main::TrackerManager");
41 trackerManager = this;
43 /* }}} */
45 /* {{{ TrackerManager::~TrackerManager() */
46 TrackerManager::~TrackerManager()
48 trackerManager = 0;
50 /* }}} */
52 /* {{{ TrackerManager::createDefaultTracker(TorrentClient *) */
53 TrackerClient *TrackerManager::createDefaultTracker(TorrentClient *client)
55 return 0;
57 /* }}} */
59 /* {{{ TrackerManager::getTracker(TorrentClient *) */
60 TrackerClient *TrackerManager::getTracker(TorrentClient *client)
62 const Tairent::Core::BEncode &metaInfo = client->getMetaInfo();
63 try {
64 Tairon::Util::RegExp r("([[:alpha:]]*)://([[:alnum:]\\.-]+)(:([0-9]+))?(.*)");
65 if (r.match(metaInfo["announce"].asString()) == -1)
66 return createDefaultTracker(client);
68 String protocol = r.matched(1);
69 if (!protocols.count(protocol))
70 return createDefaultTracker(client);
72 return protocols[protocol]->createTrackerClient(client);
73 } catch (const Tairent::Core::BEncodeException &) {
74 // there is no announce item in metainfo
75 return createDefaultTracker(client);
78 return 0;
80 /* }}} */
82 /* {{{ TrackerManager::registerFactory(const String &, TrackerClientFactory *) */
83 void TrackerManager::registerFactory(const String &protocol, TrackerClientFactory *factory)
85 protocols[protocol] = factory;
86 INFO((const char *) String("Registered factory for protocol " + protocol));
88 /* }}} */
90 /* {{{ TrackerManager::unregisterFactory(const String &) */
91 void TrackerManager::unregisterFactory(const String &protocol)
93 protocols.erase(protocol);
94 INFO((const char *) String("Unregistered factory for protocol " + protocol));
96 /* }}} */
98 }; // namespace Main
100 }; // namespace Tairent
102 // vim: ai sw=4 ts=4 noet fdm=marker