forked from LimeChain/Fruzhin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
269 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 46 additions & 12 deletions
58
src/main/java/com/limechain/network/StrictProtocolBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
package com.limechain.network; | ||
|
||
public abstract class StrictProtocolBinding<T> { | ||
import com.limechain.network.wrapper.Stream; | ||
import org.teavm.jso.JSBody; | ||
import org.teavm.jso.core.JSPromise; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public abstract class StrictProtocolBinding { | ||
String protocolId; | ||
|
||
protected StrictProtocolBinding(String protocolId/*, T protocol*/) { | ||
this.protocolId = protocolId; | ||
} | ||
|
||
public Stream dialPeer(/*PeerId peer*/) { | ||
Object peer1 = getPeer(); | ||
System.out.println("Peer: " + peer1); | ||
JSPromise<Object> dial = dial(peer1, protocolId); | ||
final var lock = new Object(); | ||
AtomicReference<Stream> stream = new AtomicReference<>(); | ||
|
||
dial.then((result) -> { | ||
stream.set((Stream) result); | ||
synchronized (lock) { | ||
lock.notify(); | ||
} | ||
return null; | ||
}); | ||
|
||
synchronized (lock) { | ||
try { | ||
lock.wait(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
Stream atomicStream = stream.get(); | ||
|
||
System.out.println("Stream: " + toJson(atomicStream)); | ||
return atomicStream; | ||
} | ||
|
||
// public T dialPeer(Host us, PeerId peer, AddressBook addrs) { | ||
// Multiaddr[] addr = addrs.get(peer) | ||
// .join().stream() | ||
// .filter(address -> !address.toString().contains("/ws") && !address.toString().contains("/wss")) | ||
// .toList() | ||
// .toArray(new Multiaddr[0]); | ||
// if (addr.length == 0) | ||
// throw new IllegalStateException("No addresses known for peer " + peer); | ||
// | ||
// return dial(us, peer, addr).getController().join(); | ||
// } | ||
@JSBody(params = {"object"}, script = "return JSON.stringify(object);") | ||
private static native String toJson(Object object); | ||
|
||
@JSBody(params = {"peerId", "protocolId"}, script = "return (async () => ItPbStream.pbStream(await libp.dialProtocol(peerId, protocolId)))()") | ||
private static native JSPromise<Object> dial(Object peerId, String protocolId); | ||
|
||
@JSBody(script = "return libp.getConnections()[0].remotePeer;") | ||
private static native Object getPeer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 24 additions & 7 deletions
31
src/main/java/com/limechain/network/protocol/warp/WarpSync.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,44 @@ | ||
package com.limechain.network.protocol.warp; | ||
|
||
import com.limechain.exception.global.ExecutionFailedException; | ||
import com.limechain.exception.global.ThreadInterruptedException; | ||
import com.limechain.network.StrictProtocolBinding; | ||
import com.limechain.network.kad.dto.Host; | ||
import com.limechain.network.kad.dto.PeerId; | ||
import com.limechain.network.protocol.warp.dto.WarpSyncRequest; | ||
import com.limechain.network.protocol.warp.dto.WarpSyncResponse; | ||
import com.limechain.network.wrapper.Stream; | ||
import lombok.extern.java.Log; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class WarpSync extends StrictProtocolBinding<WarpSyncController> { | ||
public class WarpSync extends StrictProtocolBinding { | ||
|
||
private String protocolId; | ||
|
||
public WarpSync(String protocolId, WarpSyncProtocol protocol) { | ||
super(protocolId/*, protocol*/); | ||
this.protocolId = protocolId; | ||
} | ||
|
||
// public WarpSyncResponse warpSyncRequest(Host us, PeerId peer, String blockHash) { | ||
public WarpSyncResponse warpSyncRequest(/*PeerId peer,*/ String blockHash) { | ||
// try { | ||
// WarpSyncController controller = dialPeer(us, peer, us.getAddressBook()); | ||
// WarpSyncResponse resp = controller.warpSyncRequest(blockHash).get(10, TimeUnit.SECONDS); | ||
// log.log(Level.INFO, "Received warp sync response with " + resp.getFragments().length + " fragments"); | ||
// return resp; | ||
// Stream stream = dialPeer(/*peer*/); | ||
WarpSyncProtocol.Sender sender = new WarpSyncProtocol.Sender(); | ||
System.out.println("Block hash: " + blockHash); | ||
WarpSyncResponse resp = sender.warpSyncRequest(blockHash, protocolId); | ||
log.log(Level.INFO, "Received warp sync response with " + resp/*.getFragments().length*/ + " fragments"); | ||
return resp; | ||
// } catch (ExecutionException | TimeoutException | IllegalStateException e) { | ||
// log.log(Level.SEVERE, "Error while sending remote call request: ", e); | ||
// throw new ExecutionFailedException(e); | ||
// } catch (InterruptedException e) { | ||
// Thread.currentThread().interrupt(); | ||
// throw new ThreadInterruptedException(e); | ||
// } | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.