instantiate Core

This commit is contained in:
Zlatin Balevsky
2019-05-29 18:00:55 +01:00
parent 4c9d3dc5f3
commit 2cafba56ea

View File

@@ -50,32 +50,21 @@ import net.i2p.data.Signature
import net.i2p.data.SigningPrivateKey import net.i2p.data.SigningPrivateKey
@Log @Log
class Core { public class Core {
static main(args) { final EventBus eventBus
def home = System.getProperty("user.home") + File.separator + ".MuWire" final Persona me
home = new File(home)
if (!home.exists()) {
log.info("creating home dir")
home.mkdir()
}
def props = new Properties() private final TrustService trustService
def propsFile = new File(home, "MuWire.properties") private final PersisterService persisterService
if (propsFile.exists()) { private final HostCache hostCache
log.info("loading existing props file") private final ConnectionManager connectionManager
propsFile.withInputStream { private final CacheClient cacheClient
props.load(it) private final ConnectionAcceptor connectionAcceptor
} private final ConnectionEstablisher connectionEstablisher
props = new MuWireSettings(props) private final HasherService hasherService
} else {
log.info("creating default properties")
props = new MuWireSettings()
propsFile.withOutputStream {
props.write(it)
}
}
public Core(MuWireSettings props) {
log.info "Initializing I2P context" log.info "Initializing I2P context"
I2PAppContext.getGlobalContext().logManager() I2PAppContext.getGlobalContext().logManager()
I2PAppContext.getGlobalContext()._logManager = new MuWireLogManager() I2PAppContext.getGlobalContext()._logManager = new MuWireLogManager()
@@ -101,7 +90,6 @@ class Core {
socketManager.getDefaultOptions().setConnectTimeout(30000) socketManager.getDefaultOptions().setConnectTimeout(30000)
i2pSession = socketManager.getSession() i2pSession = socketManager.getSession()
Persona me
def destination = new Destination() def destination = new Destination()
def spk = new SigningPrivateKey(Constants.SIG_TYPE) def spk = new SigningPrivateKey(Constants.SIG_TYPE)
keyDat.withInputStream { keyDat.withInputStream {
@@ -127,15 +115,14 @@ class Core {
me = new Persona(new ByteArrayInputStream(baos.toByteArray())) me = new Persona(new ByteArrayInputStream(baos.toByteArray()))
log.info("Loaded myself as "+me.getHumanReadableName()) log.info("Loaded myself as "+me.getHumanReadableName())
EventBus eventBus = new EventBus() eventBus = new EventBus()
log.info("initializing trust service") log.info("initializing trust service")
File goodTrust = new File(home, "trust.good") File goodTrust = new File(home, "trust.good")
File badTrust = new File(home, "trust.bad") File badTrust = new File(home, "trust.bad")
TrustService trustService = new TrustService(goodTrust, badTrust, 5000) trustService = new TrustService(goodTrust, badTrust, 5000)
eventBus.register(TrustEvent.class, trustService) eventBus.register(TrustEvent.class, trustService)
trustService.start()
trustService.waitForLoad()
log.info "initializing file manager" log.info "initializing file manager"
FileManager fileManager = new FileManager(eventBus) FileManager fileManager = new FileManager(eventBus)
@@ -146,16 +133,13 @@ class Core {
eventBus.register(SearchEvent.class, fileManager) eventBus.register(SearchEvent.class, fileManager)
log.info "initializing persistence service" log.info "initializing persistence service"
PersisterService persisterService = new PersisterService(new File(home, "files.json"), eventBus, 5000, fileManager) persisterService = new PersisterService(new File(home, "files.json"), eventBus, 5000, fileManager)
persisterService.start()
log.info("initializing host cache") log.info("initializing host cache")
File hostStorage = new File(home, "hosts.json") File hostStorage = new File(home, "hosts.json")
HostCache hostCache = new HostCache(trustService,hostStorage, 30000, props, i2pSession.getMyDestination()) HostCache hostCache = new HostCache(trustService,hostStorage, 30000, props, i2pSession.getMyDestination())
eventBus.register(HostDiscoveredEvent.class, hostCache) eventBus.register(HostDiscoveredEvent.class, hostCache)
eventBus.register(ConnectionEvent.class, hostCache) eventBus.register(ConnectionEvent.class, hostCache)
hostCache.start()
hostCache.waitForLoad()
log.info("initializing connection manager") log.info("initializing connection manager")
ConnectionManager connectionManager = props.isLeaf() ? ConnectionManager connectionManager = props.isLeaf() ?
@@ -164,11 +148,9 @@ class Core {
eventBus.register(ConnectionEvent.class, connectionManager) eventBus.register(ConnectionEvent.class, connectionManager)
eventBus.register(DisconnectionEvent.class, connectionManager) eventBus.register(DisconnectionEvent.class, connectionManager)
eventBus.register(QueryEvent.class, connectionManager) eventBus.register(QueryEvent.class, connectionManager)
connectionManager.start()
log.info("initializing cache client") log.info("initializing cache client")
CacheClient cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000) cacheClient = new CacheClient(eventBus,hostCache, connectionManager, i2pSession, props, 10000)
cacheClient.start()
log.info("initializing connector") log.info("initializing connector")
I2PConnector i2pConnector = new I2PConnector(socketManager) I2PConnector i2pConnector = new I2PConnector(socketManager)
@@ -191,20 +173,56 @@ class Core {
log.info("initializing acceptor") log.info("initializing acceptor")
I2PAcceptor i2pAcceptor = new I2PAcceptor(socketManager) I2PAcceptor i2pAcceptor = new I2PAcceptor(socketManager)
ConnectionAcceptor acceptor = new ConnectionAcceptor(eventBus, connectionManager, props, connectionAcceptor = new ConnectionAcceptor(eventBus, connectionManager, props,
i2pAcceptor, hostCache, trustService, searchManager, uploadManager) i2pAcceptor, hostCache, trustService, searchManager, uploadManager)
acceptor.start()
ConnectionEstablisher connector = new ConnectionEstablisher(eventBus, i2pConnector, props, connectionManager, hostCache) connectionEstablisher = new ConnectionEstablisher(eventBus, i2pConnector, props, connectionManager, hostCache)
connector.start()
log.info("initializing hasher service") log.info("initializing hasher service")
HasherService hasherService = new HasherService(new FileHasher(), eventBus) hasherService = new HasherService(new FileHasher(), eventBus)
eventBus.register(FileSharedEvent.class, hasherService) eventBus.register(FileSharedEvent.class, hasherService)
}
public void startServices() {
hasherService.start() hasherService.start()
trustService.waitForLoad()
trustService.start()
persisterService.start()
hostCache.start()
connectionManager.start()
cacheClient.start()
connectionAcceptor.start()
connectionEstablisher.start()
hostCache.waitForLoad()
}
static main(args) {
def home = System.getProperty("user.home") + File.separator + ".MuWire"
home = new File(home)
if (!home.exists()) {
log.info("creating home dir")
home.mkdir()
}
def props = new Properties()
def propsFile = new File(home, "MuWire.properties")
if (propsFile.exists()) {
log.info("loading existing props file")
propsFile.withInputStream {
props.load(it)
}
props = new MuWireSettings(props)
} else {
log.info("creating default properties")
props = new MuWireSettings()
propsFile.withOutputStream {
props.write(it)
}
}
Core core = new Core(props)
core.startServices()
// ... at the end, sleep or execute script // ... at the end, sleep or execute script
if (args.length == 0) { if (args.length == 0) {
@@ -220,12 +238,11 @@ class Core {
def binding = new Binding() def binding = new Binding()
def shell = new GroovyShell(binding) def shell = new GroovyShell(binding)
binding.setProperty('eventBus', eventBus) binding.setProperty('eventBus', core.eventBus)
binding.setProperty('me', me) binding.setProperty('me', core.me)
// TOOD: other bindings? // TOOD: other bindings?
def script = shell.parse(f) def script = shell.parse(f)
script.run() script.run()
} }
} }
} }