Add BaseP2PService

This commit is contained in:
Manfred Karrer 2015-03-20 11:56:14 +01:00
parent 79698df651
commit fa900219b2
6 changed files with 60 additions and 27 deletions

View file

@ -32,7 +32,7 @@ import io.bitsquare.locale.LanguageUtil;
import io.bitsquare.p2p.BootstrapState;
import io.bitsquare.p2p.ClientNode;
import io.bitsquare.p2p.MessageService;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.p2p.BaseP2PService;
import io.bitsquare.persistence.Persistence;
import io.bitsquare.trade.Trade;
import io.bitsquare.trade.TradeManager;
@ -159,7 +159,7 @@ class MainViewModel implements ViewModel {
() -> Platform.runLater(() -> setBitcoinNetworkSyncProgress(1.0)));
// Set executor for all P2PServices
P2PService.setUserThread(Platform::runLater);
BaseP2PService.setUserThread(Platform::runLater);
Observable<BootstrapState> bootstrapStateAsObservable = clientNode.bootstrap(user.getMessageKeyPair());
bootstrapStateAsObservable.publish();

View file

@ -0,0 +1,47 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare 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.
*
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.p2p;
import java.util.concurrent.Executor;
import net.tomp2p.dht.PeerDHT;
public class BaseP2PService implements P2PService {
private static Executor userThread;
public static void setUserThread(Executor userThread) {
BaseP2PService.userThread = userThread;
}
protected Executor executor = userThread;
protected PeerDHT peerDHT;
@Override
public void bootstrapCompleted() {
}
@Override
public void setExecutor(Executor executor) {
this.executor = executor;
}
@Override
public void shutDown() {
}
}

View file

@ -19,26 +19,10 @@ package io.bitsquare.p2p;
import java.util.concurrent.Executor;
import net.tomp2p.dht.PeerDHT;
public interface P2PService {
void bootstrapCompleted();
public class P2PService {
void setExecutor(Executor executor);
private static Executor userThread;
public static void setUserThread(Executor userThread) {
P2PService.userThread = userThread;
}
protected Executor executor = userThread;
protected PeerDHT peerDHT;
public void bootstrapCompleted() {
}
public void setExecutor(Executor executor) {
this.executor = executor;
}
public void shutDown() {
}
void shutDown();
}

View file

@ -47,7 +47,7 @@ import org.slf4j.LoggerFactory;
public class TomP2PAddressService extends TomP2PDHTService implements AddressService {
private static final Logger log = LoggerFactory.getLogger(TomP2PAddressService.class);
private static final int IP_CHECK_PERIOD = 2 * 60 * 1000; // Cheap call if nothing changes, so set it short to 2 min.
private static final int STORE_ADDRESS_PERIOD = 5 * 60 * 1000; // Save every 5 min.
private static final int ADDRESS_TTL = STORE_ADDRESS_PERIOD * 2; // TTL 10 min.
@ -71,6 +71,7 @@ public class TomP2PAddressService extends TomP2PDHTService implements AddressSer
@Override
public void bootstrapCompleted() {
super.bootstrapCompleted();
setupTimerForIPCheck();
setupTimerForStoreAddress();
storeAddress();

View file

@ -38,7 +38,7 @@ public class TomP2PMessageService extends TomP2PService implements MessageServic
private static final Logger log = LoggerFactory.getLogger(TomP2PMessageService.class);
private final CopyOnWriteArrayList<MessageHandler> messageHandlers = new CopyOnWriteArrayList<>();
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
@ -52,6 +52,7 @@ public class TomP2PMessageService extends TomP2PService implements MessageServic
@Override
public void bootstrapCompleted() {
super.bootstrapCompleted();
setupReplyHandler();
}
@ -98,7 +99,7 @@ public class TomP2PMessageService extends TomP2PService implements MessageServic
if (!messageHandlers.remove(listener))
throw new IllegalArgumentException("Try to remove listener which was never added.");
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private

View file

@ -18,7 +18,7 @@
package io.bitsquare.p2p.tomp2p;
import io.bitsquare.p2p.BootstrapState;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.p2p.BaseP2PService;
import javax.inject.Inject;
@ -36,7 +36,7 @@ import rx.Subscriber;
* That way we limit the dependency of the TomP2P library only to that class (and it's sub components).
* <p/>
*/
public class TomP2PService extends P2PService {
public class TomP2PService extends BaseP2PService {
private static final Logger log = LoggerFactory.getLogger(TomP2PService.class);
private final Subscriber<BootstrapState> subscriber;