<commit_before><commit_msg>add client helper class for websocket service (wss)<commit_after>{DBG, ERR, WARN, INFO} = global.get-logger __filename
sioc = require \socket.io-client

const EVENT_READY = \ready
const EVENT_CONFIGURED = \configured
const EVENT_DATA = \data
const REQ_CONFIGURE = \configure


module.exports = exports = class WssClient
  (@server, @channel, @name=\smith, @token=<PASSWORD>, @opts={}, @verbose=no) ->
    self = @
    ws = self.ws = sioc "#{server}/#{channel}", autoConnect: no
    ws.on \connect, -> return self.at-internal-connected!
    ws.on \disconnect, -> return self.at-internal-disconnected!
    ws.on EVENT_READY, -> return self.at-internal-ready!
    ws.on EVENT_CONFIGURED, -> return self.at-internal-configured!
    ws.on EVENT_DATA, (p) -> return self.at-ws-data p


  connect: (done) -> return @.ws.connect done


  at-internal-ready: ->
    {ws, name, token, opts} = self = @
    args = [name, token, opts]
    index = new Date! - 0
    return ws.emit REQ_CONFIGURE, {index: index, args: args}


  at-internal-connected: ->
    {server, channel} = self = @
    INFO "connected to #{server} (channel: #{channel}) via websocket protocol"
    return @.at-connected @ws


  at-internal-configured: ->
    return @.at-configured @ws

  at-internal-disconnected: ->
    return @.at-disconnected @ws


  at-ws-data: (p) ->
    {verbose, ws} = self = @
    {index, category, items, args} = p
    return WARN "missing `index` in data event" unless index?
    return WARN "missing `category` in data event" unless category?
    return WARN "missing `items` in data event" unless items?
    name = "process_data_#{category}"
    INFO "data[#{category}]: #{JSON.stringify items} (#{JSON.stringify args})" if verbose
    args = [] unless args?
    args = [args] unless Array.isArray args
    items = [items] unless Array.isArray items
    args = [items] ++ args
    f = self[name]
    return WARN "missing handler function #{name} in subclass" unless f?
    return f.apply self, args


  ##
  # Subclass shall implement following methods when necessary.
  #
  at-connected: (ws) -> return
  at-disconnected: (ws) -> return
  at-configured: (ws) -> return

{'fraction_non_alphanumeric': 0.13873626373626374, 'fraction_numerical': 0.0004578754578754579, 'mean_word_length': 3.3267326732673266, 'pattern_counts': {'":': 0, '<': 4, '<?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': '4125460', 'n_tokens_mistral': 716, 'n_tokens_neox': 689, 'n_words': 212}