Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
\#*\#
node_modules/**
npm-debug.log
/db/services.db/**
/db/test.db/**
*.swp

31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ http://HOST:PORT/static
```
where HOST and PORT are defined in config.js

# Test #

```
node db/db_tasks.js test clear
node db/db_tasks.js test seed
node test.js
```

# DB Scripts #

Available db = [development, test]

Dump DB to console.
```
node db/db_tasks.js <db> dump
```

Seed DB with services.
```
node db/db_tasks.js <db> seed
```

Drop all records.
```
node db/db_tasks.js <db> clear
```


# Gulp/Browserify/Bower #

service-browser uses Gulp, Browserify, and Bower to manage front-end dependencies.
Expand Down Expand Up @@ -233,6 +261,9 @@ It looks like python 2.6 might be a dependency for the mdns module, in which cas
* Currently only lists http/https services
* Searching services
* Organizing by category/etc.
* On service down, remember the service but flag it as down and filter when pushing to clients.
* Implement levedb on client and sync with on connect.
* Save up/down votes from client.

# License #

Expand Down
86 changes: 86 additions & 0 deletions db/db_tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
var level = require('level');

var services = [
{
name: "Maxb OwnCloud",
scope: 'peoplesopen.net',
description: 'Maxb OwnCloud Service',
type: 'storage',
region: 'oakland',
hostname: 'mini.local',
port: 80
},
{
name: "Lake Merrit Dog Club",
scope: 'peoplesopen.net',
description: 'Lake Merrit Dog Club Calendar',
type: 'storage',
region: 'oakland',
hostname: 'mini.local',
port: 80
}
]

//parse user input
if (process.argv[2] == 'test') {
var db = level(__dirname + '/test.db', { encoding: 'json' });
tasks(process.argv[3])
} else if (process.argv[2] == 'development'){
var db = level(__dirname + '/services.db', { encoding: 'json' });
tasks(process.argv[3])
} else {
console.log('please select a data base: [test, development]');
console.log(' -eg. $ node db_task.js test clear');
};


function tasks(task) {
if (task == 'clear') {
clear_db();
} else if (task == 'dump') {
stream_db();
} else if (task == 'seed') {
seed_db();
} else {
console.log('please select a task: [clear, dump, seed <pat to see file>]');
console.log(' -eg. $ node db_task.js development seed seeds/services.json');
}
};

function key_hash(str) {
var hash;
for (var i=0; i < str.length; i += 1) {
var chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
hash = Math.abs(hash).toString(16);
return hash;
};

function construct_key(service) {
var sufix = key_hash(service.name);
return 'service!' + sufix;
};

function stream_db() {
console.log('streaming db:');
var stream = db.createReadStream({ start: 'service!', end: 'service~' });
stream.on('data', function (data) {
console.log(data);
});
};

function clear_db() {
db.createKeyStream().on('data', function (data) {
db.del(data, function () {
console.log('clearing db: ', data);
})
})
};

function seed_db() {
db.put(construct_key(services[0]), services[0], function () {
console.log('adding new service ' + services[0].name + ' to the db');
});
};
57 changes: 57 additions & 0 deletions db/seeds/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

module.exports = [
{
name: "Maxb OwnCloud",
scope: 'peoplesopen.net',
description: 'Maxb OwnCloud Service',
type: 'storage',
region: 'oakland',
hostname: 'mini.local',
port: 80
},
{
name: "A messaging service!",
scope: 'peoplesopen.net',
description: 'This is a mesh service for messaging. Check it out!',
type: 'messaging',
region: 'SF',
hostname: 'mini.local',
port: 80
},
{
name: "Classical Sound Server",
scope: 'peoplesopen.net',
description: 'Classical Music all the time',
type: 'sound',
region: 'oakland',
hostname: 'mini.local',
port: 80
},
{
name: "Another Maxb OwnCloud",
scope: 'peoplesopen.net',
description: 'Maxb OwnCloud Service',
type: 'storage',
region: 'oakland',
hostname: 'mini.local',
port: 80
},
{
name: "Dustep Sounds!",
scope: 'peoplesopen.net',
description: 'Dubstep all the time!',
type: 'sound',
region: 'oakland',
hostname: 'mini.local',
port: 80
},
{
name: "Free Books!",
scope: 'peoplesopen.net',
description: 'Sooooo many free books! Come check them out!',
type: 'book',
region: 'oakland',
hostname: 'mini.local',
port: 80
}
];
Binary file added db/test.db/000025.ldb
Binary file not shown.
Binary file added db/test.db/000032.ldb
Binary file not shown.
Binary file added db/test.db/000039.ldb
Binary file not shown.
Binary file added db/test.db/000042.ldb
Binary file not shown.
1 change: 1 addition & 0 deletions db/test.db/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000341
Empty file added db/test.db/LOCK
Empty file.
5 changes: 5 additions & 0 deletions db/test.db/LOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2014/08/01-19:06:47.144568 b6fffb40 Recovering log #340
2014/08/01-19:06:47.144831 b6fffb40 Level-0 table #342: started
2014/08/01-19:06:47.158360 b6fffb40 Level-0 table #342: 324 bytes OK
2014/08/01-19:06:47.179472 b6fffb40 Delete type=0 #340
2014/08/01-19:06:47.179534 b6fffb40 Delete type=3 #338
5 changes: 5 additions & 0 deletions db/test.db/LOG.old
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2014/08/01-19:06:43.264759 b6f8bb40 Recovering log #337
2014/08/01-19:06:43.264872 b6f8bb40 Level-0 table #339: started
2014/08/01-19:06:43.275681 b6f8bb40 Level-0 table #339: 189 bytes OK
2014/08/01-19:06:43.295586 b6f8bb40 Delete type=0 #337
2014/08/01-19:06:43.295701 b6f8bb40 Delete type=3 #335
93 changes: 61 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ var express = require('express');
var sockjs = require('sockjs');
var mdns = require('mdns2');
var _ = require('lodash');
var level = require('level');
var db = level(__dirname + '/db/services.db', { encoding: 'json' });

var config = require('./config.js');

var clients = [];
var services = [];

// send message to all clients
function broadcast(msg) {
Expand All @@ -49,28 +50,48 @@ function broadcast(msg) {
}
}


function send_all_services(client) {
var i, service;
for(i=0; i < services.length; i++) {
service = services[i];
client.write(JSON.stringify({
type: 'service',
action: 'up',
service: service
}));
}
//broadcat all the services in the db to the client
var serviceStream = db.createValueStream({start: 'service!', end: 'service~'});
serviceStream.on('data',function(service){
client.write(JSON.stringify({
type: 'service',
action: 'up',
service: service
}));
});
}
function checkContains(service) {
// Check to see if we already have it in our services list
if (_.find(services, function(s) {
return s.unique === service.unique;
})) {
return false;

//could go into a module
function key_hash(str) {
var hash;
for (var i=0; i < str.length; i += 1) {
var chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
hash = Math.abs(hash).toString(16);
return hash;
};

function construct_key(service) {
var sufix = key_hash(service.unique);
return 'service!' + sufix;
};

function already_in_db(service, cbmesh) {
// Check to see if the service is already in the db
db.get(construct_key(service), function (err, value) {
var found;
if (err) {
found = false;
} else {
return true;
found = true;
}
}
cbmesh(found, value, err);
});
};

// only allow services of the types we're interested in
function filter(service) {
return service;
Expand All @@ -87,33 +108,41 @@ function createUnique(service) {
var browser = mdns.createBrowser(mdns.makeServiceType('http', 'tcp'));
browser.on('serviceUp', function(service) {
console.log('service coming up: ');
console.log(service);
//console.log(service);

service.unique = createUnique(service);
if(checkContains(service) && filter(service)) {
services.push(service);
broadcast({
type: 'service',
action: 'up',
service: service
//add service to the db
already_in_db(service, function(found) {
if(!found && filter(service)) {
var newkey = construct_key(service);
db.put(newkey, service, function () {
console.log('remembering service ' + newkey);
broadcast({
type: 'service',
action: 'up',
service: service
});
});
}
}
});
});

browser.on('serviceDown', function(service) {
console.log('service coming down: ');
console.log(service);
//console.log(service);
service.unique = createUnique(service);
if(filter(service)) {
broadcast({
type: 'service',
action: 'down',
service: service
});
services = _.filter(services, function(s) {
return service.unique !== s.unique;
});
db.del(construct_key(service), function () {
console.log('forggeting service ' + construct_key(service));
})
}
});

browser.on('error', function (err) {
console.log('mdns error: ' + err);
});
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"description": "A web app for browsing services available on the People's Open Network",
"dependencies": {
"express": "~3.4.8",
"sockjs": "^0.3.9",
"jquery": "~2.1.0",
"level": "^0.18.0",
"lodash": "^2.4.1",
"mdns2": "~2.1.3",
"process": "^0.7.0",
"jquery": "~2.1.0",
"lodash": "^2.4.1"
"sockjs": "^0.3.9",
"tape": "^2.13.4"
},
"repository": {
"type": "git",
Expand Down
Loading