<commit_before>noflo = require 'noflo'
express = require 'express'

class Server extends noflo.Component
  description: "A basic express.js server creation component"

  constructor: ->
    @inPorts =
      listen: new noflo.Port 'number'
      close: new noflo.Port 'number'
    @outPorts =
      server: new noflo.Port 'object'
      error: new noflo.Port 'object'

    @servers = {}

    @inPorts.listen.on 'data', (port) =>
      @createServer(port)
    @inPorts.close.on 'data', (port) =>
      @closeServer(port)

  sendError: (msg) ->
    return unless @outPorts.error.isAttached()
    @outPorts.error.send(new Error(msg))
    @outPorts.error.disconnect()

  createServer: (port) ->
    if @servers[port]
      @sendError("Port #{port} already in use")
      return
    server = express()
    try
      server.listen(port)
      @servers[port] = server
      if @outPorts.server.isAttached()
        @outPorts.server.send(server)
        @outPorts.server.disconnect()
    catch e
      @sendError("Cannot listen to port #{port} : #{e.message}")

  closeServer: (port) ->
    unless @server[port]
      @sendError("Port #{port} not listened to")
      return
    @servers[port].close()
    delete @servers[port]

  shutdown: ->
    for port, server of @servers
      server.close()
    @servers = {}

exports.getComponent = -> new Server
<commit_msg>Server: use createServer() method<commit_after>noflo = require 'noflo'
express = require 'express'

class Server extends noflo.Component
  description: "A basic express.js server creation component"

  constructor: ->
    @inPorts =
      listen: new noflo.Port 'number'
      close: new noflo.Port 'number'
    @outPorts =
      server: new noflo.Port 'object'
      error: new noflo.Port 'object'

    @servers = {}

    @inPorts.listen.on 'data', (port) =>
      @createServer(port)
    @inPorts.close.on 'data', (port) =>
      @closeServer(port)

  sendError: (msg) ->
    return unless @outPorts.error.isAttached()
    @outPorts.error.send(new Error(msg))
    @outPorts.error.disconnect()

  createServer: (port) ->
    if @servers[port]
      @sendError("Port #{port} already in use")
      return
    server = express.createServer()
    try
      server.listen(port)
      @servers[port] = server
      if @outPorts.server.isAttached()
        @outPorts.server.send(server)
        @outPorts.server.disconnect()
    catch e
      @sendError("Cannot listen to port #{port} : #{e.message}")

  closeServer: (port) ->
    unless @server[port]
      @sendError("Port #{port} not listened to")
      return
    @servers[port].close()
    delete @servers[port]

  shutdown: ->
    for port, server of @servers
      server.close()
    @servers = {}

exports.getComponent = -> new Server

{'fraction_non_alphanumeric': 0.1355191256830601, 'fraction_numerical': 0.0, 'mean_word_length': 2.8621659634317864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '778788', 'n_tokens_mistral': 902, 'n_tokens_neox': 893, 'n_words': 213}