From 757dbddaea8d938ad5efb08c744a0d85fc51ac14 Mon Sep 17 00:00:00 2001 From: Matthias Howald Date: Fri, 1 Sep 2017 18:24:03 +0200 Subject: [PATCH 1/8] Fix build --- .../ch/dissem/bitmessage/networking/NetworkHandlerTest.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt b/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt index 4eaface..80f9c7b 100644 --- a/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt +++ b/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt @@ -31,7 +31,8 @@ import com.nhaarman.mockito_kotlin.mock import org.hamcrest.Matchers.`is` import org.hamcrest.Matchers.notNullValue import org.junit.After -import org.junit.Assert.* +import org.junit.Assert.assertEquals +import org.junit.Assert.assertThat import org.junit.Before import org.junit.Rule import org.junit.Test @@ -233,7 +234,7 @@ class NetworkHandlerTest { private val peerAddress = NetworkAddress.Builder().ipv4(127, 0, 0, 1).port(6001).build() private fun shutdown(ctx: BitmessageContext) { - if (!ctx.isRunning) return + if (!ctx.isRunning()) return ctx.shutdown() do { @@ -242,7 +243,7 @@ class NetworkHandlerTest { } catch (ignore: InterruptedException) { } - } while (ctx.isRunning) + } while (ctx.isRunning()) } private fun shutdown(networkHandler: NetworkHandler) { From 66a6768f9b5da661b7ebd439e4b196593dc52f69 Mon Sep 17 00:00:00 2001 From: Matthias Howald Date: Fri, 1 Sep 2017 18:26:37 +0200 Subject: [PATCH 2/8] Port demo to Kotlin --- .../dissem/bitmessage/demo/Application.java | 491 ------------------ .../dissem/bitmessage/demo/CommandLine.java | 102 ---- .../java/ch/dissem/bitmessage/demo/Main.java | 125 ----- .../ch/dissem/bitmessage/demo/Application.kt | 453 ++++++++++++++++ .../ch/dissem/bitmessage/demo/CommandLine.kt | 92 ++++ .../kotlin/ch/dissem/bitmessage/demo/Main.kt | 118 +++++ 6 files changed, 663 insertions(+), 718 deletions(-) delete mode 100644 demo/src/main/java/ch/dissem/bitmessage/demo/Application.java delete mode 100644 demo/src/main/java/ch/dissem/bitmessage/demo/CommandLine.java delete mode 100644 demo/src/main/java/ch/dissem/bitmessage/demo/Main.java create mode 100644 demo/src/main/kotlin/ch/dissem/bitmessage/demo/Application.kt create mode 100644 demo/src/main/kotlin/ch/dissem/bitmessage/demo/CommandLine.kt create mode 100644 demo/src/main/kotlin/ch/dissem/bitmessage/demo/Main.kt diff --git a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java b/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java deleted file mode 100644 index d915215..0000000 --- a/demo/src/main/java/ch/dissem/bitmessage/demo/Application.java +++ /dev/null @@ -1,491 +0,0 @@ -/* - * Copyright 2015 Christian Basler - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.dissem.bitmessage.demo; - -import ch.dissem.bitmessage.BitmessageContext; -import ch.dissem.bitmessage.entity.BitmessageAddress; -import ch.dissem.bitmessage.entity.Plaintext; -import ch.dissem.bitmessage.entity.payload.Pubkey; -import ch.dissem.bitmessage.entity.valueobject.Label; -import ch.dissem.bitmessage.entity.valueobject.extended.Message; -import org.apache.commons.lang3.text.WordUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.InetAddress; -import java.util.List; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import static ch.dissem.bitmessage.demo.CommandLine.COMMAND_BACK; -import static ch.dissem.bitmessage.demo.CommandLine.ERROR_UNKNOWN_COMMAND; -import static java.util.regex.Pattern.CASE_INSENSITIVE; - -/** - * A simple command line Bitmessage application - */ -public class Application { - private final static Logger LOG = LoggerFactory.getLogger(Application.class); - private final static Pattern RESPONSE_PATTERN = Pattern.compile("^RE:.*$", CASE_INSENSITIVE); - private final CommandLine commandLine; - - private BitmessageContext ctx; - - public Application(BitmessageContext.Builder ctxBuilder, InetAddress syncServer, int syncPort) { - ctx = ctxBuilder - .listener(plaintext -> System.out.println("New Message from " + plaintext.getFrom() + ": " + plaintext.getSubject())) - .build(); - - if (syncServer == null) { - ctx.startup(); - } - - commandLine = new CommandLine(); - - String command; - do { - System.out.println(); - System.out.println("available commands:"); - System.out.println("i) identities"); - System.out.println("c) contacts"); - System.out.println("s) subscriptions"); - System.out.println("m) messages"); - if (syncServer != null) { - System.out.println("y) sync"); - } - System.out.println("?) info"); - System.out.println("e) exit"); - - command = commandLine.nextCommand(); - try { - switch (command) { - case "i": { - identities(); - break; - } - case "c": - contacts(); - break; - case "s": - subscriptions(); - break; - case "m": - labels(); - break; - case "?": - info(); - break; - case "e": - break; - case "y": - if (syncServer != null) { - ctx.synchronize(syncServer, syncPort, 120, true); - } - break; - default: - System.out.println(ERROR_UNKNOWN_COMMAND); - } - } catch (Exception e) { - LOG.debug(e.getMessage()); - } - } while (!"e".equals(command)); - LOG.info("Shutting down client"); - ctx.cleanup(); - ctx.shutdown(); - } - - private void info() { - String command; - do { - System.out.println(); - System.out.println(ctx.status()); - System.out.println(); - System.out.println("c) cleanup inventory"); - System.out.println("r) resend unacknowledged messages"); - System.out.println(COMMAND_BACK); - - command = commandLine.nextCommand(); - switch (command) { - case "c": - ctx.cleanup(); - break; - case "r": - ctx.resendUnacknowledgedMessages(); - break; - case "b": - return; - } - } while (!"b".equals(command)); - } - - private void identities() { - String command; - List identities = ctx.addresses().getIdentities(); - do { - System.out.println(); - commandLine.listAddresses(identities, "identities"); - System.out.println("a) create identity"); - System.out.println("c) join chan"); - System.out.println(COMMAND_BACK); - - command = commandLine.nextCommand(); - switch (command) { - case "a": - addIdentity(); - identities = ctx.addresses().getIdentities(); - break; - case "c": - joinChan(); - identities = ctx.addresses().getIdentities(); - break; - case "b": - return; - default: - try { - int index = Integer.parseInt(command) - 1; - address(identities.get(index)); - } catch (NumberFormatException e) { - System.out.println(ERROR_UNKNOWN_COMMAND); - } - } - } while (!"b".equals(command)); - } - - private void addIdentity() { - System.out.println(); - BitmessageAddress identity = ctx.createIdentity(commandLine.yesNo("would you like a shorter address? This will take some time to calculate."), Pubkey.Feature.DOES_ACK); - System.out.println("Please enter an alias for this identity, or an empty string for none"); - String alias = commandLine.nextLineTrimmed(); - if (alias.length() > 0) { - identity.setAlias(alias); - } - ctx.addresses().save(identity); - } - - private void joinChan() { - System.out.println(); - System.out.print("Passphrase: "); - String passphrase = commandLine.nextLine(); - System.out.print("Address: "); - String address = commandLine.nextLineTrimmed(); - ctx.joinChan(passphrase, address); - } - - private void contacts() { - String command; - List contacts = ctx.addresses().getContacts(); - do { - System.out.println(); - commandLine.listAddresses(contacts, "contacts"); - System.out.println(); - System.out.println("a) add contact"); - System.out.println(COMMAND_BACK); - - command = commandLine.nextCommand(); - switch (command) { - case "a": - addContact(false); - contacts = ctx.addresses().getContacts(); - break; - case "b": - return; - default: - try { - int index = Integer.parseInt(command) - 1; - address(contacts.get(index)); - } catch (NumberFormatException e) { - System.out.println(ERROR_UNKNOWN_COMMAND); - } - } - } while (!"b".equals(command)); - } - - private void addContact(boolean isSubscription) { - System.out.println(); - System.out.println("Please enter the Bitmessage address you want to add"); - try { - BitmessageAddress address = new BitmessageAddress(commandLine.nextLineTrimmed()); - System.out.println("Please enter an alias for this address, or an empty string for none"); - String alias = commandLine.nextLineTrimmed(); - if (alias.length() > 0) { - address.setAlias(alias); - } - if (isSubscription) { - ctx.addSubscribtion(address); - } - ctx.addContact(address); - } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); - } - } - - private void subscriptions() { - String command; - List subscriptions = ctx.addresses().getSubscriptions(); - do { - System.out.println(); - commandLine.listAddresses(subscriptions, "subscriptions"); - System.out.println(); - System.out.println("a) add subscription"); - System.out.println(COMMAND_BACK); - - command = commandLine.nextCommand(); - switch (command) { - case "a": - addContact(true); - subscriptions = ctx.addresses().getSubscriptions(); - break; - case "b": - return; - default: - try { - int index = Integer.parseInt(command) - 1; - address(subscriptions.get(index)); - } catch (NumberFormatException e) { - System.out.println(ERROR_UNKNOWN_COMMAND); - } - } - } while (!"b".equals(command)); - } - - private void address(BitmessageAddress address) { - System.out.println(); - if (address.getAlias() != null) - System.out.println(address.getAlias()); - System.out.println(address.getAddress()); - System.out.println("Stream: " + address.getStream()); - System.out.println("Version: " + address.getVersion()); - if (address.getPrivateKey() == null) { - if (address.getPubkey() == null) { - System.out.println("Public key still missing"); - } else { - System.out.println("Public key available"); - } - } else { - if (address.isChan()) { - System.out.println("Chan"); - } else { - System.out.println("Identity"); - } - } - } - - private void labels() { - List