Permalink
Browse files

Cleanup of tests; Started on Chunked Message Parser

  • Loading branch information...
1 parent 38de58e commit 332f664dbddaa8b4f05360fda7c67e4e893c13de @Havvy Havvy committed Mar 12, 2013
View
@@ -30,6 +30,9 @@
* - once(events: string, listener: function): undefined
*/
+/*
+*/
+
var BiEventSubscriber = function (primary, secondary) {
this._primary = primary;
this._secondary = secondary;
@@ -0,0 +1,24 @@
+var util = require('util');
+var events = require('events');
+
+var ChunkedMessageParser = function (messageParser) {
+ if (messageParser) {
+ this.listen(messageParser);
+ }
+};
+
+ChunkedMessageParser.prototype = new events.EventEmitter();
+
+ChunkedMessageParser.prototype.parse = function (message) {
+ this.defaultAction(message);
+};
+
+ChunkedMessageParser.prototype.listen = function (messageParser) {
+ messageParser.on('_message', this.parse.bind(this));
+};
+
+ChunkedMessageParser.prototype.defaultAction = function (message) {
+ this.emit(message.name, message);
+};
+
+module.exports = ChunkedMessageParser;
View
@@ -9,7 +9,7 @@ var index = {
Socket : 'socket',
OutputSocket : 'output-socket',
MessageParser : 'irc-message-emitter',
- // ChunkedMessageParser: 'chunked-message-parser',
+ ChunkedMessageParser: 'chunked-message-parser',
CommandParser : 'commander',
Modules : 'modules',
Bisubscriber : 'bisubscriber'
View
@@ -7,13 +7,16 @@
* 005
*
* The constructor takes two arguments:
- * 1. socket - Implementing the IrcSocket interface.
- * 2. receiver - The nominal receiver of the object.
+ * 1. receiver - The nominal receiver of the object.
+ * 2. socket - EventEmitter emitting ('data', RFC1459Message) <Optional>
+ *
+ * If you do not attach a socket to it, you can manually feed messages with
+ * the parse method.
+ *
+ * You can add multiple input sockets with the listen method.
*/
/*
-This is very close to a pure actor.
-Just need class syntax and fat arrow syntax and this will look beautiful.
*/
var events = require('events');
@@ -23,16 +26,24 @@ var Message = require('./structures/message');
var MessageParser = function (receiver, socket) {
this.receiver = receiver;
- socket.on('data', this._onData.bind(this));
+
+ if (socket) {
+ this.listen(socket);
+ }
};
MessageParser.prototype = new events.EventEmitter();
MessageParser.prototype.constructor = MessageParser;
-MessageParser.prototype._onData = function (raw) {
+MessageParser.prototype.parse = function (raw) {
var message = Object.freeze(new Message(raw, this.receiver));
this.emit(message.name, message);
this.emit("_message", message);
+ return message;
+};
+
+MessageParser.prototype.listen = function (socket) {
+ socket.on('data', this.parse.bind(this));
};
MessageParser.prototype.toString = function () {
View
@@ -21,7 +21,7 @@ var defaultFactoryConfiguration = {
'IrcSocket' : require('./socket'),
'IrcOutputSocket' : require('./output-socket'),
'MessageParser' : require('./message-parser'),
- // 'ChunkedMessageParser' : require('./chunked-message-parser'),
+ 'ChunkedMessageParser' : require('./chunked-message-parser'),
'CommandParser' : require('./command-parser'),
'Modules' : require('./modules'),
'BiSubscriber' : require('./bisubscriber')
View
@@ -1,54 +1,33 @@
/**
- * A hostmask is the combination of a nickname, a username, and a host. Every IRC user
- * has one that can be obtained from the WHOIS message.
+ * A hostmask is the combination of a nickname, a username, and a host.
+ * Every IRC user has one that can be obtained from the USERHOST message.
*
- * A hostmask's value properties (nick, user, and host) are guaranteed to be final.
+ * This object is frozen upon creation.
*
- * @author havvy
- * @param {String} asString String representation of a hostmask. nick!user@host
+ * Constructor: Takes one argument, a hostmask. For example "nick!user@host";
*/
-nullHostmask = {
- nick : '',
- user : '',
- host : ''
-};
+/*
+*/
var Hostmask = function (asString) {
- if (!asString) {
- return nullHostmask;
- }
-
- var nickEnd = asString.indexOf("!");
- var userEnd = asString.indexOf("@");
-
- var nick = asString.slice(0, nickEnd);
- var user = asString.slice(nickEnd + 1, userEnd);
- var host = asString.slice(userEnd + 1);
-
- Object.defineProperties(this, {
- "nick" : {
- "get" : function () { return nick; }
- },
- "user" : {
- "get" : function () { return user; }
- },
- "host" : {
- "get" : function () { return host; }
- }
- });
+ var nickEnd = asString.indexOf("!");
+ var userEnd = asString.indexOf("@");
+
+ this.nick = asString.slice(0, nickEnd);
+ this.user = asString.slice(nickEnd + 1, userEnd);
+ this.host = asString.slice(userEnd + 1);
+
+ Object.freeze(this);
+};
+
+Hostmask.prototype.equals = function (that) {
+ return this.nick === that.nick && this.user === that.user &&
+ this.host === that.host;
};
-Hostmask.prototype = {
- equals : function (other) {
- return this.nick === other.nick && this.user === other.user &&
- this.host === other.host;
- },
-
- toString : function () {
- return [this.nick, '!', this.user, '@', this.host].join('');
- }
+Hostmask.prototype.toString = function () {
+ return this.nick + '!' + this.user + '@' + this.host;
};
-nullHostmask.prototype = Hostmask;
module.exports = Hostmask;
Oops, something went wrong.

0 comments on commit 332f664

Please sign in to comment.