 ###
 
 parser = require('body-parser')
-
 Channel = require('./sse').Channel
 
 if typeof Promise is 'undefined' then Promise = require './promise'

#
# Server Side
#

log = require './log'
sessionTimeOut = 30 * 60 * 1000 # 30 minutes
tag = 'rpc'


  _return: (msg, rep, res) ->
    log "#{msg.id} out", rep
    res.send rep # sends response to client
    
#
# Class Server
#
class classServer # for Http POST
  constructor: (classes, @timeOut = sessionTimeOut) -> # list of classes that the server can instantiate
    @[Class] = Class:classes[Class], sessions:[] for Class of classes

  process: (Class, msg, res) ->
    uid = msg.id.split('-')[0]
    if rpc = @[Class].sessions[uid] then clearTimeout rpc.timeOut
    else # new session / new object
      # @[Class].date = new Date()
      @[Class].sessions[uid] = rpc = new Rpc new @[Class].Class()
      log "adding new session #{Class} #{uid} (total: #{Object.keys(@[Class]).length})"
    rpc.timeOut = setTimeout =>
      delete @[Class].sessions[uid]
      log "removing session #{uid} (total: #{Object.keys(@[Class]).length})"
    , @timeOut
    
    rpc.process msg, res

#
# Class expressRpc: dispatch incoming HTTP requests / class
