1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/** Implementation of the DKPort class for NSConnection integration.
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Created: May 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import "DBusKit/DKPort.h"
#import "DBusKit/DKNotificationCenter.h"
#import "DKProxy+Private.h"
#import "DKEndpoint.h"
#import "DKEndpointManager.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSPort.h>
#import <Foundation/NSPortCoder.h>
#import <Foundation/NSPortMessage.h>
#import <Foundation/NSRunLoop.h>
#include <dbus/dbus.h>
/*
* Enumeration of GNUstep DO message IDs, will need to be kept in sync with
* GNUstepBase/DistributedObjects.h.
*/
enum {
METHOD_REQUEST = 0,
METHOD_REPLY,
ROOTPROXY_REQUEST,
ROOTPROXY_REPLY,
CONNECTION_SHUTDOWN,
METHODTYPE_REQUEST,
METHODTYPE_REPLY,
PROXY_RELEASE,
PROXY_RETAIN,
RETAIN_REPLY,
// Custom types needed by D-Bus
PROXY_AT_PATH_REQUEST = 254,
PROXY_AT_PATH_REPLY = 255
};
@protocol DBus
- (NSArray*)ListNames;
- (NSArray*)ListActivatableNames;
@end
/*
* We need to access the private -[NSPortCoder _components] method.
*/
@interface NSPortCoder (UnhideComponents)
- (NSArray*)_components;
@end
@interface DKPort (DKPortPrivate)
/**
* Performs checks to ensure that the corresponding D-Bus service and object
* path exist and sends a message to the delegate NSConnection object containing
* an encoded DKProxy.
*/
- (BOOL) _returnProxyForPath: (NSString*)path
utilizingComponents: (NSArray*)components
fromPort: (NSPort*)receivePort;
/**
* Called by the notification center when the remote is removed from the bus.
*/
- (void)_remoteDisappeared: (NSNotification*)notification;
/**
* Called by the notification center when the port was disconnected from the
* bus.
*/
- (void)_disconnected: (NSNotification*)notification;
- (id)initForBusType: (DKDBusBusType)type;
@end
@implementation DKPort
+ (NSPort*)port
{
return [self sessionBusPort];
}
+ (id)portForBusType: (DKDBusBusType)type
{
return [[[self alloc] initForBusType: type] autorelease];
}
+ (id)sessionBusPort
{
return [self portForBusType: DKDBusSessionBus];
}
+ (id)systemBusPort
{
return [self portForBusType: DKDBusSystemBus];
}
+ (void)enableWorkerThread
{
[[DKEndpointManager sharedEndpointManager] enableThread];
}
- (void)_registerNotifications
{
DKDBusBusType busType = [endpoint DBusBusType];
DKNotificationCenter *center = [DKNotificationCenter centerForBusType: busType];
/*
* If the port is non-local (i.e. has a specified name), we set up the
* notification center to inform us when the remote disappears. The whole
* process would be pointless for ports to the org.freedesktop.DBus service,
* so we avoid observing its name.
*/
if ((0 != [remote length])
&& (NO == [@"org.freedesktop.DBus" isEqualToString: remote]))
{
/*
* Setup observation rule: arg0 carries the name, arg2 the new owner, which
* is empty if the name disappeared.
*/
[center addObserver: self
selector: @selector(_remoteDisappeared:)
signal: @"NameOwnerChanged"
interface: @"org.freedesktop.DBus"
sender: [DKDBus busWithBusType: busType]
destination: nil
filtersAndIndices: remote, 0, @"", 2, nil];
}
/*
* For all ports, we want to watch for the Disconnected signal on the
* o.fd.DBus.Local interface. This is a pseudo-signal that will be generated
* in-process when libdbus looses the connection to the dbus-daemon.
*/
[center addObserver: self
selector: @selector(_disconnected:)
signal: @"Disconnected"
interface: [NSString stringWithUTF8String: DBUS_INTERFACE_LOCAL]
sender: nil
destination: nil];
}
- (id) initWithRemote: (NSString*)aRemote
atEndpoint: (DKEndpoint*)anEndpoint
{
if (nil == (self = [super init]))
{
return nil;
}
if (nil == anEndpoint)
{
// Default to an endpoint to the session bus if none is given.
anEndpoint = [[DKEndpointManager sharedEndpointManager] endpointForWellKnownBus: DBUS_BUS_SESSION];
}
ASSIGN(endpoint, anEndpoint);
ASSIGNCOPY(remote, aRemote);
[self _registerNotifications];
return self;
}
- (id) initWithRemote: (NSString*)aRemote
{
return [self initWithRemote: aRemote
atEndpoint: nil];
}
- (id) initWithRemote: (NSString*)aRemote
onBus: (DKDBusBusType)type
{
DKEndpoint *ep = [[DKEndpointManager sharedEndpointManager] endpointForWellKnownBus: type];
return [self initWithRemote: aRemote
atEndpoint: ep];
}
- (id) initForBusType: (DKDBusBusType)type
{
return [self initWithRemote: nil
onBus: type];
}
- (id) init
{
return [self initWithRemote: nil];
}
/**
* Determines whether the service that the ports connects to is valid.
*/
- (BOOL) hasValidRemoteOnBus: (id<DBus>)bus
{
if ([remote isEqualToString: @"org.freedesktop.DBus"])
{
// It is save to assume that the bus object is available.
return YES;
}
if ([[bus ListNames] containsObject: remote])
{
return YES;
}
if ([[bus ListActivatableNames] containsObject: remote])
{
return YES;
}
else
{
NSWarnMLog(@"D-Bus service %@ is neither available nor activatable",
remote);
return NO;
}
}
- (BOOL) hasValidRemote
{
return [self hasValidRemoteOnBus: (id<DBus>)[DKDBus busWithBusType: [endpoint DBusBusType]]];
}
/**
* Returns the present endpoint.
*/
- (DKEndpoint*)endpoint
{
return endpoint;
}
/**
* Returns the name of the remote.
*/
- (NSString*)serviceName
{
return remote;
}
/**
* Two ports are considered equal if they connect to the same service behind the
* same endpoint.
*/
- (BOOL)isEqual: (DKPort*)other
{
if (self == other)
{
return YES;
}
/*
* Check whether the class of other is also DKPort. Only then we stand any
* chance of being equal to it. Otherwise, we might crash because most NSPort
* subclasses won't respond to -endpoint.
*/
if (NO == [other isKindOfClass: [DKPort class]])
{
return NO;
}
return ([endpoint isEqual: [other endpoint]]
&& [remote isEqual: [other serviceName]]);
}
- (NSUInteger)hash
{
// Bitwise XOR the hashes of the two values we are using for equality tests:
return ([endpoint hash] ^ [remote hash]);
}
/**
* This is the main method used to dispatch stuff from the DO system to D-Bus.
* Primarily we want to respond to ROOTPROXY_REQUEST, because everyting else
* will be handled from DKProxy.
*/
- (BOOL)sendBeforeDate: (NSDate *)limitDate
msgid: (NSInteger)msgid
components: (NSMutableArray *)components
from: (NSPort *)recievePort
reserved: (NSUInteger)reserverdHeaderSpace
{
/*
* NOTE: I'm not sure whether every detail of D-Bus IPC should be processed
* here. It might be easier to have the proxy take care of things like message
* dispatch, etc.
*/
switch (msgid)
{
case ROOTPROXY_REQUEST:
/* TODO:
* 1. Check whether the remote side exists by sending a ping
* 2. Schedule generation of reply for NSConnection to consume
*/
NSDebugMLog(@"Got rootproxy request for remote %@", remote);
return [self _returnProxyForPath: @"/"
utilizingComponents: components
fromPort: recievePort];
case METHODTYPE_REQUEST:
/* TODO:
* 1. Check whether the remote side exists
* 2. Decode D-Bus interface from the components
* 3. Send D-Bus request for introspection data (possibly trigger
* generation of the cache)
* 4. Schedule generation of reply for NSConnection to consume.
*/
NSDebugMLog(@"Got methodtype request");
break;
case METHOD_REQUEST:
/*
* TODO:
* 1. Check whether the remote side exists
* 2. Decode components (Where will the unboxing take place?)
* 3. Generate and send the D-Bus message
* 4. If this is not one-way, schedule waiting for the reply.
*/
case CONNECTION_SHUTDOWN:
/*
* TODO: Cleanup
*/
NSDebugMLog(@"Got CONNECTION_SHUTDOWN");
break;
case PROXY_RETAIN:
NSDebugMLog(@"Got PROXY_RETAIN");
break;
case METHOD_REPLY:
/*
* TODO:
* 1. Decode components (how will we box them?)
* 2.
*/
case ROOTPROXY_REPLY:
case METHODTYPE_REPLY:
case PROXY_RELEASE:
case RETAIN_REPLY:
NSDebugMLog(@"Got reply type %ld", msgid);
break;
case PROXY_AT_PATH_REQUEST:
/*
* TODO:
* 1. Check whether the remote side exists.
* 2. Discover the object path.
* 3. Create proxy
*/
NSDebugMLog(@"Special proxy request");
break;
case PROXY_AT_PATH_REPLY:
/*
* TODO:
* 1. Do something
*/
NSDebugMLog(@"Special proxy reply");
break;
default:
break;
}
return NO;
}
/**
* Required for NSPort compatibility.
*/
- (void) receivedEvent: (void*)data
type: (RunLoopEventType)type
extra: (void*)extra
forMode: (NSString*)mode
{
NSDebugMLog(@"RunLoop events: Ignoring event of type %llu", (unsigned long long)type);
}
/**
* Required for NSPort compatibility. Will make NSRunLoop leave us alone because
* we don't have any file descriptors to watch.
*/
- (void) getFds: (int*)fds count: (int*)count
{
*fds=0;
*count=0;
}
/**
* Required for NSPort compatibility.
*/
- (NSUInteger)reservedSpaceLength
{
return 0;
}
- (void)invalidate
{
[[DKNotificationCenter centerForBusType: [endpoint DBusBusType]] removeObserver: self];
// The implementation in NSPort sends out the appropriate notification.
[super invalidate];
}
- (void)dealloc
{
[[DKNotificationCenter centerForBusType: [endpoint DBusBusType]] removeObserver: self];
[endpoint release];
[remote release];
[super dealloc];
}
- (void)_remoteDisappeared: (NSNotification*)n
{
NSDictionary *userInfo = [n userInfo];
NSString *name = [userInfo objectForKey: @"arg0"];
NSString *newOwner = [userInfo objectForKey: @"arg2"];
// Bail out if we got rubbish data from the notification:
if ((NO == [@"" isEqualToString: newOwner])
|| (NO == [remote isEqualToString: name]))
{
return;
}
[self invalidate];
}
- (void)_disconnected: (NSNotification*)n
{
[self invalidate];
}
/**
* Performs checks to ensure that the corresponding D-Bus service and object
* path exist and sends a message to the delegate NSConnection object containing
* an encoded DKProxy.
*/
- (BOOL) _returnProxyForPath: (NSString*)path
utilizingComponents: (NSArray*)components
fromPort: (NSPort*)receivePort
{
int sequence = -1;
NSPortCoder *seqCoder = nil;
NSPortCoder *proxyCoder = nil;
DKProxy *proxy = nil;
NSPortMessage *pm = nil;
if (NO == [self hasValidRemote])
{
return NO;
}
/* Decode the sequence number, we need it to send the correct reply. */
seqCoder = [[NSPortCoder alloc] initWithReceivePort: receivePort
sendPort: self
components: components];
[seqCoder decodeValueOfObjCType: @encode(int) at: &sequence];
NSDebugMLog(@"Sequence number for proxy request: %d", sequence);
[seqCoder release];
/* Create and encode the proxy. */
proxyCoder = [[NSPortCoder alloc] initWithReceivePort: receivePort
sendPort: self
components: nil];
proxy = [DKProxy proxyWithPort: self
path: path];
if (nil == proxy)
{
NSDebugMLog(@"Got nil proxy for %@ (path: %@).", self, path);
[proxyCoder release];
return NO;
}
[proxyCoder encodeValueOfObjCType: @encode(int) at: &sequence];
[proxyCoder encodeObject: proxy];
/* Wrap it in an NSPortMessage */
pm = [[NSPortMessage alloc] initWithSendPort: self
receivePort: receivePort
components: [proxyCoder _components]];
[pm setMsgid: ROOTPROXY_REPLY];
/* Let the connection handle it */
[[receivePort delegate] handlePortMessage: pm];
/* Cleanup */
[pm release];
[proxyCoder release];
return YES;
}
@end
|