00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00039 _EXPORT
00040 RemoteRosterImpl::RemoteRosterImpl()
00041 : fInitStatus(B_NO_INIT),
00042 fRequestMessenger()
00043 {
00044 }
00045
00046
00047 _EXPORT
00048 RemoteRosterImpl::RemoteRosterImpl(BMessenger requestMessenger)
00049 : fInitStatus(B_NO_INIT),
00050 fRequestMessenger()
00051 {
00052 SetTo(requestMessenger);
00053 }
00054
00055
00056 _EXPORT
00057 RemoteRosterImpl::~RemoteRosterImpl()
00058 {
00059 Unset();
00060 }
00061
00062
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
00076 _EXPORT
00077 void
00078 RemoteRosterImpl::Unset()
00079 {
00080 fRequestMessenger = BMessenger();
00081 fInitStatus = B_NO_INIT;
00082 }
00083
00084
00085 _EXPORT
00086 status_t
00087 RemoteRosterImpl::InitCheck() const
00088 {
00089 return fInitStatus;
00090 }
00091
00092
00093 _EXPORT
00094 status_t
00095 RemoteRosterImpl::GetAppList(BList* teams) const
00096 {
00097 return GetAppList(NULL, teams);
00098 }
00099
00100
00101 _EXPORT
00102 status_t
00103 RemoteRosterImpl::GetAppList(const char* signature, BList* teams) const
00104 {
00105
00106 if (InitCheck() != B_OK)
00107 return InitCheck();
00108 if (!teams)
00109 return B_BAD_VALUE;
00110
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
00116 BMessage reply;
00117 if (error == B_OK)
00118 error = _SendRequest(&request, &reply);
00119
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
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
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
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
00158 _EXPORT
00159 status_t
00160 RemoteRosterImpl::GetRunningAppInfo(team_id team, app_info* info) const
00161 {
00162
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
00171 _EXPORT
00172 status_t
00173 RemoteRosterImpl::GetActiveAppInfo(app_info* info) const
00174 {
00175
00176 BMessage request(MSG_GET_APP_INFO);
00177 return _GetAppInfo(&request, info);
00178 }
00179
00180
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
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
00199 _EXPORT
00200 status_t
00201 RemoteRosterImpl::GetMessenger(team_id team, BMessenger* messenger) const
00202 {
00203
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
00212 status_t
00213 RemoteRosterImpl::_SendRequest(BMessage* request, BMessage* reply) const
00214 {
00215 status_t error = fRequestMessenger.SendMessage(request, reply);
00216
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
00226 status_t
00227 RemoteRosterImpl::_GetAppInfo(BMessage* request, app_info* info) const
00228 {
00229
00230 if (InitCheck() != B_OK)
00231 return InitCheck();
00232 if (!info)
00233 return B_BAD_VALUE;
00234
00235 BMessage reply;
00236 status_t error = _SendRequest(request, &reply);
00237
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
00259 status_t
00260 RemoteRosterImpl::_GetMessenger(BMessage* request, BMessenger* messenger) const
00261 {
00262
00263 if (InitCheck() != B_OK)
00264 return InitCheck();
00265 if (!messenger)
00266 return B_BAD_VALUE;
00267
00268 BMessage reply;
00269 status_t error = _SendRequest(request, &reply);
00270
00271 if (error == B_OK)
00272 error = reply.FindMessenger("messenger", messenger);
00273 return error;
00274 }
00275