Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 8bf198e0feab67dc976e02560014c44552db4d96 @kazuyukitanimura committed Nov 9, 2012
Showing with 95 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +3 −0 README.md
  3. +78 −0 index.js
  4. +10 −0 package.json
4 .gitignore
@@ -0,0 +1,4 @@
+node_modules
+.*.swp
+npm-debug.log
+dump.rdb
3 README.md
@@ -0,0 +1,3 @@
+addressbook
+============
+TBW
78 index.js
@@ -0,0 +1,78 @@
+var os = require('os');
+var redis = require("redis");
+var dbName = 'addressbook';
+
+var NodeAddressbook = module.exports = function(port, host, redisOpt) {
+ var client = this.client = redis.createClient(port, host, redisOpt);
+ client.on('error', function(err) {
+ console.error(err);
+ });
+ this.myAddress;
+ this.protocols;
+ this.addressbook;
+};
+
+var returnCallBack = function(cb, err, data) {
+ if (!cb) {
+ console.error('callback function is mandatory');
+ process.exit(0);
+ }
+ process.nextTick(cb.bind(err, data));
+};
+
+NodeAddressbook.prototype.update = function(protocols, cb) {
+ //protocols = [{
+ // protocol: 'tcp',
+ // port: '6789'
+ //}];
+ if (typeof protocols === 'function') {
+ cb = protocols;
+ protocols = undefined;
+ }
+ if (protocols && ! Array.isArray(protocols)) {
+ returnCallBack(cb, 'protocols has to be an Array');
+ }
+ if (!protocols) {
+ protocols = this.protocols;
+ if (!protocols) {
+ returnCallBack(cb, 'protocols has to be provided at least once');
+ }
+ }
+
+ // get the network IP
+ // http://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
+ var ifaces = os.networkInterfaces();
+ var en0;
+ for (var dev in ifaces) {
+ ifaces[dev].forEach(function(details) {
+ if (details.family === 'IPv4' || details.family === 'IPv6') {
+ if (dev === 'en0' && !en0) {
+ en0 = details.address;
+ }
+ }
+ });
+ }
+
+ if (!en0) {
+ returnCallBack(cb, 'No network IP found');
+ } else {
+ var client = this.client;
+
+ if (en0 !== this.myAddress || protocols !== this.protocols) {
+ this.myAddress = en0;
+ this.protocols = protocols;
+ client.hset(dbName, en0, JSON.stringify(protocols), function(err, res) {
+ if (err) {
+ returnCallBack(cb, err);
+ } else if (res !== 0) {
+ returnCallBack(cb, 'Redis did not return 0');
+ }
+ });
+ }
+
+ client.hgetall(dbName, function(err, data) {
+ returnCallBack(cb, err, JSON.parse(data));
+ });
+ }
+};
+
10 package.json
@@ -0,0 +1,10 @@
+{
+ "name": "addressbook"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "redis" : ">= 0.8.2"
+ }
+ , "engine": "node >= 0.8.14"
+ , "license": "MIT"
+}

0 comments on commit 8bf198e

Please sign in to comment.
Something went wrong with that request. Please try again.