Skip to content

Commit

Permalink
LukeIS: changing SessionId to be 'unique' for iOS. Now is a composite…
Browse files Browse the repository at this point in the history
… of ip address, port and previous session id. This is so grid will not confuse multiple instances of iOS, since Grid assumes sessionId is unique. Also changing which IP address is chosen to prefer the wifi connection over the carrier ip.

r14945
  • Loading branch information
lukeis committed Nov 28, 2011
1 parent 4fc7b79 commit fb2df06
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 80 deletions.
5 changes: 0 additions & 5 deletions iphone/src/objc/Cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@
// This |HTTPVirtualDirectory| matches the /hub/:session/cookie
// directory in the WebDriver REST service.
@interface Cookie : HTTPVirtualDirectory {
int sessionId_;
}

- (id)initWithSessionId:(int)sessionId;

+ (Cookie*)cookieWithSessionId:(int)sessionId;

- (NSURL *)currentUrl;
- (NSArray *)getCookies;
- (void)addCookie:(NSDictionary *)cookie;
Expand Down
6 changes: 1 addition & 5 deletions iphone/src/objc/Cookie.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@

@implementation Cookie

- (id) initWithSessionId:(int)sessionId {
- (id) init {
self = [super init];
if (!self) {
return nil;
}
sessionId_ = sessionId;

[self setIndex:
[WebDriverResource resourceWithTarget:self
Expand All @@ -45,9 +44,6 @@ - (id) initWithSessionId:(int)sessionId {
return self;
}

+ (Cookie*) cookieWithSessionId:(int)sessionId {
return [[[Cookie alloc] initWithSessionId:sessionId] autorelease];
}

- (NSURL *)currentUrl {
return [NSURL URLWithString:[[self viewController] URL]];
Expand Down
6 changes: 0 additions & 6 deletions iphone/src/objc/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@
#import <sqlite3.h>

@interface Database : HTTPVirtualDirectory {
int sessionId_;
}

- (id)initWithSessionId:(int)sessionId;

+ (Database *)databaseWithSessionId:(int)sessionId;

- (NSDictionary *)executeSql:(NSDictionary *)dict;


@end
7 changes: 1 addition & 6 deletions iphone/src/objc/Database.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@

@implementation Database

- (id)initWithSessionId:(int)sessionId {
- (id)init {
self = [super init];
if (!self) {
return nil;
}
sessionId_ = sessionId;

[self setIndex:
[WebDriverResource resourceWithTarget:self
Expand All @@ -43,10 +42,6 @@ - (id)initWithSessionId:(int)sessionId {
return self;
}

+ (Database *)databaseWithSessionId:(int)sessionId {
return [[[Database alloc] initWithSessionId:sessionId] autorelease];
}


// Get HTML5 database path + db name from local db path
// return autoreleased object.
Expand Down
10 changes: 8 additions & 2 deletions iphone/src/objc/HTTPServerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ -(NSString *)getAddress {
struct in_addr inaddr = ((struct sockaddr_in *)sock)->sin_addr;
char *name = inet_ntoa(inaddr);
address = [NSString stringWithUTF8String:name];
break;
// if wifi use this, otherwise it's the carrier network (pdp_ip0)
// keep looking for the wifi, if no other network interface is found
// it will use the carrier network.
if ([interfaceName isEqualToString:@"en0"]) {
break;
}
}
}

Expand Down Expand Up @@ -165,7 +170,8 @@ -(id) init {

}

serviceMapping_ = [[RESTServiceMapping alloc] init];
serviceMapping_ = [[RESTServiceMapping alloc] initWithIpAddress:[self getAddress]
port:[server_ port]];

return self;
}
Expand Down
9 changes: 6 additions & 3 deletions iphone/src/objc/RESTServiceMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
#import <CFNetwork/CFNetwork.h>
#import "HTTPResponse.h"

@class VirtualDirectory;
@class HTTPVirtualDirectory;

// |RESTServiceMapping| stores the tree of virtual directories. When the HTTP
// server recieves an http request, the request is forwarded through
// |HTTPServerController| to |RESTServiceMapping| which in turn delegates it to
// the appropriate |HTTPResource|.
@interface RESTServiceMapping : NSObject {
// Stores the root directory of the HTTP server
VirtualDirectory *serverRoot_;
HTTPVirtualDirectory *serverRoot_;
}

@property (nonatomic, copy) VirtualDirectory *serverRoot;
@property (nonatomic, copy) HTTPVirtualDirectory *serverRoot;

- (id) initWithIpAddress:(NSString *)ipAddress
port:(int)port;

- (NSObject<HTTPResponse> *)httpResponseForRequest:(CFHTTPMessageRef)request;

Expand Down
5 changes: 3 additions & 2 deletions iphone/src/objc/RESTServiceMapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ @implementation RESTServiceMapping

@synthesize serverRoot = serverRoot_;

- (id)init {
- (id)initWithIpAddress:(NSString *)ipAddress
port:(int)port {
if (![super init])
return nil;

Expand Down Expand Up @@ -65,7 +66,7 @@ - (id)init {
[restRoot setIndex:[HTTPStaticResource resourceWithResponse:response]];
[response release];

[restRoot setResource:[[[SessionRoot alloc] init] autorelease]
[restRoot setResource:[[SessionRoot alloc] initWithAddress:ipAddress port:[NSString stringWithFormat:@"%d", port] ]
withName:@"session"];

return self;
Expand Down
6 changes: 3 additions & 3 deletions iphone/src/objc/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
// expects.
@interface Session : HTTPVirtualDirectory {
SessionRoot* sessionRoot_;
int sessionId_;
NSString* sessionId_;
ElementStore* elementStore_;
NSTimeInterval implicitWait_;
NSTimeInterval scriptTimeout_;
}

@property (nonatomic, readonly) ElementStore* elementStore;
@property (nonatomic) int sessionId;
@property (nonatomic, retain) NSString *sessionId;
@property (nonatomic) NSTimeInterval implicitWait;
@property (nonatomic) NSTimeInterval scriptTimeout;

- (id) initWithSessionRootAndSessionId:(SessionRoot*)root
sessionId:(int)sessionId;
sessionId:(NSString *)sessionId;

- (void)cleanSessionStatus;

Expand Down
43 changes: 18 additions & 25 deletions iphone/src/objc/Session.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ - (void)setResourceToViewMethodGET:(SEL)getMethod
}

- (id) initWithSessionRootAndSessionId:(SessionRoot*)root
sessionId:(int)sessionId {
sessionId:(NSString*)sessionId {
self = [super init];
if (!self) {
return nil;
}
sessionRoot_ = root;
sessionId_ = sessionId;
sessionId_ = [[NSString alloc] initWithString:sessionId];

[self setIndex:[WebDriverResource
resourceWithTarget:self
Expand Down Expand Up @@ -114,13 +114,11 @@ - (id) initWithSessionRootAndSessionId:(SessionRoot*)root
withName:@"screenshot"];

// HTML5 Local WebStorage
[self setResource:[Storage storageWithSessionId:sessionId_
andType:LOCAL_STORAGE]
[self setResource:[Storage storageWithType:LOCAL_STORAGE]
withName:@"local_storage"];

// HTML5 Session WebStorage
[self setResource:[Storage storageWithSessionId:sessionId_
andType:SESSION_STORAGE]
[self setResource:[Storage storageWithType:SESSION_STORAGE]
withName:@"session_storage"];

// HTML5 Get and Set GeoLocation
Expand All @@ -129,7 +127,7 @@ - (id) initWithSessionRootAndSessionId:(SessionRoot*)root
withName:@"location"];

// HTML5 Database Storage
[self setResource:[Database databaseWithSessionId:sessionId_]
[self setResource:[Database alloc]
withName:@"execute_sql"];

// Execute JS function with the given body.
Expand All @@ -151,7 +149,7 @@ - (id) initWithSessionRootAndSessionId:(SessionRoot*)root
// page.
elementStore_ = [ElementStore elementStoreForSession:self];

[self setResource:[Cookie cookieWithSessionId:sessionId_]
[self setResource:[Cookie alloc]
withName:@"cookie"];

[self setResource:[Timeouts timeoutsForSession:self]
Expand All @@ -169,26 +167,21 @@ - (void)cleanSessionStatus{
}

- (id)capabilities {
NSMutableDictionary *caps = [NSMutableDictionary dictionary];
[caps setObject:@"mobile safari" forKey:@"browserName"];
[caps setObject:@"MAC" forKey:@"platform"];
[caps setValue:[NSNumber numberWithBool:YES]
forKey:@"javascriptEnabled"];
[caps setValue:[NSNumber numberWithBool:YES]
forKey:@"webStorageEnabled"];
[caps setValue:[NSNumber numberWithBool:YES]
forKey:@"databaseEnabled"];
[caps setValue:[NSNumber numberWithBool:YES]
forKey:@"locationContextEnabled"];
[caps setValue:[NSNumber numberWithBool:YES]
forKey:@"takesScreenshot"];
[caps setObject:[[UIDevice currentDevice] systemVersion]
forKey:@"version"];
NSMutableDictionary *caps = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[[[[UIDevice currentDevice] model] componentsSeparatedByString:@" "] objectAtIndex:0], @"browserName",
@"MAC", @"platform",
[NSNumber numberWithBool:YES], @"javascriptEnabled",
[NSNumber numberWithBool:YES], @"webStorageEnabled",
[NSNumber numberWithBool:YES], @"databaseEnabled",
[NSNumber numberWithBool:YES], @"locationContextEnabled",
[NSNumber numberWithBool:YES], @"takesScreenshot",
[[UIDevice currentDevice] systemVersion], @"version",
nil];
return caps;
}

- (void)deleteSession {
NSLog( @"Delete session: %d", sessionId_ );
NSLog( @"Delete session: %@", sessionId_ );
[contents removeAllObjects];
// Tell the session root to remove this resource.
[sessionRoot_ deleteSessionWithId:sessionId_];
Expand All @@ -211,7 +204,7 @@ - (void)dealloc {

- (void)configureResource:(id<HTTPResource>)resource {
if ([resource respondsToSelector:@selector(setSession:)]) {
[(id)resource setSession:[NSString stringWithFormat:@"%d", sessionId_]];
[(id)resource setSession:sessionId_];
}
}

Expand Down
7 changes: 6 additions & 1 deletion iphone/src/objc/SessionRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@
// root of the WebDriver REST service.
@interface SessionRoot : HTTPVirtualDirectory {
int nextId_;
NSString *ipAddress_;
NSString *port_;
}

- (id)initWithAddress:(NSString *)ipAddress
port:(NSString *)port;

- (NSObject<HTTPResponse> *)createSessionWithData:(id)desiredCapabilities
method:(NSString*)method;
- (void)deleteSessionWithId:(int)sessionId;
- (void)deleteSessionWithId:(NSString *)sessionId;

@end;
27 changes: 13 additions & 14 deletions iphone/src/objc/SessionRoot.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

@implementation SessionRoot

- (id)init {

- (id)initWithAddress:(NSString*)ipAddress
port:(NSString *)port {
if (![super init])
return nil;

Expand All @@ -35,6 +37,8 @@ - (id)init {

// Session IDs start at 1001.
nextId_ = 1001;
ipAddress_ = [[NSString alloc] initWithString:ipAddress];
port_ = [[NSString alloc] initWithString:port];

return self;
}
Expand All @@ -49,10 +53,10 @@ - (id)init {

if (![method isEqualToString:@"POST"] && ![method isEqualToString:@"GET"])
return nil;

int sessionId = nextId_++;
NSString* sessionId = [NSString stringWithFormat:@"%@:%@:%d", ipAddress_, port_, nextId_++];

NSLog(@"session %d created", sessionId);
NSLog(@"session %@ created", sessionId);

// Sessions don't really mean anything on the iphone. There's only one
// browser and only one session.
Expand All @@ -63,20 +67,15 @@ - (id)init {
initWithSessionRootAndSessionId:self
sessionId:sessionId] autorelease];

NSString *sessionIdStr = [NSString stringWithFormat:@"%d", sessionId];
[self setResource:session withName:sessionIdStr];
[self setResource:session withName:sessionId];

return [HTTPRedirectResponse redirectToURL:
[NSString stringWithFormat:@"session/%d/", sessionId]];
[NSString stringWithFormat:@"session/%@/", sessionId]];
}

- (void) deleteSessionWithId:(int)sessionId {
NSString *sessionIdStr = [NSString stringWithFormat:@"%d", sessionId];
[self setResource:nil withName:sessionIdStr];
NSLog(@"session %d deleted", sessionId);
if (sessionId == nextId_ - 1) {
nextId_--;
}
- (void) deleteSessionWithId:(NSString *)sessionId {
[self setResource:nil withName:sessionId];
NSLog(@"session %@ deleted", sessionId);
}

- (void)dealloc {
Expand Down
5 changes: 2 additions & 3 deletions iphone/src/objc/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
// This |HTTPVirtualDirectory| matches the /hub/:session/local_storage
// | session_storage directory in the WebDriver REST service.
@interface Storage : HTTPVirtualDirectory {
int sessionId_;
NSString *storageType_;
}

- (id)initWithSessionId:(int)sessionId andType:(NSString *)type;
- (id)initWithType:(NSString *)type;

+ (Storage *)storageWithSessionId:(int)sessionId andType:(NSString *)type;
+ (Storage *)storageWithType:(NSString *)type;

- (NSString *)storageSize;
- (NSArray *)keySet;
Expand Down
8 changes: 4 additions & 4 deletions iphone/src/objc/Storage.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

@implementation Storage

- (id)initWithSessionId:(int)sessionId andType:(NSString *)type {
- (id)initWithType:(NSString *)type {
self = [super init];
if (!self) {
return nil;
}
sessionId_ = sessionId;
storageType_ = type;

[self setIndex:
Expand All @@ -43,10 +42,11 @@ - (id)initWithSessionId:(int)sessionId andType:(NSString *)type {
return self;
}

+ (Storage *)storageWithSessionId:(int)sessionId andType:(NSString *)type {
return [[[Storage alloc] initWithSessionId:sessionId andType:type] autorelease];
+ (Storage *)storageWithType:(NSString *)type {
return [[[Storage alloc] initWithType:type] autorelease];
}


- (NSString *)storageSize {
NSString *size = [[self viewController] jsEval:[NSString stringWithFormat:
@"%@.length", storageType_]];
Expand Down
2 changes: 1 addition & 1 deletion iphone/src/objc/WebDriverRequestFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (id) init {
connectorAddr = [preferences connectorAddr];
requesterId = [preferences requesterId];

serviceMapping_ = [[RESTServiceMapping alloc] init];
serviceMapping_ = [[RESTServiceMapping alloc] initWithIpAddress:connectorAddr port:3001 ];
status_ = [[NSString alloc] initWithFormat:@"Driven by requests from %@ routed by %@",
requesterId, connectorAddr];

Expand Down

0 comments on commit fb2df06

Please sign in to comment.