Permalink
Browse files

Update for latest luvit API

  • Loading branch information...
1 parent a64ac73 commit 5c876b85476e696eae8d44b9dc2e1152812c8175 @creationix committed Feb 9, 2012
Showing with 58 additions and 57 deletions.
  1. +9 −10 joyclient.lua
  2. +19 −16 joystick.lua
  3. +27 −28 server.lua
  4. +3 −3 static.lua
View
@@ -1,8 +1,7 @@
-local Joystick = require('./joystick')
-local JsonStream = require('./jsonstream')
-local Json = require('json')
-local Emitter = require('emitter')
-local TCP = require('tcp')
+local Joystick = require('./joystick').Joystick
+local JSON = require('json')
+local Emitter = require('core').Emitter
+local Tcp = require('tcp').Tcp
if not process.argv[1] then
print("Please pass in joystick number as first argument")
@@ -12,17 +11,17 @@ local host = process.argv[2] or "0.0.0.0"
local port = process.argv[3] or 5000
local function connect(host, port, callback)
- local client = TCP:new()
+ local client = Tcp:new()
client:connect(host, port)
client:on("error", callback)
client:on("connect", function ()
- client:remove_listener("error", callback)
- client:read_start()
+ client:removeListener("error", callback)
+ client:readStart()
local emitter = Emitter:new()
function emitter.send(message)
- client:write(Json.stringify(message))
+ client:write(JSON.stringify(message))
end
- local parser = JsonStream(function (value)
+ local parser = JSON.streamingParser(function (value)
emitter:emit("message", value)
end)
client:on('data', function (chunk)
View
@@ -1,16 +1,18 @@
local Bit = require('bit')
local FS = require('fs')
-local Emitter = require('emitter')
-local Buffer = require('buffer')
+local Emitter = require('core').Emitter
+local Buffer = require('buffer').Buffer
+
+local JOYSTICK = {}
-- http://www.mjmwired.net/kernel/Documentation/input/joystick-api.txt
function parse(buffer)
local event = {
time = buffer:readUInt32LE(1),
- number = buffer:readUInt8(8),
value = buffer:readInt16LE(5),
+ number = buffer[8],
}
- local type = buffer:readUInt8(7)
+ local type = buffer[7]
if Bit.band(type, 0x80) > 0 then event.init = true end
if Bit.band(type, 0x01) > 0 then event.type = "button" end
if Bit.band(type, 0x02) > 0 then event.type = "axis" end
@@ -19,35 +21,36 @@ end
-- Expose as a nice Lua API
local Joystick = Emitter:extend()
+JOYSTICK.Joystick = Joystick
-function Joystick.prototype:initialize(id)
- self:wrap("on_open")
- self:wrap("on_read")
+function Joystick:initialize(id)
+ self:wrap("onOpen")
+ self:wrap("onRead")
self.id = id
- FS.open("/dev/input/js" .. id, "r", "0644", self.on_open)
+ FS.open("/dev/input/js" .. id, "r", "0644", self.onOpen)
end
-function Joystick.prototype:on_open(fd)
+function Joystick:onOpen(fd)
self.fd = fd
self:emit("opened")
- self:start_read()
+ self:startRead()
end
-function Joystick.prototype:start_read()
- FS.read(self.fd, nil, 8, self.on_read)
+function Joystick:startRead()
+ FS.read(self.fd, nil, 8, self.onRead)
end
-function Joystick.prototype:on_read(chunk)
+function Joystick:onRead(chunk)
local event = parse(Buffer:new(chunk))
self:emit(event.type, event)
- if self.fd then self:start_read() end
+ if self.fd then self:startRead() end
end
-function Joystick.prototype:close(callback)
+function Joystick:close(callback)
local fd = self.fd
self.fd = nil
FS.close(fd, callback)
end
-return Joystick
+return JOYSTICK
View
@@ -1,18 +1,17 @@
-local Tcp = require('tcp')
-local Table = require('table')
-local Http = require('http')
-local Stack = require('stack')
-local Emitter = require('emitter')
-local JsonStream = require('./jsonstream')
-local Json = require('json')
-local Path = require('path')
-local Url = require('url')
-local QueryString = require('querystring')
+local NET = require('net')
+local TABLE = require('table')
+local HTTP = require('http')
+local STACK = require('stack')
+local Emitter = require('core').Emitter
+local JSON = require('json')
+local PATH = require('path')
+local URL = require('url')
+local QUERY_STRING = require('querystring')
local history = {}
local pending = {}
-local function flush_pending()
+local function flushPending()
local callbacks = pending
pending = {}
for i, callback in ipairs(callbacks) do
@@ -21,15 +20,15 @@ local function flush_pending()
end
-- An HTTP server for browsers to watch
-Http.create_server("0.0.0.0", 8080, Stack.stack(
+HTTP.createServer("0.0.0.0", 8080, STACK.stack(
-- Long Poll connection
function (req, res, next)
if not (req.method == "GET") then return next() end
- req.uri = req.uri or Url.parse(req.url)
+ req.uri = req.uri or URL.parse(req.url)
if not (req.uri.pathname == "/listen") then return next() end
local since = 0;
if req.headers.cookie then
- since = tonumber(QueryString.parse(req.headers.cookie).since)
+ since = tonumber(QUERY_STRING.parse(req.headers.cookie).since)
end
if (not since) and #history > 0 then
since = history[#history].time
@@ -38,55 +37,55 @@ Http.create_server("0.0.0.0", 8080, Stack.stack(
local new = {}
for i, v in ipairs(history) do
if v.time > since then
- Table.insert(new, v.message)
+ TABLE.insert(new, v.message)
end
end
return new
end
local function respond()
- local json = Json.stringify(filter()) .."\n"
- res:write_head(200, {
+ local json = JSON.stringify(filter()) .."\n"
+ res:writeHead(200, {
["Set-Cookie"] = "since="..history[#history].time,
["Content-Type"] = "application/json",
["Content-Length"] = #json
})
res:finish(json)
end
- Table.insert(pending, respond)
+ TABLE.insert(pending, respond)
end,
-- Serve static resources
- require('./static')(Path.join(__dirname, "ui"), "index.html")
+ require('./static')(PATH.join(__dirname, "ui"), "index.html")
))
print("Http server listening at http://localhost:8080/")
-- A server for joystick clients to connect to
-Tcp:create_server("0.0.0.0", 5000, function (socket)
+NET.createServer(function (socket)
local client = Emitter:new()
function client.send(message)
- socket:write(Json.stringify(message))
+ socket:write(JSON.stringify(message))
end
- local parser = JsonStream(function (message)
+ local parser = JSON.streamingParser(function (message)
client:emit('message', message)
- end)
+ end, {allow_multiple_values=true})
socket:on('data', function (chunk)
parser:parse(chunk)
end)
- new_player(client)
-end)
+ newPlayer(client)
+end):listen(5000, "0.0.0.0")
local clients = {}
-function new_player(client)
+function newPlayer(client)
local id = #clients + 1
clients[id] = client
client.send({welcome=id})
client:on('message', function (message)
local time = message.time
message.time = nil
message.id = id
- Table.insert(history, {time=time,message=message})
+ TABLE.insert(history, {time=time,message=message})
p(message)
- flush_pending()
+ flushPending()
end)
end
View
@@ -22,14 +22,14 @@ return function (root, index)
res:finish()
return
end
- local stream = FS.create_read_stream(path);
+ local stream = FS.createReadStream(path);
stream:on('error', next)
local sent
local function header()
if sent then return end
sent = true
- res:write_head(200, {
- ["Content-Type"] = MIME.get_type(path),
+ res:writeHead(200, {
+ ["Content-Type"] = MIME.getType(path),
["Content-Length"] = stat.size
})
end

0 comments on commit 5c876b8

Please sign in to comment.