IpResolvingTransportFactory.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "IpResolvingTransportFactory.h"
00025 #include "NameResolver.h"
00026 #include "TransportFactoryStatus.h"
00027 #include "Imports.cpp"
00028 
00029 IpResolvingTransportFactory::IpResolvingTransportFactory (
00030                                                 TransportFactory *otherFactory,
00031                                                 NameResolver *nameResolver)
00032  :  otherFactory_ (otherFactory),
00033     nameResolver_ (nameResolver),
00034     resolvingStates_(),
00035     creatingStates_()
00036 {
00037     Q_ASSERT (otherFactory_ != 0);
00038 }
00039 
00040 IpResolvingTransportFactory::~IpResolvingTransportFactory()
00041 {
00042     // \todo Clean up resolvingStates_ and creatingStates_?
00043 }
00044 
00045 void
00046 IpResolvingTransportFactory::createTransport (const Uri &uri,
00047                                               TransportFactoryStatus *status)
00048 {
00049     QHostAddress hostAddress (QString (uri.host()));
00050     if (hostAddress.isNull())
00051         startNameResolution (uri, status);
00052     else
00053         otherFactory_->createTransport (uri, status);
00054 }
00055 
00056 void IpResolvingTransportFactory::destroyTransport (Transport *transport)
00057 {
00058     otherFactory_->destroyTransport (transport);
00059 }
00060 
00061 void IpResolvingTransportFactory::transportFactorySucceeded (
00062                                                     const Uri &uri,
00063                                                     Transport *transport)
00064 {
00065     Q_ASSERT (creatingStates_.contains (uri));
00066     auto_ptr <ResolvingState> state (creatingStates_.take (uri));
00067     state->status->transportFactorySucceeded (state->uri, transport);
00068 }
00069 
00070 void IpResolvingTransportFactory::transportFactoryFailed (const Uri &uri)
00071 {
00072     Q_ASSERT (creatingStates_.contains (uri));
00073     // \todo tryCreateTransport() modify it to read creatingStates_
00074     auto_ptr <ResolvingState> state (creatingStates_.take (uri));
00075     state->status->transportFactoryFailed (state->uri);
00076 }
00077 
00078 void IpResolvingTransportFactory::nameResolverResolvedName (
00079                                         const QByteArray &host,
00080                                         const QList <QHostAddress> &addresses)
00081 {
00082     Q_ASSERT (resolvingStates_.contains (host));
00083     resolvingStates_.value (host)->addresses = addresses;
00084     tryCreateTransport (host);
00085 }
00086 
00087 void IpResolvingTransportFactory::startNameResolution (
00088                                             const Uri &uri,
00089                                             TransportFactoryStatus *status)
00090 {
00091     QByteArray host = uri.host();
00092     auto_ptr <ResolvingState> state (new ResolvingState);
00093     state->uri = uri;
00094     state->status = status;
00095     resolvingStates_.insert (host, state.get());
00096     state.release();
00097     nameResolver_->resolveName (host, this);
00098 }
00099 
00100 void IpResolvingTransportFactory::createFailed (const QByteArray &host)
00101 {
00102     auto_ptr <ResolvingState> state;
00103     state.reset (resolvingStates_.take (host));
00104     state->status->transportFactoryFailed (state->uri);
00105 }
00106 
00107 void IpResolvingTransportFactory::tryCreateTransport (const QByteArray &host)
00108 {
00109     QList <QHostAddress> addresses = resolvingStates_.value (host)->addresses;
00110     if (addresses.isEmpty())
00111         createFailed (host);
00112     else {
00113         ResolvingState * state = resolvingStates_.value (host);
00114         Uri uri = state->uri;
00115         uri.setAuthority (uri.userInfo(),
00116                           addresses.takeFirst().toString().toLatin1(),
00117                           uri.port());
00118         creatingStates_.insert (uri, state);
00119         resolvingStates_.take (host);
00120         otherFactory_->createTransport (uri, this);
00121     }
00122 }