Skip to content

Commit

Permalink
begins to adhere to the proposed contract in connector issue #50
Browse files Browse the repository at this point in the history
  • Loading branch information
barberdt committed Dec 8, 2011
1 parent 1fcbb72 commit 23d7656
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
47 changes: 29 additions & 18 deletions fake-connector/fake-connector.js
Expand Up @@ -4,36 +4,43 @@ resourceID = "d00884c1eb2d2ee6d886da0407028e52fd0ee188";
swarmID = "caa6343db41ce9359c76a4d0b066e34d27e47eb0";

// send functions
var announceConnector = function() {
var payload;
payload = {"status_announcement": "connector"};
console.log("Announcint connector status");
SWARM.send(payload);
};

var sendCapabilities = function(from) {
var payload = {"capabilities": {"feeds": ["Location", "Acceleration"], "modules": {"slot1": "LCD", "slot2": "GPS"}}};
console.log("Sending capabilities to resource: " + from.resource);
SWARM.send(payload, [{"swarm": swarmID, "resource": from.resource}]);
var payload;
payload = {"capabilities": {"feeds": ["Location", "Acceleration"], "modules": {"slot1": "LCD", "slot2": "GPS"}}};
console.log("Sending private capabilities to resource: " + from.resource);
SWARM.send(payload, [{swarm: "caa6343db41ce9359c76a4d0b066e34d27e47eb0", resource: from.resource}]);
};

var sendFeedResponse = function(from, payload) {
console.log("Will send feed response to: " + from.resource);
console.log("Will send feed response with feed: " + payload.feed);
console.log("TODO: Send feed response to: " + from.resource);
};

//conditionals
var isSwarmPresence = function(from) {
if (from.swarm) {
var isConnectorInterest = function(payload) {
if (payload.status_announcement && (payload.status_announcement === "connector-interest")) {
return true;
} else {
return false;
}
}
};

var isSelfPresence = function(from) {
if (from.resource === resourceID) {
var isSwarmPresence = function(from) {
if (from.swarm) {
return true;
} else {
return false;
}
}
};

var isPresenceUnavailable = function(type) {
if (type && (type === "unavailable")) {
var isPublicMessage = function(publicVal) {
if (publicVal === true) {
return true;
} else {
return false;
Expand Down Expand Up @@ -64,7 +71,8 @@ SWARM.connect({apikey: participationKey,
// callbacks
onconnect:
function onConnect() {
console.log("Connected to swarm: " + swarmID);
console.log("Connected to swarm: " + swarmID);
announceConnector();
},
onpresence:
function onPresence(presence) {
Expand All @@ -76,9 +84,6 @@ SWARM.connect({apikey: participationKey,

if (isSwarmPresence(from)) {
console.log("Presence: " + presence);
if (!isSelfPresence(from) && !isPresenceUnavailable(type)) {
sendCapabilities(from);
}
}
},
onmessage:
Expand All @@ -90,8 +95,14 @@ SWARM.connect({apikey: participationKey,
payload = messageObj.message.payload;
publicVal = messageObj.message.public;

if (isPublicMessage(publicVal)) {
if (isConnectorInterest(payload)) {
sendCapabilities(from);
}
}

if (isPrivateMessage(publicVal)) {
console.log("Message: " + message);
console.log("Private Message: " + message);
if (isFeedRequest(payload)) {
sendFeedResponse(from, payload);
}
Expand Down
3 changes: 2 additions & 1 deletion fake-connector/index.html
@@ -1 +1,2 @@
<!DOCTYPE html><html lang="en"><head><title>BUGswarm Connector</title><script src="swarm-v0.3.2.js"></script><script src="fake-connector.js"></script></head><body><h1>BUGswarm Connector</h1></body></html>
<!DOCTYPE html><html lang="en"><head><title>BUGswarm Connector</title><script src="swarm-v0.3.2.js"></script><script src="fake-connector.js"></script></head><body><h1>BUGswarm Connector: The JavaScript Version</h1><p>View your browser's JavaScript console for more information.
</p></body></html>
4 changes: 3 additions & 1 deletion fake-connector/index.jade
Expand Up @@ -7,4 +7,6 @@ html(lang='en')

body

h1 BUGswarm Connector
h1 BUGswarm Connector: The JavaScript Version
p
| View your browser's JavaScript console for more information.

0 comments on commit 23d7656

Please sign in to comment.