Main Page   Namespace List   Compound List   File List   Namespace Members   Compound Members  

RemoteRoster.cpp

00001 // RemoteRoster.cpp
00002 // 
00003 // Copyright (c) 2004, Ingo Weinhold (bonefish@cs.tu-berlin.de)
00004 // 
00005 // Permission is hereby granted, free of charge, to any person obtaining a
00006 // copy of this software and associated documentation files (the "Software"),
00007 // to deal in the Software without restriction, including without limitation 
00008 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 // and/or sell copies of the Software, and to permit persons to whom the
00010 // Software is furnished to do so, subject to the following conditions:
00011 // 
00012 // The above copyright notice and this permission notice shall be included in
00013 // all copies or substantial portions of the Software.
00014 // 
00015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00018 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00021 // DEALINGS IN THE SOFTWARE.
00022 // 
00023 // Except as contained in this notice, the name of a copyright holder shall
00024 // not be used in advertising or otherwise to promote the sale, use or other
00025 // dealings in this Software without prior written authorization of the
00026 // copyright holder.
00027 
00028 #include <new>
00029 #include <string.h>
00030 
00031 #include <List.h>
00032 #include <Roster.h>
00033 
00034 #include <RemoteRoster.h>
00035 
00036 #include "Debug.h"
00037 #include "RemoteRosterImpl.h"
00038 #include "RMessageServerDefs.h"
00039 
00050 // constructor
00060 _EXPORT
00061 RemoteRoster::RemoteRoster(const BNetAddress* address)
00062     : fInitStatus(B_NO_INIT),
00063       fAddress(NULL),
00064       fImpl(NULL)
00065 {
00066     if (address)
00067         SetTo(address);
00068 }
00069 
00070 // constructor
00080 _EXPORT
00081 RemoteRoster::RemoteRoster(const char* host)
00082     : fInitStatus(B_NO_INIT),
00083       fAddress(NULL),
00084       fImpl(NULL)
00085 {
00086     if (host)
00087         SetTo(host);
00088 }
00089 
00090 // destructor
00093 _EXPORT
00094 RemoteRoster::~RemoteRoster()
00095 {
00096     Unset();
00097 }
00098 
00099 // SetTo
00108 _EXPORT
00109 status_t
00110 RemoteRoster::SetTo(const BNetAddress* address)
00111 {
00112 FUNCTION_START();
00113     Unset();
00114     if (!address)
00115         return (fInitStatus = B_BAD_VALUE);
00116     // clone address
00117     status_t error = B_OK;
00118     fAddress = new(nothrow) BNetAddress(*address);
00119     if (!fAddress)
00120         RETURN_ERROR(B_NO_MEMORY);
00121     // get server messenger
00122     BMessenger serverMessenger(kRMessageServerSignature);
00123     if (error == B_OK && !serverMessenger.IsValid())
00124         SET_ERROR(error, B_ERROR);
00125     // ask server to connect
00126     if (error == B_OK) {
00127         // prepare request message
00128         BMessage request(MSG_CONNECT);
00129         BMessage addressArchive;
00130         error = fAddress->Archive(&addressArchive);
00131         if (error == B_OK)
00132             SET_ERROR(error, request.AddMessage("address", &addressArchive));
00133         // send request
00134         BMessage reply;
00135         if (error == B_OK)
00136             SET_ERROR(error, serverMessenger.SendMessage(&request, &reply));
00137         // check request result
00138         if (error == B_OK && reply.what != MSG_REPLY)
00139             SET_ERROR(error, B_ERROR);
00140         status_t requestError;
00141         if (error == B_OK && reply.FindInt32("error", &requestError) == B_OK)
00142             SET_ERROR(error, requestError);
00143         // get request messenger
00144         BMessenger requestMessenger;
00145         if (error == B_OK) {
00146             SET_ERROR(error, reply.FindMessenger("request messenger",
00147                 &requestMessenger));
00148         }
00149         if (error == B_OK && !requestMessenger.IsValid())
00150             SET_ERROR(error, B_ERROR);
00151         // create the impl object
00152         if (error == B_OK) {
00153             fImpl = new(nothrow) RemoteRosterImpl(requestMessenger);
00154             if (fImpl) {
00155                 SET_ERROR(error, fImpl->InitCheck());
00156             } else {
00157                 SET_ERROR(error, B_NO_MEMORY);
00158             }
00159         }
00160     }
00161     // cleanup on error
00162     if (error != B_OK)
00163         Unset();
00164     RETURN_ERROR(fInitStatus = error);
00165 }
00166 
00167 // SetTo
00176 _EXPORT
00177 status_t
00178 RemoteRoster::SetTo(const char* host)
00179 {
00180     Unset();
00181     if (!host)
00182         return (fInitStatus = B_BAD_VALUE);
00183     BNetAddress address(host);
00184     if (address.InitCheck() != B_OK)
00185         RETURN_ERROR(fInitStatus = address.InitCheck());
00186     return SetTo(&address);
00187 }
00188 
00189 // Unset
00192 _EXPORT
00193 void
00194 RemoteRoster::Unset()
00195 {
00196     delete fAddress;
00197     fAddress = NULL;
00198     delete fImpl;
00199     fImpl = NULL;
00200     fInitStatus = B_NO_INIT;
00201 }
00202 
00203 // InitCheck
00211 _EXPORT
00212 status_t
00213 RemoteRoster::InitCheck() const
00214 {
00215     return fInitStatus;
00216 }
00217 
00218 // GetAppList
00231 _EXPORT
00232 status_t
00233 RemoteRoster::GetAppList(BList* teams) const
00234 {
00235     return (InitCheck() == B_OK ? fImpl->GetAppList(teams) : InitCheck());
00236 }
00237 
00238 // GetAppList
00255 _EXPORT
00256 status_t
00257 RemoteRoster::GetAppList(const char* signature, BList* teams) const
00258 {
00259     return (InitCheck() == B_OK ? fImpl->GetAppList(signature, teams)
00260                                 : InitCheck());
00261 }
00262 
00263 // TeamFor
00280 _EXPORT
00281 team_id
00282 RemoteRoster::TeamFor(const char* signature) const
00283 {
00284     return (InitCheck() == B_OK ? fImpl->TeamFor(signature) : InitCheck());
00285 }
00286 
00287 // GetRunningAppInfo
00306 _EXPORT
00307 status_t
00308 RemoteRoster::GetRunningAppInfo(const char* signature, app_info* info) const
00309 {
00310     return (InitCheck() == B_OK ? fImpl->GetRunningAppInfo(signature, info)
00311                                 : InitCheck());
00312 }
00313 
00314 // GetRunningAppInfo
00329 _EXPORT
00330 status_t
00331 RemoteRoster::GetRunningAppInfo(team_id team, app_info* info) const
00332 {
00333     return (InitCheck() == B_OK ? fImpl->GetRunningAppInfo(team, info)
00334                                 : InitCheck());
00335 }
00336 
00337 // GetActiveAppInfo
00349 _EXPORT
00350 status_t
00351 RemoteRoster::GetActiveAppInfo(app_info* info) const
00352 {
00353     return (InitCheck() == B_OK ? fImpl->GetActiveAppInfo(info) : InitCheck());
00354 }
00355 
00356 // GetMessenger
00373 _EXPORT
00374 status_t
00375 RemoteRoster::GetMessenger(const char* signature, BMessenger* messenger,
00376     bool byName) const
00377 {
00378     return (InitCheck() == B_OK
00379         ? fImpl->GetMessenger(signature, messenger, byName) : InitCheck());
00380 }
00381 
00382 // GetMessenger
00395 _EXPORT
00396 status_t
00397 RemoteRoster::GetMessenger(team_id team, BMessenger* messenger) const
00398 {
00399     return (InitCheck() == B_OK ? fImpl->GetMessenger(team, messenger)
00400                                 : InitCheck());
00401 }

Generated at Mon Mar 22 02:35:22 2004 for RMessage by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000