Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[css] Added Bonjour support for chameleon/observer

  • Loading branch information...
commit f357b9d283b35d8d774b56f4bdee0358bd8a24b1 1 parent e2730e4
Max Metral authored
View
4 src/css/chameleon/chameleon.js
@@ -22,7 +22,7 @@ var server = require("./server");
// Only accept strings.
if (typeof argv.watch != 'string') {
- console.log("Usage:\n node chameleon.js --watch <path to watch changes in>");
+ console.log("Usage:\n node chameleon.js --watch <path to watch changes in> --bonjour <service_identifier>");
return;
}
@@ -35,4 +35,4 @@ if (!path.existsSync(argv.watch)) { // it's interesting that methods are assumed
console.log("Starting chameleon...");
console.log(" Watching "+argv.watch);
-server.start(argv.watch);
+server.start(argv.watch,argv.bonjour);
View
3  src/css/chameleon/package.json
@@ -6,6 +6,7 @@
"npm": "1.1.x"
},
"dependencies": {
- "optimist": "0.3.4"
+ "optimist": "0.3.4",
+ "mdns": "1.1.0"
}
}
View
12 src/css/chameleon/server.js
@@ -18,6 +18,7 @@ var http = require("http");
var url = require("url");
var path = require('path');
var fs = require('fs');
+var mdns = null;
var changeSet = {};
var activeWatcher = null;
@@ -28,8 +29,11 @@ var watchInterval = 500; // 500 ms makes it effectively instant.
/**
* Starts the Chameleon HTTP server and file watcher on the given path.
*/
-function start(watchPath) {
+function start(watchPath,bonjour) {
+ if (bonjour) {
+ mdns = require('mdns');
+ }
/**
* Load a file from disk and pipe it down.
*/
@@ -66,6 +70,7 @@ function start(watchPath) {
* Otherwise the consume method will be stowed away until a file does change.
*/
function onWatch(request, response) {
+ console.log("Client connected from", request.connection.remoteAddress);
var sendResponse = function () {
response.writeHead(200, { 'Content-Type':'text/plain' });
var changed = [];
@@ -177,6 +182,11 @@ function start(watchPath) {
http.createServer(onRequest).listen(port);
console.log(" Server: http://localhost:" + port + "/");
+
+ if (bonjour) {
+ var ad = mdns.createAdvertisement(mdns.tcp(bonjour), port);
+ ad.start();
+ }
}
// This allows us to call server.start from the chameleon.js file.
View
9 src/css/src/NIChameleonObserver.h
@@ -56,6 +56,8 @@ extern NSString* const NIJSONDidChangeNameKey;
- (void)watchSkinChanges;
+- (void)enableBonjourDiscovery: (NSString*) serviceName;
+
@end
/**
@@ -78,3 +80,10 @@ extern NSString* const NIJSONDidChangeNameKey;
*
* @fn NIChameleonObserver::watchSkinChanges
*/
+
+/**
+ * Browses Bonjour for services with the given name (e.g. your username) and sets the host
+ * automatically.
+ *
+ * @fn NIChameleonObserver::enableBonjourDiscovery:
+ */
View
38 src/css/src/NIChameleonObserver.m
@@ -28,14 +28,20 @@
static NSString* const kWatchFilenameKey = @"___watch___";
static const NSTimeInterval kTimeoutInterval = 1000;
+static const NSTimeInterval kRetryInterval = 10000;
static const NSInteger kMaxNumberOfRetries = 3;
NSString* const NIJSONDidChangeNotification = @"NIJSONDidChangeNotification";
NSString* const NIJSONDidChangeFilePathKey = @"NIJSONPathKey";
NSString* const NIJSONDidChangeNameKey = @"NIJSONNameKey";
-@interface NIChameleonObserver()
+@interface NIChameleonObserver() <
+ NSNetServiceBrowserDelegate,
+ NSNetServiceDelegate
+>
- (NSString *)pathFromPath:(NSString *)path;
+@property (nonatomic,strong) NSNetServiceBrowser *netBrowser;
+@property (nonatomic,strong) NSNetService *netService;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -62,7 +68,7 @@ - (id)initWithStylesheetCache:(NIStylesheetCache *)stylesheetCache host:(NSStrin
if ([host hasSuffix:@"/"]) {
_host = [host copy];
- } else {
+ } else if (host) {
_host = [[host stringByAppendingString:@"/"] copy];
}
@@ -244,12 +250,38 @@ - (void)watchSkinChanges {
if (_retryCount < kMaxNumberOfRetries) {
++_retryCount;
- [self watchSkinChanges];
+ dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kRetryInterval * NSEC_PER_MSEC));
+ dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
+ [self watchSkinChanges];
+ });
}
}];
[_queue addOperation:requestOp];
}
+-(void)enableBonjourDiscovery:(NSString *)serviceName
+{
+ self.netBrowser = [[NSNetServiceBrowser alloc] init];
+ self.netBrowser.delegate = self;
+ [self.netBrowser searchForServicesOfType:[NSString stringWithFormat:@"_%@._tcp", serviceName] inDomain:@""];
+}
+
+-(void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
+{
+ [self.netBrowser stop];
+ self.netBrowser = nil;
+
+ self.netService = aNetService;
+ aNetService.delegate = self;
+ [aNetService resolveWithTimeout:15.0];
+}
+
+-(void)netServiceDidResolveAddress:(NSNetService *)sender
+{
+ _host = [NSString stringWithFormat:@"http://%@:%d/", [sender hostName], [sender port]];
+ self.netService = nil;
+ [self watchSkinChanges];
+}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.