Commits

firefly committed 24c22e5

Separated remote module 'server part' from the bot core; started refactoring the code.
There's still much to be done.

Comments (0)

Files changed (3)

 net     = require 'net'
 
 bs      = require './buffered-socket'
+modserv = require './remote-module-server'
 irc     = require './irc'
 {Shell} = require './shell'
 
 			console.log err.stack
 
 #### Setup remote module server #####################################
-uidCounter = 0
-eidCounter = 0
-executions = {}
-
-moduleServer = net.createServer (socket) ->
-	module =
-		uid    : (++uidCounter)
-		socket : new bs.BufferedSocket socket
-
-		toString: -> "[#{@uid}]"
-	
-	send = (obj) ->
-		module.socket.write JSON.stringify obj
-		module.socket.write "\r\n"
-
-	send {
-		type    : 'handshake'
-		version : '0.0.1'
-		uid     : module.uid
-	}
-
-	module.socket.on 'data', (line) ->
-		try
-			data = JSON.parse line
-			console.log "#{module}:#{data.type}:", data
-			
-			switch data.type
-				when 'handshake'
-					console.log "Module #{module} shook hands"
-					console.log "   Module metadata:", data.metadata
-
-				when 'register'
-					console.log "Module #{module} registered command #{data.name}."
-					shell.commands[data.name] = (exec, args...) ->
-						eid = (++eidCounter)
-						executions[eid] = exec
-
-						exec.stdin.on 'data', (data) ->
-							send {
-								type  : 'input'
-								eid   : eid
-								value : data
-							}
-
-						exec.stdin.on 'end', ->
-							send {
-								type : 'input-end'
-								eid  : eid
-							}
-
-						send {
-							type      : 'execute'
-							eid       : eid
-							command   : data.name
-							arguments : args
-						}
-
-					shell.commands[data.name].manualEnd = true
-
-				when 'output'
-					console.log "Module #{module} outputted #{data.value}."
-					executions[data.eid].output data.value
-
-				when 'output-end'
-					console.log "Module #{module} ended output!"
-					executions[data.eid].end()
-
-		catch err
-			console.log "Got invalid data from #{module}:"
-			console.log line
-			send {
-				type  : 'error'
-				value : "Your request couldn't be processed."
-			}
-
-moduleServer.listen 13450
+moduleServer = new modserv.ModuleServer
+	hostname: 'localhost'
+	add: (name, func) ->
+		func.manualEnd = true
+		shell.commands[name] = func
 
 #### Create bot object ##############################################
 bot =

src/client/client.coffee

 		when 'handshake'
 			console.log "Got handshake from server!"
 			send {
-				type    : 'handshake'
-				version : '0.0.1'
+				type     : 'handshake'
+				version  : '0.0.1'
+				metadata : {
+					name    : "Test module"
+					author  : "Jonas Höglund"
+					version : "0.0.1"
+				}
 			}
 
 			for name of commands

src/remote-module-server.coffee

+assert = require 'assert'
+net    = require 'net'
+
+bs     = require './buffered-socket'
+
+#### ModuleServer ###################################################
+ModuleServer = (opts) ->
+	self = this
+	assert.ok opts and opts.hostname and opts.add, "Missing required " +
+			"property in ModuleServer options; either 'hostname' or 'add'."
+	
+	@addCommand = opts.add
+
+	@uidCounter = 0
+	@eidCounter = 0
+	@executions = {}
+	@modules    = {}
+
+	server = net.createServer (socket) ->
+		module =
+			uid    : (++self.uidCounter)
+			socket : new bs.BufferedSocket socket
+
+			send   : (obj) ->
+				@socket.write JSON.stringify obj
+				@socket.write "\r\n"
+
+			toString: -> "[#{@uid}]"
+
+		self.modules[module.uid] = module
+		self.initializeModule module
+
+		module.send {
+			type    : 'handshake'
+			version : '0.0.01'
+			uid     : module.uid
+		}
+
+	server.listen opts.port or 13450
+	this
+
+ModuleServer::initializeModule = (module) ->
+	self = this
+
+	module.socket.on 'data', (line) ->
+		try
+			data = JSON.parse line
+			console.log "#{module}:#{data.type}:", data
+
+			switch data.type
+				when 'handshake'
+					console.log "Module #{module} shook hands"
+					console.log "   Module metadata:", data.metadata
+
+				when 'register'
+					console.log "Module #{module} registered command #{data.name}."
+
+					self.addCommand data.name, (exec, args...) ->
+						eid = (++self.eidCounter)
+						self.executions[eid] = exec
+
+						exec.stdin.on 'data', (data) ->
+							module.send {
+								type  : 'input'
+								eid   : eid
+								value : data
+							}
+
+						exec.stdin.on 'end', ->
+							module.send {
+								type : 'input-end'
+								eid  : eid
+							}
+
+						module.send {
+							type      : 'execute'
+							eid       : eid
+							command   : data.name
+							arguments : args
+						}
+
+				when 'output'
+					console.log "Module #{module} outputted #{data.value}."
+					self.executions[data.eid].output data.value
+
+				when 'output-end'
+					console.log "Module #{module} ended output!"
+					self.executions[data.eid].end()
+
+		catch err
+			console.log "Got invalid data from #{module}:"
+			console.log line
+			module.send {
+				type  : 'error'
+				value : "Your request couldn't be processed."
+			}
+
+#### exports ########################################################
+exports.ModuleServer = ModuleServer
+