diff --git a/app/Bootstrap/Channel.js b/app/Bootstrap/Channel.js deleted file mode 100755 index 801e57c..0000000 --- a/app/Bootstrap/Channel.js +++ /dev/null @@ -1,34 +0,0 @@ -define([ - "Game/Server/Channel", - "Game/Server/CoordinatorLink" -], - -function (Channel, CoordinatorLink) { - - function ChannelBootstrap (process) { - - var coordinatorLink = new CoordinatorLink(process); - var channel = null; - - process.on('message', function (message) { - - switch(message) { - case 'CREATE': - channel = new Channel(coordinatorLink); - break; - - case 'KILL': - channel.destroy(); - process.exit(0); - break; - - default: - coordinatorLink.receive(message); - break; - } - }); - } - - return ChannelBootstrap; - -}); \ No newline at end of file diff --git a/app/Bootstrap/Client.js b/app/Bootstrap/Client.js deleted file mode 100755 index 8d11dac..0000000 --- a/app/Bootstrap/Client.js +++ /dev/null @@ -1,15 +0,0 @@ -define([ - "Game/Client/Networker", - "Lib/Vendor/SocketIO" -], - -function (Networker, SocketIO) { - - function Client (location, options) { - this.socket = SocketIO.connect(location, options); - this.networker = new Networker(this.socket); - } - - return Client; - -}); \ No newline at end of file diff --git a/app/Bootstrap/Server.js b/app/Bootstrap/Server.js deleted file mode 100755 index 2aa5dc7..0000000 --- a/app/Bootstrap/Server.js +++ /dev/null @@ -1,16 +0,0 @@ -define([ - "Bootstrap/HttpServer", - "Bootstrap/Socket", - "Lobby/Coordinator" -], - -function (HttpServer, Socket, Coordinator) { - - function Server (options) { - coordinator = new Coordinator(); - httpServer = new HttpServer(options); - this.socket = new Socket(httpServer.getServer(), options, coordinator); - } - - return Server; -}); \ No newline at end of file diff --git a/app/Game/Core/NotificationCenter.js b/app/Game/Core/NotificationCenter.js index 9c824e7..2747c2a 100755 --- a/app/Game/Core/NotificationCenter.js +++ b/app/Game/Core/NotificationCenter.js @@ -8,7 +8,7 @@ function () { this.subUid = -1; } - NotificationCenter.prototype.trigger = function (topic) { + NotificationCenter.prototype.trigger = function (topic /*, arguments*/) { if (!this.topics[topic]) { throw "No such topic " + topic + ". Could not trigger."; diff --git a/app/Game/Server/CoordinatorLink.js b/app/Game/Server/CoordinatorLink.js deleted file mode 100755 index 5287849..0000000 --- a/app/Game/Server/CoordinatorLink.js +++ /dev/null @@ -1,20 +0,0 @@ -define([ -], - -function () { - - function CoordinatorLink (process) { - this.process = process; - } - - CoordinatorLink.prototype.send = function (message) { - this.process.send(message); - }; - - CoordinatorLink.prototype.receive = function (message) { - throw 'This method is abstract and must be overwritten by Channel'; - }; - - return CoordinatorLink; - -}); \ No newline at end of file diff --git a/app/Game/Server/PipeToLobby.js b/app/Game/Server/PipeToLobby.js new file mode 100755 index 0000000..a28d97f --- /dev/null +++ b/app/Game/Server/PipeToLobby.js @@ -0,0 +1,48 @@ +define([ + "Game/Core/NotficationCenter" +], + +function (NotficationCenter) { + + function PipeToLobby (process) { + + this.channel = null; + this.process = process; + + NotficationCenter.on('net/send', this.send, this); + + process.on('message', function (message, handle) { + + switch(message) { + case 'CREATE': + this.channel = new Channel(this); + break; + + case 'KILL': + this.channel.destroy(); + process.exit(0); + break; + + default: + this.onMessage(message); + break; + } + }); + } + + PipeToLobby.prototype.send = function (recipient, data) { + var message = { + recipient: recipient, + data: data + } + + this.process.send(message); + }; + + PipeToLobby.prototype.onMessage = function (message) { + NotficationCenter.trigger(message.recipient + '/message', message.data); + } + + return PipeToLobby; + +}); \ No newline at end of file diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index b24cd41..1261f34 100755 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -6,8 +6,8 @@ define([ function (Parent, ProtocolHelper, NotificationCenter) { - function User (id, coordinator) { - Parent.call(this, id); + function User (socketLink, coordinator) { + Parent.call(this, socketLink.id); this.id = socketLink.id; this.socketLink = socketLink; this.coordinator = coordinator; diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js index a85f897..9ab30bc 100755 --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -1,10 +1,11 @@ define([ "Lobby/User", "Game/Server/Channel", + "Lobby/PipeToChannel" "child_process" ], -function (User, Channel, childProcess) { +function (User, Channel, PipeToChannel, childProcess) { var fork = childProcess.fork; @@ -38,22 +39,22 @@ function (User, Channel, childProcess) { var channel = this.channels[channelName]; if(!channel) { - - try { - channel = fork('channel.js'); - channel.send('CREATE'); - - channel.send({ - channel: { - setName: channelName - } - }); - - } catch (err) { - throw 'Failed to fork channel ' + channelName + '! (' + err + ')'; - } - + channel = new PipeToChannel(channelName); this.channels[channelName] = channel; + + NotificationCenter.on('channel/' + channelName + '/message', function (data) { + channel.send('channel', data); + }, this); + + NotificationCenter.on('user/joined', function (user) { + NotificationCenter.on('channel/' + channelName + '/user/' + user.id, function (recipient, data) { + channel.send(recipient, data); + }, this); + }, this); + + NotificationCenter.on('user/left', function (user) { + + }, this); } //channel.addUser(user); @@ -63,10 +64,11 @@ function (User, Channel, childProcess) { } Coordinator.prototype.removeUser = function (user) { + + user.channel.send('user/' + user.id + '/left'); + NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id); + delete this.lobbyUsers[user.id]; - if(user.channelProcess) { - //user.channel.releaseUser(user); -> generate message - } } return Coordinator; diff --git a/app/Lobby/PipeToChannel.js b/app/Lobby/PipeToChannel.js new file mode 100755 index 0000000..bf8f54b --- /dev/null +++ b/app/Lobby/PipeToChannel.js @@ -0,0 +1,38 @@ +define([ + "Game/Core/NotificationCenter" +], + +function (NotificationCenter) { + + function PipeToChannel (channelName) { + + this.channelProcess = null; + + try { + this.channelProcess = fork('channel.js'); + } catch (err) { + throw 'Failed to fork channel! (' + err + ')'; + } + + this.send('channel/' + channelName, 'CREATE'); + this.channelProcess.on('message', this.onMessage.bind(this)); + + var self = this; + } + + PipeToChannel.prototype.send = function (recipient, data) { + var message = { + recipient: recipient, + data: data + } + + this.channelProcess.send(message); + } + + PipeToChannel.prototype.onMessage = function (message) { + NotficationCenter.trigger(message.recipient + '/message', message.data); + } + + return PipeToChannel; + +}); \ No newline at end of file diff --git a/app/Lobby/User.js b/app/Lobby/User.js index f982ccf..ab9e891 100755 --- a/app/Lobby/User.js +++ b/app/Lobby/User.js @@ -10,6 +10,7 @@ function (Parent, ProtocolHelper) { this.coordinator = coordinator; this.channelProcess = null; + this.socketLink = socketLink; var self = this; diff --git a/channel.js b/channel.js index 4ac7e95..f4102ce 100755 --- a/channel.js +++ b/channel.js @@ -6,8 +6,12 @@ requirejs.config({ var inspector = {}; -requirejs(["Bootstrap/Channel"], function (ChannelBootstrap) { +requirejs([ + "Game/Server/LobbyPipe" +], + +function (LobbyPipe) { + var lobbyPipe = new LobbyPipe(process); - var channelBootstrap = new ChannelBootstrap(process); - inspector.channelBootstrap = channelBootstrap; + inspector.lobbyPipe = lobbyPipe; }); \ No newline at end of file diff --git a/client.js b/client.js index c4f1eea..f662051 100755 --- a/client.js +++ b/client.js @@ -4,7 +4,12 @@ requirejs.config({ var inspector = {}; -requirejs(["Bootstrap/Client"], function (Client) { +requirejs([ + "Game/Client/Networker", + "Lib/Vendor/SocketIO" +], + +function (Networker, SocketIO) { var options = { "reconnect": false, @@ -17,6 +22,8 @@ requirejs(["Bootstrap/Client"], function (Client) { ], }; - var client = new Client(location.href, options); - inspector.client = client; + var socket = SocketIO.connect(location.href, options); + var networker = new Networker(socket); + + inspector.networker = networker; }); \ No newline at end of file diff --git a/server.js b/server.js index 5d8a4f9..59a4c09 100755 --- a/server.js +++ b/server.js @@ -1,6 +1,6 @@ var requirejs = require('requirejs'); -var inspector = {}; +var inspector; requirejs.config({ nodeRequire: require, @@ -18,9 +18,22 @@ var options = { logLevel: process.argv[3] || 0 }; -requirejs(["Bootstrap/Server"], function (Server) { - var server = new Server(options); - inspector.server = server; +requirejs([ + "Bootstrap/HttpServer", + "Bootstrap/Socket", + "Lobby/Coordinator" +], + +function (HttpServer, Socket, Coordinator) { + var coordinator = new Coordinator(); + var httpServer = new HttpServer(options); + var socket = new Socket(httpServer.getServer(), options, coordinator); + + inspector = { + coordinator: coordinator, + httpServer: httpServer, + socket: socket + } }); exports = module.exports = inspector; \ No newline at end of file