Skip to content

Commit

Permalink
Merge common project at 6085d83
Browse files Browse the repository at this point in the history
  • Loading branch information
ripcurlx committed Sep 5, 2018
1 parent 9014925 commit fce7f45
Show file tree
Hide file tree
Showing 73 changed files with 7,366 additions and 0 deletions.
62 changes: 62 additions & 0 deletions common/build.gradle
@@ -0,0 +1,62 @@
plugins {
id 'java'
id 'maven'
id 'com.google.protobuf' version '0.8.5'
}

group = 'network.bisq'
version = '-SNAPSHOT'

ext {
protobufVersion = '3.5.1'
}

sourceCompatibility = 1.8

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

repositories {
jcenter()
maven { url 'https://jitpack.io' }
}

sourceSets.main.java.srcDir "$buildDir/generated/source/proto/main/java"

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:$protobufVersion"
}
}

dependencies {
compile "com.google.protobuf:protobuf-java:$protobufVersion"
compile 'com.google.code.gson:gson:2.7'
compile('com.googlecode.json-simple:json-simple:1.1.1') {
exclude(module: 'junit')
}
compile 'org.springframework:spring-core:4.3.6.RELEASE'
compile 'org.slf4j:slf4j-api:1.7.22'
compile 'ch.qos.logback:logback-core:1.1.10'
compile 'ch.qos.logback:logback-classic:1.1.10'
compile 'com.google.code.findbugs:jsr305:3.0.1'
compile 'com.google.guava:guava:20.0'
compile('com.google.inject:guice:4.1.0') {
exclude(module: 'guava')
}
compile('network.bisq.libdohj:libdohj-core:9573d077') {
exclude(module: 'jsr305')
exclude(module: 'slf4j-api')
exclude(module: 'guava')
exclude(module: 'protobuf-java')
}
compile 'org.jetbrains:annotations:13.0'
runtime 'org.bouncycastle:bcprov-jdk15on:1.56'
compile 'org.bouncycastle:bcpg-jdk15on:1.56'
compile 'commons-io:commons-io:2.4'
compile 'org.apache.commons:commons-lang3:3.4'
compileOnly 'org.projectlombok:lombok:1.16.16'
annotationProcessor 'org.projectlombok:lombok:1.16.16'
testCompile 'junit:junit:4.12'
}
84 changes: 84 additions & 0 deletions common/src/main/java/bisq/common/Clock.java
@@ -0,0 +1,84 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Helps configure listener objects that are run by the `UserThread` each second
// and can do per second, per minute and delayed second actions.
public class Clock {
private static final Logger log = LoggerFactory.getLogger(Clock.class);

public static final int IDLE_TOLERANCE = 20000;

public interface Listener {
void onSecondTick();

void onMinuteTick();

void onMissedSecondTick(long missed);
}

private Timer timer;
private final List<Listener> listeners = new LinkedList<>();
private long counter = 0;
private long lastSecondTick;

public Clock() {
}

public void start() {
if (timer == null) {
lastSecondTick = System.currentTimeMillis();
timer = UserThread.runPeriodically(() -> {
listeners.stream().forEach(Listener::onSecondTick);
counter++;
if (counter >= 60) {
counter = 0;
listeners.stream().forEach(Listener::onMinuteTick);
}

long currentTimeMillis = System.currentTimeMillis();
long diff = currentTimeMillis - lastSecondTick;
if (diff > 1000)
listeners.stream().forEach(listener -> listener.onMissedSecondTick(diff - 1000));

lastSecondTick = currentTimeMillis;
}, 1, TimeUnit.SECONDS);
}
}

public void stop() {
timer.stop();
timer = null;
counter = 0;
}

public void addListener(Listener listener) {
listeners.add(listener);
}

public void removeListener(Listener listener) {
listeners.remove(listener);
}
}
23 changes: 23 additions & 0 deletions common/src/main/java/bisq/common/CommonOptionKeys.java
@@ -0,0 +1,23 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

public class CommonOptionKeys {
public static final String LOG_LEVEL_KEY = "logLevel";
public static final String USE_DEV_MODE = "useDevMode";
}
24 changes: 24 additions & 0 deletions common/src/main/java/bisq/common/Envelope.java
@@ -0,0 +1,24 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

/**
* Interface for the outside envelope object sent over the network or persisted to disc.
*/
public interface Envelope extends Proto {
}
106 changes: 106 additions & 0 deletions common/src/main/java/bisq/common/FrameRateTimer.java
@@ -0,0 +1,106 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

import java.time.Duration;

import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* We simulate a global frame rate timer similar to FXTimer to avoid creation of threads for each timer call.
* Used only in headless apps like the seed node.
*/
public class FrameRateTimer implements Timer, Runnable {
private final Logger log = LoggerFactory.getLogger(FrameRateTimer.class);

private long interval;
private Runnable runnable;
private long startTs;
private boolean isPeriodically;
private final String uid = UUID.randomUUID().toString();
private volatile boolean stopped;

public FrameRateTimer() {
}

@Override
public void run() {
if (!stopped) {
try {
long currentTimeMillis = System.currentTimeMillis();
if ((currentTimeMillis - startTs) >= interval) {
runnable.run();
if (isPeriodically)
startTs = currentTimeMillis;
else
stop();
}
} catch (Throwable t) {
log.error(t.getMessage());
t.printStackTrace();
stop();
throw t;
}
}
}

@Override
public Timer runLater(Duration delay, Runnable runnable) {
this.interval = delay.toMillis();
this.runnable = runnable;
startTs = System.currentTimeMillis();
MasterTimer.addListener(this);
return this;
}

@Override
public Timer runPeriodically(Duration interval, Runnable runnable) {
this.interval = interval.toMillis();
isPeriodically = true;
this.runnable = runnable;
startTs = System.currentTimeMillis();
MasterTimer.addListener(this);
return this;
}

@Override
public void stop() {
stopped = true;
MasterTimer.removeListener(this);
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof FrameRateTimer)) return false;

FrameRateTimer that = (FrameRateTimer) o;

return !(uid != null ? !uid.equals(that.uid) : that.uid != null);

}

@Override
public int hashCode() {
return uid != null ? uid.hashCode() : 0;
}
}
52 changes: 52 additions & 0 deletions common/src/main/java/bisq/common/MasterTimer.java
@@ -0,0 +1,52 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

import java.util.Set;
import java.util.TimerTask;
import java.util.concurrent.CopyOnWriteArraySet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Runs all listener objects periodically in a short interval.
public class MasterTimer {
private final static Logger log = LoggerFactory.getLogger(MasterTimer.class);
private static final java.util.Timer timer = new java.util.Timer();
// frame rate of 60 fps is about 16 ms but we don't need such a short interval, 100 ms should be good enough
public static final long FRAME_INTERVAL_MS = 100;

static {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
UserThread.execute(() -> listeners.stream().forEach(Runnable::run));
}
}, FRAME_INTERVAL_MS, FRAME_INTERVAL_MS);
}

private static final Set<Runnable> listeners = new CopyOnWriteArraySet<>();

public static void addListener(Runnable runnable) {
listeners.add(runnable);
}

public static void removeListener(Runnable runnable) {
listeners.remove(runnable);
}
}
24 changes: 24 additions & 0 deletions common/src/main/java/bisq/common/Payload.java
@@ -0,0 +1,24 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.common;

/**
* Interface for objects used inside an Envelope or other Payloads.
*/
public interface Payload extends Proto {
}

0 comments on commit fce7f45

Please sign in to comment.