Browse Source

initial commit

master
mortie 8 years ago
commit
bdacbf2f0f
2 changed files with 88 additions and 0 deletions
  1. 59
    0
      client/client.sh
  2. 29
    0
      server/server.js

+ 59
- 0
client/client.sh View File

@@ -0,0 +1,59 @@
#!/bin/sh

REMOTE='http://localhost:8080'
INTERVAL=1

requirecmd()
{
which "$1" 2>1 > /dev/null
if [ "$?" -ne 0 ]; then
echo "Missing command: $1"
exit 1
fi
}

requirecmd curl
requirecmd fbv
requirecmd shasum

calchash()
{
shasum --algorithm 1 "$1" 2>/dev/null | cut -d ' ' -f 1
}

PICFILE="pic"
PICHASH=$(calchash "$PICFILE")
PROGPID=$$

# Loop responsible for requesting to the server
# and updating the picture if necessary
requestloop()
{
while :; do
URL="$REMOTE/$PICHASH"
curl --silent "$URL" > refresh

# Empty response means the file hasn't changed
if [ $(file refresh | cut -d ' ' -f 2) = "empty" ]; then
rm refresh

# If the response is non-empty, we received an image file.
# We move that file to $PICFILE, then display it
else
mv refresh "$PICFILE"
PICHASH=$(calchash "$PICFILE")
pkill --parent "$PROGPID" fbv
fi
sleep "$INTERVAL"
done
}

requestloop &

while :; do
if [ -f "$PICFILE" ]; then
fbv --noclear --noinfo "$PICFILE"
else
sleep 2;
fi
done

+ 29
- 0
server/server.js View File

@@ -0,0 +1,29 @@
var fs = require("fs");
var http = require("http");
var crypto = require("crypto");

function Pic(path) {
var content = fs.readFileSync(path);
var hash = crypto.createHash("sha1").update(content).digest("hex");

var self = {
path: path,
hash: hash,
content: content
}

return self;
}

var currentPic = Pic("/home/martin/background.jpg");

http.createServer((req, res) => {
var hash = req.url.substring(1); // Remove the first / to get only hash
console.log("got hash "+hash+", existing hash is "+currentPic.hash);

if (hash === currentPic.hash) {
res.end();
} else {
res.end(currentPic.content);
}
}).listen(8080);

Loading…
Cancel
Save