Skip to content

Commit

Permalink
add remote host and port in messages and bundles
Browse files Browse the repository at this point in the history
All bundle sub-elements will have the values set. It isn't clear if this is actually helpful as the remote port says nothing of it being reachable.
  • Loading branch information
pizthewiz committed Feb 20, 2014
1 parent d3d83ef commit f5ea502
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 32 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
@@ -1,9 +1,9 @@

### v0.3.0 (XX YYY 2013)
### v0.3.0 (XX YYY 2014)
* ADD bundle support (received bundles are dispatched immediately)
* ADD static library project target for iOS support
* ADD time tag type
* ADD remote host and port to message
* ADD remote host and port properties on a message and bundle
* CHANGE sender should be connectionless
* CHANGE migrate sender to callback block instead of delegate
* CHANGE unit testing via specta and expecta
Expand Down
9 changes: 6 additions & 3 deletions PonyExpress/PEOSCBundle-Private.h
Expand Up @@ -3,14 +3,17 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 24 Mar 2013.
// Copyright (c) 2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2013-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCBundle.h"

@interface PEOSCBundle ()
+ (instancetype)bundleWithData:(NSData*)data;
- (instancetype)initWithData:(NSData*)data;
+ (instancetype)bundleWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port;
- (instancetype)initWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port;

@property (nonatomic, readwrite, strong) NSString* remoteHost;
@property (nonatomic, readwrite) uint16_t remotePort;

+ (BOOL)_dataIsLikelyBundle:(NSData*)data;
- (BOOL)_isValid;
Expand Down
4 changes: 3 additions & 1 deletion PonyExpress/PEOSCBundle.h
Expand Up @@ -3,7 +3,7 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 24 Mar 2013.
// Copyright (c) 2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2013-2014 Chorded Constructions. All rights reserved.
//

#import <Foundation/Foundation.h>
Expand All @@ -15,4 +15,6 @@
- (instancetype)initWithElements:(NSArray*)elements;

@property (nonatomic, strong) NSArray* elements; // could be a mix of messages and bundles
@property (nonatomic, readonly, strong) NSString *remoteHost;
@property (nonatomic, readonly) uint16_t remotePort;
@end
15 changes: 9 additions & 6 deletions PonyExpress/PEOSCBundle.m
Expand Up @@ -3,7 +3,7 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 24 Mar 2013.
// Copyright (c) 2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2013-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCBundle.h"
Expand Down Expand Up @@ -33,17 +33,20 @@ - (instancetype)initWithElements:(NSArray*)elements {
return self;
}

+ (instancetype)bundleWithData:(NSData*)data {
id bundle = [[[self class] alloc] initWithData:data];
+ (instancetype)bundleWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port {
id bundle = [[[self class] alloc] initWithData:data remoteHost:host remotePort:port];
return bundle;
}

- (instancetype)initWithData:(NSData*)data {
- (instancetype)initWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port {
self = [super init];
if (self) {
NSUInteger length = [data length];
NSUInteger start = 0;

self.remoteHost = host;
self.remotePort = port;

// check for bundle marker
if (![PEOSCBundle _dataIsLikelyBundle:data]) {
CCErrorLog(@"ERROR - missing bundle marker, bundle dropped");
Expand All @@ -69,12 +72,12 @@ - (instancetype)initWithData:(NSData*)data {
// divine element type
NSData* subdata = [data subdataWithRange:NSMakeRange(start, value)];
if ([PEOSCBundle _dataIsLikelyBundle:subdata]) {
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:subdata];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:subdata remoteHost:host remotePort:port];
if (bundle) {
[elements addObject:bundle];
}
} else {
PEOSCMessage* message = [PEOSCMessage messageWithData:subdata];
PEOSCMessage* message = [PEOSCMessage messageWithData:subdata remoteHost:host remotePort:port];
if (message) {
[elements addObject:message];
}
Expand Down
6 changes: 3 additions & 3 deletions PonyExpress/PEOSCMessage-Private.h
Expand Up @@ -3,14 +3,14 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 02 Sept 2011.
// Copyright (c) 2011-2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2011-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCMessage.h"

@interface PEOSCMessage ()
+ (instancetype)messageWithData:(NSData*)data;
- (instancetype)initWithData:(NSData*)data;
+ (instancetype)messageWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port;
- (instancetype)initWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(uint16_t)port;

@property (nonatomic, readwrite, strong) NSString* remoteHost;
@property (nonatomic, readwrite) uint16_t remotePort;
Expand Down
11 changes: 7 additions & 4 deletions PonyExpress/PEOSCMessage.m
Expand Up @@ -3,7 +3,7 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 02 Sept 2011.
// Copyright (c) 2011-2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2011-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCMessage.h"
Expand Down Expand Up @@ -39,17 +39,20 @@ - (instancetype)initWithAddress:(NSString*)address typeTags:(NSArray*)typeTags a
return self;
}

+ (instancetype)messageWithData:(NSData*)data {
id message = [[[self class] alloc] initWithData:data];
+ (instancetype)messageWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(u_int16_t)port {
id message = [[[self class] alloc] initWithData:data remoteHost:host remotePort:port];
return message;
}

- (instancetype)initWithData:(NSData*)data {
- (instancetype)initWithData:(NSData*)data remoteHost:(NSString*)host remotePort:(u_int16_t)port {
self = [super init];
if (self) {
NSUInteger length = [data length];
NSUInteger start = 0;

self.remoteHost = host;
self.remotePort = port;

// address
NSString* addressString = [data readStringAtOffset:start];
// TODO - replace naïve validation with +[PEOSCMessage addressIsValid:]
Expand Down
9 changes: 5 additions & 4 deletions PonyExpress/PEOSCReceiver.m
Expand Up @@ -96,17 +96,18 @@ - (void)udpSocket:(GCDAsyncUdpSocket*)sock didReceiveData:(NSData*)data fromAddr

// NB - messages sent to 0.0.0.0 could be receied n times for n IP addreses on the local machine

NSString* host = [GCDAsyncUdpSocket hostFromAddress:address];
u_int16_t port = [GCDAsyncUdpSocket portFromAddress:address];

if ([PEOSCBundle _dataIsLikelyBundle:data]) {
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data remoteHost:host remotePort:port];
if (!bundle) {
// TODO - error?
return;
}
[self.delegate didReceiveBundle:bundle];
} else {
PEOSCMessage* message = [PEOSCMessage messageWithData:data];
message.remoteHost = [GCDAsyncUdpSocket hostFromAddress:address];
message.remotePort = [GCDAsyncUdpSocket portFromAddress:address];
PEOSCMessage* message = [PEOSCMessage messageWithData:data remoteHost:host remotePort:port];
if (!message) {
// TODO - error?
return;
Expand Down
8 changes: 5 additions & 3 deletions PonyExpressTests/PEOSCBundleSpec.m
Expand Up @@ -3,7 +3,7 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 09 Jul 2013.
// Copyright (c) 2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2013-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCBundle-Private.h"
Expand Down Expand Up @@ -39,6 +39,8 @@
expect(bundle.elements).to.beIdenticalTo(messages);
});

// TODO - remoteHost and remotePort

#pragma mark - ELEMENTS

it(@"should report elements as invalid when containing bad element", ^{
Expand Down Expand Up @@ -100,7 +102,7 @@

it(@"should not create a bundle instance from bad data", ^{
NSData* data = [@"XYZZY" dataUsingEncoding:NSASCIIStringEncoding];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data remoteHost:nil remotePort:0];
expect(bundle).to.beNil();

// TODO - more complex examples
Expand All @@ -120,7 +122,7 @@

it(@"should create non-nil, equal bundle", ^{
NSData* data = [sourceBundle _data];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data];
PEOSCBundle* bundle = [PEOSCBundle bundleWithData:data remoteHost:nil remotePort:0];
expect(bundle).toNot.beNil();
expect(bundle.elements).to.equal(sourceBundle.elements);
expect(bundle).to.equal(sourceBundle);
Expand Down
10 changes: 5 additions & 5 deletions PonyExpressTests/PEOSCMessageSpec.m
Expand Up @@ -3,7 +3,7 @@
// PonyExpress
//
// Created by Jean-Pierre Mouilleseaux on 26 Dec 2012.
// Copyright (c) 2012-2013 Chorded Constructions. All rights reserved.
// Copyright (c) 2012-2014 Chorded Constructions. All rights reserved.
//

#import "PEOSCMessage-Private.h"
Expand Down Expand Up @@ -33,7 +33,7 @@
expect(message.arguments).to.beIdenticalTo(allArgs);
});

// TODO - remoteHost and remotePort?
// TODO - remoteHost and remotePort

#pragma mark - ADDRESS

Expand Down Expand Up @@ -155,7 +155,7 @@

it(@"should not create a message instance from bad data", ^{
NSData* data = [@"XYZZY" dataUsingEncoding:NSASCIIStringEncoding];
PEOSCMessage* message = [PEOSCMessage messageWithData:data];
PEOSCMessage* message = [PEOSCMessage messageWithData:data remoteHost:nil remotePort:0];
expect(message).to.beNil();
});

Expand All @@ -169,7 +169,7 @@
});
it(@"should create non-nil, equal message", ^{
NSData* data = [sourceMessage _data];
PEOSCMessage* message = [PEOSCMessage messageWithData:data];
PEOSCMessage* message = [PEOSCMessage messageWithData:data remoteHost:nil remotePort:0];
expect(message).toNot.beNil();
expect(message.address).to.equal(sourceMessage.address);
expect(message.typeTags).to.equal(sourceMessage.typeTags);
Expand Down Expand Up @@ -200,7 +200,7 @@
it(@"should create message instance from message with immediate time tag data", ^{
PEOSCMessage* sourceMessage = [PEOSCMessage messageWithAddress:address typeTags:@[PEOSCMessageTypeTagTimetag] arguments:@[[NSDate OSCImmediate]]];
NSData* data = [sourceMessage _data];
PEOSCMessage* message = [PEOSCMessage messageWithData:data];
PEOSCMessage* message = [PEOSCMessage messageWithData:data remoteHost:nil remotePort:0];
expect(message).toNot.beNil();
expect(message.typeTags).to.equal(sourceMessage.typeTags);
expect(message.arguments).to.equal(sourceMessage.arguments);
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Expand Up @@ -13,7 +13,6 @@
- ignore unknown types (is that even possible?)

### BUNDLE
- store originating host and port
- double check that bundle element length doesn't overflow int32
- do a better job with NSData allocation and writing in -_data?
- respect time tag dispatch time
Expand Down

0 comments on commit f5ea502

Please sign in to comment.