generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert drive-kotlin to an agent plugin.
$ ftl serve ./drive-kotlin/src/main/kotlin/com/squareup/ftldemo ... $ ftl list com.squareup.ftldemo.notifyCustomer com.squareup.ftldemo.makePizza com.squareup.ftldemo.pay $ ftl call com.squareup.ftldemo.notifyCustomer '{"recipient": "Dhanji"}' {"message":"OK"}
- Loading branch information
1 parent
4cbdf3f
commit 619dd2a
Showing
12 changed files
with
271 additions
and
32 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
61 changes: 61 additions & 0 deletions
61
drive-kotlin/src/main/kotlin/xyz/block/ftl/control/ControlChannelServer.kt
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,61 @@ | ||
package xyz.block.ftl.control | ||
|
||
import com.google.gson.Gson | ||
import com.google.protobuf.ByteString | ||
import io.grpc.Status | ||
import io.grpc.netty.NettyServerBuilder | ||
import xyz.block.ftl.Context | ||
import xyz.block.ftl.drive.verb.VerbDeck | ||
import xyz.block.ftl.v1.* | ||
import java.net.SocketAddress | ||
|
||
|
||
class ControlChannelServer(val deck: VerbDeck) : DriveServiceGrpcKt.DriveServiceCoroutineImplBase() { | ||
private val gson = Gson() | ||
|
||
override suspend fun ping(request: PingRequest): PingResponse { | ||
return PingResponse.getDefaultInstance() | ||
} | ||
|
||
override suspend fun call(request: CallRequest): CallResponse { | ||
val cassette = deck.lookupFullyQualifiedName(request.verb) ?: throw Status.NOT_FOUND.asException() | ||
val req = gson.fromJson<Any>(request.body.toStringUtf8(), cassette.argumentType.java) | ||
var resp: Any | ||
try { | ||
resp = cassette.dispatch(Context(http = null), req) // TODO: do something with Context | ||
} catch (e: Exception) { | ||
return CallResponse.newBuilder() | ||
.setError( | ||
CallResponse.Error.newBuilder() | ||
.setMessage(e.message ?: "no error message") | ||
.build() | ||
) | ||
.build() | ||
} | ||
return CallResponse.newBuilder() | ||
.setBody(ByteString.copyFromUtf8(gson.toJson(resp))) | ||
.build() | ||
} | ||
|
||
override suspend fun list(request: ListRequest): ListResponse { | ||
return ListResponse.newBuilder() | ||
.addAllVerbs(deck.list().map { deck.fullyQualifiedName(it) }) | ||
.build() | ||
} | ||
|
||
override suspend fun fileChange(request: FileChangeRequest): FileChangeResponse { | ||
return FileChangeResponse.getDefaultInstance() | ||
} | ||
} | ||
|
||
/** | ||
* Start DriveService on the given socket. | ||
*/ | ||
fun startControlChannelServer(socket: SocketAddress, controlChannel: ControlChannelServer) { | ||
val server = NettyServerBuilder | ||
.forAddress(socket) | ||
.addService(controlChannel) | ||
.build() | ||
// TODO: Terminate the process if this fails to startup. | ||
server.start() | ||
} |
24 changes: 24 additions & 0 deletions
24
drive-kotlin/src/main/kotlin/xyz/block/ftl/control/Socket.kt
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,24 @@ | ||
package xyz.block.ftl.control | ||
|
||
import java.net.InetSocketAddress | ||
import java.net.SocketAddress | ||
import java.net.UnixDomainSocketAddress | ||
|
||
/** | ||
* Parses a socket URI in the form unix://PATH or tcp://HOST:PORT | ||
*/ | ||
fun parseSocket(socket: String): SocketAddress { | ||
val schema = socket.split("://", limit = 2) | ||
return when (schema[0]) { | ||
// TODO(aat) Remove this. | ||
// Unix sockets are effectively unusable in gRPC, unfortunately, as | ||
// they require either kqueue or epoll native implementations | ||
"unix" -> UnixDomainSocketAddress.of(schema[1]) | ||
"tcp" -> { | ||
val hostPort = schema[1].split(":", limit = 2) | ||
return InetSocketAddress(hostPort[0], hostPort[1].toInt()) | ||
} | ||
|
||
else -> throw RuntimeException("unsupported socket type ${schema[0]}") | ||
} | ||
} |
Oops, something went wrong.