buildListener: Builds a listener from a JSON description.

buildListenerR Documentation

Builds a listener from a JSON description.

Description

This is used in configuration, it will build a listener from a JSON description of the listener. The “name” and “type” fields are required. The other fields should match the arguments for the constructor, with the exceptions noted below:

Usage

buildListener(specs, app, dburi, defaultDB="Proc4",
              ssl_options=mongolite::ssl_options(),
              noMongo = !missing(dburi) && length(dburi) > 0L && nchar(dburi) > 0L)

Arguments

specs

A named list (from the JSON) containing the instructions for building the listener.

app

A character value that will get substituted for the string “<app>” in the “name” and “sender” fields.

dburi

If a database is used for this listener, then this is the uri for the connection. Note that this is specified in the code and not in the JSON.

defaultDB

The name of the database with which the Listener will interact, only used if no dbname field in specs.

ssl_options

Options used for an SSL connection to the database. ssl_options.

noMongo

A logical value. If true, then the connection to the Mongo database will not be made, and CRUD operations will basically become no-ops.

Details

The input to this function is a list that comes from JSON (or some other input method that returns a named list). The specs$type field should be the name of a Listener class. This means that specs$type is the name of a constructor function, and the rest of the spec argument are the arguments.

Currently, the following fields are used.

name

The name of the listener, required. The string “<app>” is substituted for app.

type

Required, the name of the constructor for the desired class. The function will generate an error if this does not correspond to the name of a class.

sender

A string insterted into logged messages. The string “<app>” is substituted for app.

dbname

The name of the database in which the messages will be recorded. If not present, then the defaultdb will be used.

colname

The name of the database collection in which the messages will be recorded.

messages

A character vector giving the names of the messages the listener will pay attention to. Note that this maps to the field “messSet” in the listener object.

targetField

Used in the UpdateListener and UpsertListener to indicate the field to be modified.

jsonEncoder

The name of a function used to encode the field value to be modified as JSON. See stats2json.

qfields

A character vector giving the names of the fields used as the key for finding the message to replace. Usually should contain c("uid","app").

fields

This should be a named character vector (or list) whose names indicate the names of the observables/statistics to collect, and whose values are the types. See TableListener; this field maps to the “fieldlist” field of that class.

Other fields in specs are ignored.

Value

An object of the virtual class Listener (i.e., something for which isListener should return true.

Note

The field name “messages” maps to the internal field messSet. The field name “fields” maps to the internal field fieldlist.

Author(s)

Russell Almond

See Also

Listener, fromJSON

Examples


jspecs <- '[
	{
	  "name":"ppLS<app>",
	  "type":"TableListener",
	  "messages":["Coins Earned","Coins Spent", "LS Watched"],
	  "fields":{
		  "uid":"character",
		  "context":"character",
		  "timestamp":"character",
		  "currentMoney":"numeric",
		  "appId":"numeric",
		  "mess":"character",
		  "money":"numeric",
		  "onWhat":"character",
		  "LS_duration":"difftime",
		  "learningSupportType":"character"
	  }
	},
	{
	    "name":"ToEA",
	    "type":"InjectionListener",
	    "dbname":"EARecords",
	    "colname":"EvidenceSets",
	    "messages":["New Observables"]
	},
	{
	    "name":"PPPersistantData",
	    "type":"UpdateListener",
	    "dbname":"Proc4",
	    "colname":"Players",
	    "targetField":"data",
	    "jsonEncoder":"trophy2json",
	    "messages":["Money Earned", "Money Spent"]
	}
    ]'

speclist <- jsonlite::fromJSON(jspecs,FALSE)

l1 <- buildListener(speclist[[1]],"test",mongo::makeDBuri())

l2 <- buildListener(speclist[[2]],"test",mongo::makeDBuri())

l3 <- buildListener(speclist[[3]],"test",mongo::makeDBuri())


ralmond/Proc4 documentation built on June 13, 2025, 7:13 a.m.