-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
121 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
syntax = "proto3"; | ||
|
||
package kr.ac.postech.paranode.rpc; | ||
|
||
import "common.proto"; | ||
|
||
service Exchange { | ||
rpc Save (SaveRequest) returns (SaveReply) {} | ||
} | ||
|
||
|
||
message SaveRequest { | ||
bytes block = 1; | ||
} | ||
|
||
message SaveReply {} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package kr.ac.postech.paranode.rpc | ||
|
||
import io.grpc.ManagedChannel | ||
import io.grpc.ManagedChannelBuilder | ||
import kr.ac.postech.paranode.core.Block | ||
import kr.ac.postech.paranode.rpc.exchange.ExchangeGrpc.ExchangeStub | ||
import kr.ac.postech.paranode.rpc.exchange.SaveReply | ||
import kr.ac.postech.paranode.rpc.exchange.SaveRequest | ||
|
||
import java.util.concurrent.TimeUnit | ||
import scala.concurrent.Future | ||
|
||
import Implicit._ | ||
|
||
object ExchangeClient { | ||
def apply(host: String, port: Int): ExchangeClient = { | ||
val channel = | ||
ManagedChannelBuilder.forAddress(host, port).usePlaintext().build | ||
val stub = exchange.ExchangeGrpc.stub(channel) | ||
new ExchangeClient(channel, stub) | ||
} | ||
} | ||
|
||
class ExchangeClient private ( | ||
private val channel: ManagedChannel, | ||
private val stub: ExchangeStub | ||
) { | ||
def shutdown(): Unit = { | ||
channel.shutdown.awaitTermination(5, TimeUnit.SECONDS) | ||
} | ||
|
||
def save(block: Block): Future[SaveReply] = { | ||
val request = SaveRequest(block) | ||
|
||
stub.save(request) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package kr.ac.postech.paranode.rpc | ||
|
||
import kr.ac.postech.paranode.core.Block | ||
import org.apache.logging.log4j.scala.Logging | ||
|
||
import java.util.UUID | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
import scala.concurrent.Promise | ||
import scala.reflect.io.Directory | ||
|
||
import exchange.{ExchangeGrpc, SaveReply, SaveRequest} | ||
import Implicit._ | ||
|
||
class ExchangeService( | ||
executionContext: ExecutionContext, | ||
outputDirectory: Directory | ||
) extends ExchangeGrpc.Exchange | ||
with Logging { | ||
override def save(request: SaveRequest): Future[SaveReply] = { | ||
val promise = Promise[SaveReply] | ||
|
||
Future { | ||
logger.info(s"[ExchangeServer] Save ($request)") | ||
|
||
val block: Block = request.block | ||
|
||
val path = outputDirectory / UUID.randomUUID().toString | ||
|
||
logger.info(s"[ExchangeServer] Writing block to $path") | ||
|
||
block.writeTo(path) | ||
|
||
logger.info(s"[ExchangeServer] Block written to $path") | ||
|
||
promise.success(new SaveReply()) | ||
}(executionContext) | ||
|
||
promise.future | ||
} | ||
} |
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