-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4592372
Showing
13 changed files
with
1,748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.un~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"> </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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.