Permalink
Browse files

add remote host and port in messages and bundles

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...
1 parent d3d83ef commit f5ea502a25b9fa9762301943e0ce2ed16e6b7d50 @pizthewiz committed Feb 20, 2014
View
@@ -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
@@ -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;
@@ -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>
@@ -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
View
@@ -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"
@@ -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");
@@ -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];
}
@@ -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;
View
@@ -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"
@@ -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:]
@@ -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;
@@ -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"
@@ -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", ^{
@@ -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
@@ -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);
@@ -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"
@@ -33,7 +33,7 @@
expect(message.arguments).to.beIdenticalTo(allArgs);
});
-// TODO - remoteHost and remotePort?
+// TODO - remoteHost and remotePort
#pragma mark - ADDRESS
@@ -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();
});
@@ -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);
@@ -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);
View
@@ -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

0 comments on commit f5ea502

Please sign in to comment.