Skip to content

Instantly share code, notes, and snippets.

@jonnor
Last active August 29, 2015 14:06
Show Gist options
  • Save jonnor/86abad941b05e9ce5c00 to your computer and use it in GitHub Desktop.
Save jonnor/86abad941b05e9ce5c00 to your computer and use it in GitHub Desktop.
Dataflow/FBP engine sketch in Objective-C
#include <Foundation/Foundation.h>
#include <stdio.h>
#include <objc/objc.h>
/*
TODO: Implement addNode,connect,addInitial for building network incrementally
TODO: Implement a component factory, to instantiate component from string name
TODO: Implement parsing of FBP .json format, build network from this
TODO: Implement queing of messages and asyncronous delicivery
*/
// Packet
typedef enum {
kPacketData,
kPacketBracketStart,
kPacketBracketEnd
} PacketType;
@interface Packet : NSObject
{
PacketType type;
//id data;
NSInteger integer;
}
@property (nonatomic) NSInteger integer;
@end
// Component
@protocol ComponentProtocol <NSObject>
- (void)process: (Packet *)packet onPort:(NSString *)portName;
- (void)send: (Packet *)packet onPort:(NSString *)portName;
@end
@class Network;
@interface Component : NSObject <ComponentProtocol>
{
id data;
Network *_network;
}
@end
@interface Network : NSObject
{
NSDictionary *nodes;
NSDictionary *connections;
}
- (void)_sendPacket: (Packet *)packet from: (Component *)srcNode outPort: (NSString *)srcPort;
@end
@implementation Packet
@synthesize integer;
@end
@implementation Component
- (id)init: (Network *)net {
self = [super init];
if (self != nil) {
self->_network = net;
}
return self;
}
- (void)process: (Packet *)packet onPort:(NSString *)portName {
NSLog(@"ERRROR: Component::process() not implememted");
}
- (void)send: (Packet *)packet onPort:(NSString *)portName {
[_network _sendPacket: packet from: self outPort: portName];
}
@end
@interface AddOneComponent : Component
@end
@implementation AddOneComponent
- (void)process: (Packet *)packet onPort:(NSString *)portName {
NSLog(@"AddOneComponent process");
Packet *new = [[Packet alloc] init];
new.integer = (packet.integer)+1;
[self send: new onPort: @"out"];
}
@end
// Network
@implementation Network
- (id)init {
self = [super init];
if (self != nil) {
NSLog(@"Network init");
self->nodes = [NSDictionary dictionaryWithObjectsAndKeys:
[[AddOneComponent alloc] init: self], @"first",
[[AddOneComponent alloc] init: self], @"second",
[[AddOneComponent alloc] init: self], @"third",
nil
];
// node:port
self->connections = [NSDictionary dictionaryWithObjectsAndKeys:
@"second:in", @"first:out",
@"third:in", @"second:out",
nil
];
Packet *iip = [[Packet alloc] init];
iip.integer = 11;
[(Component *)([self->nodes objectForKey: @"first"]) process : iip onPort: @"in"];
NSLog(@"Network init complete");
}
return self;
}
- (void)_sendPacket: (Packet *)packet from: (Component *)srcNode outPort: (NSString *)srcPort
{
NSLog(@"%d", packet.integer);
NSArray *keys = [self->nodes allKeysForObject: srcNode];
if ([keys count] == 1) {
NSString *srcNodeId = (NSString *)[keys objectAtIndex: 0];
NSString *srcConn = [srcNodeId stringByAppendingString: @":out"];
NSString *tgtConn = [self->connections objectForKey: srcConn];
NSArray *tokens = [tgtConn componentsSeparatedByString:@":"];
if ([tokens count] == 2) {
Component *tgtNode = (Component *)[self->nodes objectForKey: [tokens objectAtIndex: 0]];
[tgtNode process: packet onPort: [tokens objectAtIndex: 1]];
} else {
NSLog(@"ERROR: expected node:port");
}
} else {
NSLog(@"ERROR: expected unique identifier for component");
}
}
/*
- (void)addNode: (NSString *)name withComponent: (Component *) component
{
}
- (void)connect: (NSString *)srcNode outPort: (NSString *)srcPort
toNode: (NSString *)tgtNode inPort: (NSString *)tgtPort
{
}
- (void)setInitial: (Packet *)packet toNode: (NSString *)tgtNode inPort: (NSString *)tgtPort
{
}
*/
@end
int main(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Network *net = [[Network alloc] init];
[pool release];
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment