import freenet.client.async.ClientPutter;
import freenet.client.async.SnoopMetadata;
import freenet.client.async.USKCallback;
+import freenet.client.async.USKManager;
import freenet.keys.FreenetURI;
import freenet.keys.InsertableClientSSK;
import freenet.keys.USK;
/** The node to interact with. */
private final Node node;
+ private final USKManager uskManager;
+ private final ClientContext clientContext;
private final SoneUriCreator soneUriCreator;
private final RequestClient imageLoader = new RequestClientBuilder().realTime().build();
@Inject
- public FreenetInterface(EventBus eventBus, Node node, SoneUriCreator soneUriCreator) {
+ public FreenetInterface(EventBus eventBus, Node node, USKManager uskManager, ClientContext clientContext, SoneUriCreator soneUriCreator, HighLevelSimpleClientCreator highLevelSimpleClientCreator) {
this.eventBus = eventBus;
this.node = node;
+ this.uskManager = uskManager;
+ this.clientContext = clientContext;
this.soneUriCreator = soneUriCreator;
- this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
+ this.client = highLevelSimpleClientCreator.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
}
//
try {
ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS);
clientGetter.setMetaSnoop(snoop);
- clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext);
+ clientGetter.restart(uri, fetchContext.filterData, clientContext);
} catch (FetchException fe) {
/* stupid exception that can not actually be thrown! */
}
USKCallback uskCallback) {
try {
soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
- node.clientCore.uskManager.subscribe(create(requestUri),
+ uskManager.subscribe(create(requestUri),
uskCallback, true, requestClient);
} catch (MalformedURLException mue1) {
logger.log(WARNING, format("Could not subscribe USK “%s”!",
USKCallback uskCallback) {
try {
soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
- node.clientCore
- .uskManager
- .subscribe(create(requestUri), uskCallback, false, requestClient);
+ uskManager.subscribe(create(requestUri), uskCallback, false, requestClient);
} catch (MalformedURLException mue1) {
logger.log(WARNING,
format("Could not subscribe USK “%s”!", requestUri),
logger.log(Level.FINEST, String.format("USKs left: %d", soneUskCallbacks.size()));
uskCallbacks.forEach(uskCallback -> {
try {
- node.clientCore.uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback);
+ uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback);
} catch (MalformedURLException mue1) {
logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", soneUriCreator.getRequestUri(sone)), mue1);
}
};
try {
- node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
+ uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
uriUskCallbacks.put(USK.create(uri).clearCopy().getURI(), uskCallback);
} catch (MalformedURLException mue1) {
logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
return;
}
- node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
+ uskManager.unsubscribe(USK.create(uri), uskCallback);
} catch (MalformedURLException mue1) {
logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
}
*/
@SuppressWarnings("synthetic-access")
public void cancel() {
- clientPutter.cancel(node.clientCore.clientContext);
+ clientPutter.cancel(clientContext);
eventBus.post(new ImageInsertAbortedEvent(image));
bucket.free();
}
--- /dev/null
+package net.pterodactylus.sone.freenet
+
+import freenet.client.HighLevelSimpleClient
+import freenet.node.NodeClientCore
+
+fun interface HighLevelSimpleClientCreator {
+
+ fun makeClient(prioClass: Short, forceDontIgnoreTooManyPathComponents: Boolean, realTimeFlag: Boolean): HighLevelSimpleClient
+
+}
+
+class DefaultHighLevelSimpleClientCreator(private val nodeClientCore: NodeClientCore) : HighLevelSimpleClientCreator {
+
+ override fun makeClient(prioClass: Short, forceDontIgnoreTooManyPathComponents: Boolean, realTimeFlag: Boolean): HighLevelSimpleClient =
+ nodeClientCore.makeClient(prioClass, forceDontIgnoreTooManyPathComponents, realTimeFlag)
+
+}
import com.google.inject.*
import freenet.client.*
+import freenet.client.async.ClientContext
+import freenet.client.async.USKManager
import freenet.clients.http.*
import freenet.node.*
import freenet.pluginmanager.*
+import net.pterodactylus.sone.freenet.DefaultHighLevelSimpleClientCreator
+import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator
import net.pterodactylus.sone.freenet.plugin.*
import javax.inject.Provider
import javax.inject.Singleton
@Singleton
fun getSessionManager() = pluginRespirator.getSessionManager("Sone")!!
+ @Provides
+ fun getNodeClientCore(node: Node): NodeClientCore =
+ node.clientCore
+
+ @Provides
+ fun getHighLevelSimpleClientCreator(nodeClientCore: NodeClientCore): HighLevelSimpleClientCreator =
+ DefaultHighLevelSimpleClientCreator(nodeClientCore)
+
+ @Provides
+ fun getClientContext(nodeClientCore: NodeClientCore): ClientContext =
+ nodeClientCore.clientContext
+
+ @Provides
+ fun getUskManager(nodeClientCore: NodeClientCore): USKManager =
+ nodeClientCore.uskManager
+
}
@Suppress("UnstableApiUsage")
private val eventBus = mock<EventBus>()
private val node = mock<Node>()
- private val nodeClientCore = mock<NodeClientCore>()
+ private val clientContext = mock<ClientContext>()
private val highLevelSimpleClient: HighLevelSimpleClient = mock(HighLevelSimpleClient::class.java, withSettings().extraInterfaces(RequestClient::class.java))
private val randomSource = DummyRandomSource()
private val uskManager = mock<USKManager>()
private val freenetInterface: FreenetInterface
init {
- whenever(nodeClientCore.makeClient(anyShort(), anyBoolean(), anyBoolean())).thenReturn(highLevelSimpleClient)
- setField(node, "clientCore", nodeClientCore)
setField(node, "random", randomSource)
- setField(nodeClientCore, "uskManager", uskManager)
- setField(nodeClientCore, "clientContext", mock<ClientContext>())
- freenetInterface = FreenetInterface(eventBus, node, soneUriCreator)
+ freenetInterface = FreenetInterface(eventBus, node, uskManager, clientContext, soneUriCreator) { _, _, _ -> highLevelSimpleClient }
insertToken = freenetInterface.InsertToken(image)
insertToken.setBucket(bucket)
}
import com.google.inject.*
import freenet.client.*
+import freenet.client.async.ClientContext
+import freenet.client.async.USKManager
import freenet.clients.http.*
import freenet.node.*
import freenet.pluginmanager.*
+import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator
import net.pterodactylus.sone.freenet.plugin.*
import net.pterodactylus.sone.test.*
import org.hamcrest.MatcherAssert.*
val expectedException = ExpectedException.none()!!
private val sessionManager = mock<SessionManager>()
+ private val uskManager = mock<USKManager>()
+ private val clientContext = mock<ClientContext>()
private val pluginRespirator = deepMock<PluginRespirator>().apply {
whenever(getSessionManager("Sone")).thenReturn(sessionManager)
}
- private val node = pluginRespirator.node!!
+ private val nodeClientCore = mock<NodeClientCore>().also {
+ setField(it, "uskManager", uskManager)
+ setField(it, "clientContext", clientContext)
+ }
+ private val node = pluginRespirator.node!!.also {
+ setField(it, "clientCore", nodeClientCore)
+ }
private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!!
private val toadletContainer: ToadletContainer = pluginRespirator.toadletContainer
private val pageMaker: PageMaker = pluginRespirator.pageMaker
injector.verifySingletonInstance<PluginConnector>()
}
+ @Test
+ fun `high level simple client creator is returned correctly`() {
+ assertThat(injector.getInstance<HighLevelSimpleClientCreator>(), notNullValue());
+ }
+
+ @Test
+ fun `client context is returned correctly`() {
+ assertThat(injector.getInstance<ClientContext>(), sameInstance(clientContext))
+ }
+
+ @Test
+ fun `usk manager is returned correctly`() {
+ assertThat(injector.getInstance<USKManager>(), sameInstance(uskManager))
+ }
+
}
setField(node, "clientCore", clientCore)
whenever(pluginRespirator.node).thenReturn(node)
setField(clientCore, "uskManager", uskManager)
+ setField(clientCore, "clientContext", mock<ClientContext>())
}
@Test
--- /dev/null
+package net.pterodactylus.sone.test
+
+import freenet.client.HighLevelSimpleClient
+import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator
+
+val dummyHighLevelSimpleClient = deepMock<HighLevelSimpleClient>()
+
+val dummyHighLevelSimpleClientCreator: HighLevelSimpleClientCreator = HighLevelSimpleClientCreator { _, _, _ -> dummyHighLevelSimpleClient }