var app, connectedSockets, file, http, httpPort, inPort, inSocket, io, log, nlogger, outPort, outSocket, socketio, static, staticPath, titles, util, zmq;
http = require('http');
zmq = require('zmq');
socketio = require('socket.io');
static = require('node-static');
nlogger = require('nlogger');
util = require('util');
log = nlogger.logger(module);
inPort = 5555;
outPort = 5556;
httpPort = 8080;
staticPath = '.';
connectedSockets = {};
inSocket = zmq.createSocket('push');
inSocket.bind("tcp://127.0.0.1:" + inPort, function(e) {});
outSocket = zmq.createSocket('pull');
outSocket.bind("tcp://127.0.0.1:" + outPort, function(e) {
  return outSocket.on('message', function(m) {
    var id, title, _results;
    title = m.toString('utf-8');
    log.debug('title: {}', title);
    _results = [];
    for (id in connectedSockets) {
      _results.push(connectedSockets[id].send(title));
    }
    return _results;
  });
});
titles = {
  '/1': 'Alfa Romeo Alfetta',
  '/2': 'Alfa Romeo "Nuova" Giulietta',
  '/3': 'Alfa Romeo 33',
  '/4': 'Alfa Romeo 75',
  '/5': 'Alfa Romeo 164',
  '/6': 'Alfa Romeo 155',
  '/7': 'Alfa Romeo 156',
  '/8': 'Alfa Romeo 166',
  '/9': 'Alfa Romeo 159'
};
file = new static.Server(staticPath);
app = http.createServer(function(request, response) {
  return request.addListener('end', function() {
    var t;
    if (request.url in titles) {
      t = titles[request.url];
      response.writeHead(200, {
        'Content-Type': 'text/html'
      });
      return response.end("<html><head><title>" + t + "</title></head><body>" + t + "</body></html>");
    } else {
      return file.serve(request, response);
    }
  });
});
app.listen(httpPort);
io = socketio.listen(app);
io.configure(function() {
  io.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']);
  io.set('browser client', true);
  io.set('log level', 1);
  return io.set('heartbeats', true);
});
io.sockets.on('connection', function(s) {
  log.debug('Connect: {} {}', s.id, s.manager.transports[s.id].name);
  connectedSockets[s.id] = s;
  s.on('message', function(url) {
    log.debug('Got url: {}', url);
    return inSocket.send(url);
  });
  return s.on('disconnect', function() {
    return delete connectedSockets[s.id];
  });
});