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

RemoteRosterImpl.cpp

00001 // RemoteRosterImpl.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 "Debug.h"
00035 #include "RemoteRosterImpl.h"
00036 #include "RMessageServerDefs.h"
00037 
00038 // constructor
00039 _EXPORT
00040 RemoteRosterImpl::RemoteRosterImpl()
00041     : fInitStatus(B_NO_INIT),
00042       fRequestMessenger()
00043 {
00044 }
00045 
00046 // constructor
00047 _EXPORT
00048 RemoteRosterImpl::RemoteRosterImpl(BMessenger requestMessenger)
00049     : fInitStatus(B_NO_INIT),
00050       fRequestMessenger()
00051 {
00052     SetTo(requestMessenger);
00053 }
00054 
00055 // destructor
00056 _EXPORT
00057 RemoteRosterImpl::~RemoteRosterImpl()
00058 {
00059     Unset();
00060 }
00061 
00062 // SetTo
00063 _EXPORT
00064 status_t
00065 RemoteRosterImpl::SetTo(BMessenger requestMessenger)
00066 {
00067     fRequestMessenger = requestMessenger;
00068     if (fRequestMessenger.IsValid())
00069         fInitStatus = B_OK;
00070     else
00071         fInitStatus = B_NO_INIT;
00072     return fInitStatus;
00073 }
00074 
00075 // Unset
00076 _EXPORT
00077 void
00078 RemoteRosterImpl::Unset()
00079 {
00080     fRequestMessenger = BMessenger();
00081     fInitStatus = B_NO_INIT;
00082 }
00083 
00084 // InitCheck
00085 _EXPORT
00086 status_t
00087 RemoteRosterImpl::InitCheck() const
00088 {
00089     return fInitStatus;
00090 }
00091 
00092 // GetAppList
00093 _EXPORT
00094 status_t
00095 RemoteRosterImpl::GetAppList(BList* teams) const
00096 {
00097     return GetAppList(NULL, teams);
00098 }
00099 
00100 // GetAppList
00101 _EXPORT
00102 status_t
00103 RemoteRosterImpl::GetAppList(const char* signature, BList* teams) const
00104 {
00105     // check initialization and parameters
00106     if (InitCheck() != B_OK)
00107         return InitCheck();
00108     if (!teams)
00109         return B_BAD_VALUE;
00110     // prepare request message
00111     status_t error = B_OK;
00112     BMessage request(MSG_GET_APP_LIST);
00113     if (error == B_OK && signature)
00114         error = request.AddString("signature", signature);
00115     // send request
00116     BMessage reply;
00117     if (error == B_OK)
00118         error = _SendRequest(&request, &reply);
00119     // get the teams
00120     if (error == B_OK) {
00121         team_id team;
00122         for (int32 i = 0;
00123              error == B_OK && reply.FindInt32("teams", i, &team) == B_OK;
00124              i++) {
00125             if (!teams->AddItem((void*)team))
00126                 error = B_NO_MEMORY;
00127         }
00128     }
00129     return error;
00130 }
00131 
00132 // TeamFor
00133 _EXPORT
00134 team_id
00135 RemoteRosterImpl::TeamFor(const char* signature) const
00136 {
00137     app_info info;
00138     status_t error = GetRunningAppInfo(signature, &info);
00139     return (error == B_OK ? info.team : error);
00140 }
00141 
00142 // GetRunningAppInfo
00143 _EXPORT
00144 status_t
00145 RemoteRosterImpl::GetRunningAppInfo(const char* signature, app_info* info) const
00146 {
00147     if (!signature)
00148         return B_BAD_VALUE;
00149     // prepare the request and delegate the real work to the worker method
00150     BMessage request(MSG_GET_APP_INFO);
00151     status_t error = request.AddString("signature", signature);
00152     if (error  == B_OK)
00153         error = _GetAppInfo(&request, info);
00154     return error;
00155 }
00156 
00157 // GetRunningAppInfo
00158 _EXPORT
00159 status_t
00160 RemoteRosterImpl::GetRunningAppInfo(team_id team, app_info* info) const
00161 {
00162     // prepare the request and delegate the real work to the worker method
00163     BMessage request(MSG_GET_APP_INFO);
00164     status_t error = request.AddInt32("team", team);
00165     if (error  == B_OK)
00166         error = _GetAppInfo(&request, info);
00167     return error;
00168 }
00169 
00170 // GetActiveAppInfo
00171 _EXPORT
00172 status_t
00173 RemoteRosterImpl::GetActiveAppInfo(app_info* info) const
00174 {
00175     // prepare the request and delegate the real work to the worker method
00176     BMessage request(MSG_GET_APP_INFO);
00177     return _GetAppInfo(&request, info);
00178 }
00179 
00180 // GetMessenger
00181 _EXPORT
00182 status_t
00183 RemoteRosterImpl::GetMessenger(const char* signature, BMessenger* messenger,
00184     bool byName) const
00185 {
00186     if (!signature)
00187         return B_BAD_VALUE;
00188     // prepare the request and delegate the real work to the worker method
00189     BMessage request(MSG_GET_MESSENGER);
00190     status_t error = request.AddString("signature", signature);
00191     if (error == B_OK && byName)
00192         error = request.AddBool("or name", byName);
00193     if (error  == B_OK)
00194         error = _GetMessenger(&request, messenger);
00195     return error;
00196 }
00197 
00198 // GetMessenger
00199 _EXPORT
00200 status_t
00201 RemoteRosterImpl::GetMessenger(team_id team, BMessenger* messenger) const
00202 {
00203     // prepare the request and delegate the real work to the worker method
00204     BMessage request(MSG_GET_MESSENGER);
00205     status_t error = request.AddInt32("team", team);
00206     if (error  == B_OK)
00207         error = _GetMessenger(&request, messenger);
00208     return error;
00209 }
00210 
00211 // _SendRequest
00212 status_t
00213 RemoteRosterImpl::_SendRequest(BMessage* request, BMessage* reply) const
00214 {
00215     status_t error = fRequestMessenger.SendMessage(request, reply);
00216     // check request result
00217     if (error == B_OK && reply->what != MSG_REPLY)
00218         error = B_ERROR;
00219     status_t requestError;
00220     if (error == B_OK && reply->FindInt32("error", &requestError) == B_OK)
00221         error = requestError;
00222     return error;
00223 }
00224 
00225 // _GetAppInfo
00226 status_t
00227 RemoteRosterImpl::_GetAppInfo(BMessage* request, app_info* info) const
00228 {
00229     // check initialization and parameters
00230     if (InitCheck() != B_OK)
00231         return InitCheck();
00232     if (!info)
00233         return B_BAD_VALUE;
00234     // send request
00235     BMessage reply;
00236     status_t error = _SendRequest(request, &reply);
00237     // get the info
00238     if (error == B_OK)
00239          error = reply.FindInt32("thread", &info->thread);
00240     if (error == B_OK)
00241          error = reply.FindInt32("team", &info->team);
00242     if (error == B_OK)
00243          error = reply.FindInt32("port", &info->port);
00244     if (error == B_OK)
00245          error = reply.FindInt32("flags", (int32*)&info->flags);
00246     if (error == B_OK)
00247          error = reply.FindRef("ref", &info->ref);
00248     const char* signature;
00249     if (error == B_OK)
00250          error = reply.FindString("signature", &signature);
00251     if (error == B_OK) {
00252         strncpy(info->signature, signature, B_MIME_TYPE_LENGTH);
00253         info->signature[B_MIME_TYPE_LENGTH - 1] = '\0';
00254     }
00255     return error;
00256 }
00257 
00258 // _GetMessenger
00259 status_t
00260 RemoteRosterImpl::_GetMessenger(BMessage* request, BMessenger* messenger) const
00261 {
00262     // check initialization and parameters
00263     if (InitCheck() != B_OK)
00264         return InitCheck();
00265     if (!messenger)
00266         return B_BAD_VALUE;
00267     // send request
00268     BMessage reply;
00269     status_t error = _SendRequest(request, &reply);
00270     // get the messenger
00271     if (error == B_OK)
00272          error = reply.FindMessenger("messenger", messenger);
00273     return error;
00274 }
00275 

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