1 /***************************************************************************
3 * Copyright (C) 2006 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 #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"
33 TrackerManager
*TrackerManager::trackerManager
= 0;
35 /* {{{ TrackerManager::TrackerManager() */
36 TrackerManager::TrackerManager()
39 throw Tairon::Core::SingletonException("Cannot make another instance of Tairent::Main::TrackerManager");
41 trackerManager
= this;
45 /* {{{ TrackerManager::~TrackerManager() */
46 TrackerManager::~TrackerManager()
52 /* {{{ TrackerManager::createDefaultTracker(TorrentClient *) */
53 TrackerClient
*TrackerManager::createDefaultTracker(TorrentClient
*client
)
59 /* {{{ TrackerManager::getTracker(TorrentClient *) */
60 TrackerClient
*TrackerManager::getTracker(TorrentClient
*client
)
62 const Tairent::Core::BEncode
&metaInfo
= client
->getMetaInfo();
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
);
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
));
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
));
100 }; // namespace Tairent
102 // vim: ai sw=4 ts=4 noet fdm=marker