Ability to load custom modules. A module for logging queries

pull/53/head
Zlatin Balevsky 2020-12-08 13:06:01 +00:00
parent 759136cff3
commit 13e23a7622
No known key found for this signature in database
GPG Key ID: A72832072D525E41
5 changed files with 74 additions and 0 deletions

View File

@ -166,6 +166,8 @@ public class Core {
final SigningPrivateKey spk
private final List<MWModule> modules = new ArrayList<>()
public Core(MuWireSettings props, File home, String myVersion) {
this.home = home
this.version = myVersion
@ -499,9 +501,25 @@ public class Core {
register(UIMessageDeleteEvent.class, messenger)
register(UIMessageReadEvent.class, messenger)
}
File modulesProps = new File(home, "mwmodules.list")
if (modulesProps.exists()) {
log.info("loading modules")
modulesProps.eachLine {
Class moduleClass = Class.forName(it)
MWModule module = moduleClass.newInstance()
modules.add(module)
}
}
}
public void startServices() {
modules.each {
log.info("initializing module ${it.getName()}")
it.init(this)
}
i2pSession.connect()
hasherService.start()
trustService.start()
@ -516,6 +534,11 @@ public class Core {
feedManager.start()
feedClient.start()
trackerResponder.start()
modules.each {
log.info("starting module ${it.getName()}")
it.start()
}
}
public void shutdown() {
@ -523,6 +546,12 @@ public class Core {
log.info("already shutting down")
return
}
modules.each {
log.info("shutting down module ${it.getName()}")
it.stop()
}
log.info("saving settings")
saveMuSettings()
log.info("shutting down host cache")

View File

@ -0,0 +1,9 @@
package com.muwire.core
interface MWModule {
public String getName()
public void init(Core core)
public void start()
public void stop()
}

View File

@ -0,0 +1,3 @@
dependencies {
compile project(":core")
}

View File

@ -0,0 +1,32 @@
import com.muwire.core.Core
import com.muwire.core.MWModule
import com.muwire.core.search.SearchEvent
class QueryLogger implements MWModule {
private final File dump = new File("searches.csv")
void onSearchEvent(SearchEvent e) {
dump.withWriterAppend("UTF-8", {writer ->
String coalesced = String.join(" ", e.searchTerms)
writer.append(String.format("%d,%s,%s\n", e.getTimestamp(), coalesced, e.persona.getHumanReadableName()))
})
}
@Override
String getName() {
"QueryLogger"
}
@Override
void init(Core core) {
core.getEventBus().register(SearchEvent.class, this)
}
@Override
void start() {
}
@Override
void stop() {
}
}

View File

@ -2,6 +2,7 @@ include 'pinger'
include 'host-cache'
include 'update-server'
include 'core'
include 'mwmodules'
include 'gui'
include 'cli'
include 'cli-lanterna'