messed everything up .... :( 111

This commit is contained in:
logsol 2012-07-28 21:09:39 +02:00
parent 03678eb773
commit ec1bceb1b6
13 changed files with 145 additions and 117 deletions

View file

@ -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;
});

View file

@ -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;
});

View file

@ -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;
});

View file

@ -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.";

View file

@ -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;
});

48
app/Game/Server/PipeToLobby.js Executable file
View file

@ -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;
});

View file

@ -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;

View file

@ -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;

38
app/Lobby/PipeToChannel.js Executable file
View file

@ -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;
});

View file

@ -10,6 +10,7 @@ function (Parent, ProtocolHelper) {
this.coordinator = coordinator;
this.channelProcess = null;
this.socketLink = socketLink;
var self = this;