Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mglazer committed May 9, 2011
0 parents commit 4592372
Show file tree
Hide file tree
Showing 13 changed files with 1,748 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.un~
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
gem "sinatra"
gem "zmq"
gem "mongo"
gem "json"
gem "bson"
gem "haml"
gem "em-websocket"

30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
GEM
specs:
addressable (2.2.5)
bson (1.3.0)
em-websocket (0.3.0)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (0.12.10)
haml (3.1.1)
json (1.5.1)
mongo (1.3.0)
bson (>= 1.3.0)
rack (1.2.2)
sinatra (1.2.6)
rack (~> 1.1)
tilt (>= 1.2.2, < 2.0)
tilt (1.3)
zmq (2.1.0.1)

PLATFORMS
ruby

DEPENDENCIES
bson
em-websocket
haml
json
mongo
sinatra
zmq
77 changes: 77 additions & 0 deletions PixelDisplay/pixel-display.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env ruby
#
#


require 'rubygems'
require 'sinatra'
require 'mongo'
require 'bson'
require 'json'
require 'haml'

require 'em-websocket'
require 'zmq'


connection = Mongo::Connection.new
db = connection['pixel-logger-test']
collection = db['pixel-logs']

if collection.nil?
puts "Could not make connection to [pixel-logs] in [pixel-logger-test]"
exit 1
end

emfork = Process.fork do

LOG_PREFIX = "LOG "
def strip_prefix( msg )
puts "Sending #{msg}"
msg[LOG_PREFIX.size..-1]
end

@sockets = []


EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 4569) do |ws|
ws.onopen do
puts "Opening web socket"
@sockets << ws
zmq = ZMQ::Context.new(1)
@socket = zmq.socket(ZMQ::SUB)
@socket.connect( "tcp://localhost:5555" )
@socket.setsockopt( ZMQ::SUBSCRIBE, LOG_PREFIX )

puts "Finished opening web socket"
end

ws.onmessage do |mess|
while (!( msg = @socket.recv( ZMQ::NOBLOCK ) ).nil? )
ws.send( strip_prefix( msg ) )
end
end

ws.onclose do
@socket.close
@sockets.delete @socket
end
end
end


get '/' do
haml :index
end


get '/pixels' do
[201,
{'Content-Type' => 'application/json'},
collection.find.map { |log| log }.to_json]
end





11 changes: 11 additions & 0 deletions PixelDisplay/public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.pixel-dot {
position: absolute;
background-color: #CC0066;
border-radius: 3px;
width: 3px;
height: 3px;
}

#globalmap {
position: relative;
}
Binary file added PixelDisplay/public/images/globalmap.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions PixelDisplay/public/js/jquery.min.js

Large diffs are not rendered by default.

84 changes: 84 additions & 0 deletions PixelDisplay/public/js/pixel-display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function populateDataPoints(data) {
for ( var i = 0, length = data.length; i < length; ++i ) {
var current = data[i];
$("#logbody").append("<tr><td>" + current['time'] + "</td><td>" + current['ip'] + "</td><td>" + current['user_agent'] + "</td><td><span class='longitude'>" + current['coordinates'][0] + "</span>,<span class='latitude'>" + current['coordinates'][1] + "</span></td></tr>");
$("#globalmap").addPoint(current['coordinates']);

};
}

(function( $ ) {
$.fn.addPoint = function(point) {
var lon = point[0];
var lat = point[1];

var theNewPoint = newPoint(lon, lat);
this.append(theNewPoint);

theNewPoint.animate({
opacity: 1,
height: '6px',
width: '6px',
'background-color': '#6699FF'
}, 1500)
.animate({
height: '3px',
width: '3px',
'background-color': '#CC0033'
}, 1500);
};

function newPoint(lon, lat) {
console.log("Adding: " + lon + " " + lat);
return $("<span class=\"pixel-dot\">&nbsp;</span>")
.css({left: translateLon(lon), top: translateLat(lat), opacity: 0.25});
}

function translateLon(lon) {
return (parseFloat(lon) + 150.);
}

function translateLat(lat) {
return (parseFloat(lat) + 25.);
}

})( jQuery );


function checkWebSocket(ws) {
ws.send("CHECK");
setTimeout(function() {
checkWebSocket(ws);
}, 1500);
}

$(document).ready(function() {
$.get('/pixels', function(data) {
$("#logbody").empty()
populateDataPoints(data);
});

if ( !("WebSocket" in window) ) {
alert("Sorry, your browser doesn't support WebSockets");
return;
}

var ws = new WebSocket("ws://localhost:4569");

ws.onopen = function() {
console.log("Web socket has been opened");
}

ws.onmessage = function(evt) {
console.log("Received event: ");
console.log(evt);
var data = JSON.parse( evt.data );

console.log(data);
populateDataPoints([data]);
}

setTimeout(function() {
checkWebSocket(ws);
}, 500);
});
24 changes: 24 additions & 0 deletions PixelDisplay/views/index.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%script{:type => "text/javascript",
:src => "/js/jquery.min.js"}
%script{:type => "text/javascript",
:src => "/js/pixel-display.js"}
%link{:rel => "stylesheet", :type=>"text/css", :href=>"/css/style.css"}


%h1 Pixel Logs

#globalmap
%img{:src => '/images/globalmap.jpg'}


%table#logs
%thead
%tr
%th Timestamp
%th IP
%th User Agent
%th Position (lon,lat)
%tbody#logbody



88 changes: 88 additions & 0 deletions PixelLogger/pixel-logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env ruby
#
#


require 'rubygems'
require 'mongo'
require 'bson'
require 'zmq'
require 'json'


LOG_PREFIX = "LOG "


class MongoLogger

def initialize
@connection = Mongo::Connection.new
@db = @connection['pixel-logger-test']
@collection = @db['pixel-logs']
ensure_indicies
end

def log(pixel)
puts "Logging: #{pixel}"
@collection.insert(JSON.parse(pixel))
end

private

def ensure_indicies
@collection.ensure_index( [['coordinates', Mongo::GEO2D]] )
end

end


class ZMQLogProcessor

def initialize(logger)
@logger = logger
@zmq = ZMQ::Context.new
@socket = @zmq.socket(ZMQ::SUB)
@socket.connect( "tcp://localhost:5555" )
@socket.setsockopt( ZMQ::SUBSCRIBE, LOG_PREFIX )
puts "Connected to [tcp://localhost:5555]"
@continue = true
end

def receive
while @continue
@logger.log(strip_prefix(@socket.recv))
end
end

def strip_prefix(msg)
msg[LOG_PREFIX.size..-1]
end

def stop
@continue = false
end
end


logger = MongoLogger.new
log_processor = ZMQLogProcessor.new(logger)

puts "Starting request handler thread"
t1 = Thread.new do
log_processor.receive
end

Signal.trap("QUIT") do
puts "Stopping request handler"
log_processor.stop
end

Signal.trap("TERM") do
puts "Stopping request handler"
log_processor.stop
end

while true
sleep(1)
end

Loading

0 comments on commit 4592372

Please sign in to comment.