 
 #### CallServer Example
 #
-#     server = new esl.CallServer()
+#     server = esl.createCallServer()
 #
 #     server.on 'CONNECT', (req,res) ->
 #       # Start processing the call

#     server.on 'CHANNEL_HANGUP_COMPLETE', (req,res) ->
#       # Call was disconnected.
#
#     server.listen port
#

#### Headers parser

# [prepaid code](http://stephane.shimaore.net/git/?p=ccnq3.git;a=blob;f=applications/prepaid/)
# and handles the nitty-gritty of setting up the server properly.

exports.createCallServer = ->

    Unique_ID = 'Unique-ID'

    server = new eslServer (res) ->
      if exports.debug
        util.log "Incoming connection"
        util.log util.inspect res

      res.connect (req,res) =>

        # Channel data
        channel_data = req.body
        # UUID
        unique_id = channel_data[Unique_ID]

        if exports.debug
          util.log "Incoming call UUID = #{unique_id}"

        # Clean-up at the end of the connection.
        res.on 'esl_disconnect_notice', (req,res) ->
          if exports.debug
            util.log "Received ESL disconnection notice"
          switch req.headers['Content-Disposition']
            when 'linger'      then res.exit()
            when 'disconnect'  then res.end()

        # Use this from your code to force a disconnection.
        #
        #     res.emit 'force_disconnect'
        #
        res.on 'force_disconnect', ->
          util.log 'Hangup call'
          @bgapi "uuid_kill #{unique_id}"

        # Translate channel events into server events.
        res.on 'esl_event', (req,res) ->
          req.channel_data = channel_data
          req.unique_id = unique_id
          server.emit req.body['Event-Name'], req, res

        # Handle the incoming connection
        res.linger (req,res) ->
          res.filter Unique_ID, unique_id, (req,res) ->
            res.event_json 'ALL', (req,res) ->
              req.channel_data = channel_data
              req.unique_id = unique_id
              server.emit 'CONNECT', req, res
