corrected car control module

This commit is contained in:
MaximZaitsev
2016-07-14 13:16:58 +03:00
parent e6025f6bcb
commit 1b689c6414
3 changed files with 22 additions and 14 deletions
@@ -1,3 +1,4 @@
import client.Client
import io.netty.buffer.Unpooled
import io.netty.handler.codec.http.*
import proto.car.RouteP
@@ -6,17 +7,14 @@ import proto.car.RouteP.Direction.Command
/**
* Created by user on 7/14/16.
*/
class CarControl constructor(host: String, port: Int) {
class CarControl constructor(client: Client) {
val host: String;
val port: Int
val client: Client
init {
this.host = host;
this.port = port;
this.client = client
}
fun executeCommand(direction: Char) {
val directionBuilder = RouteP.Direction.newBuilder()
@@ -38,12 +36,12 @@ class CarControl constructor(host: String, port: Int) {
}
}
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/control", Unpooled.copiedBuffer(directionBuilder.build().toByteArray()));
request.headers().set(HttpHeaderNames.HOST, host)
request.headers().set(HttpHeaderNames.HOST, client.host)
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
request.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8")
client.Client.sendRequest(request, host, port)
client.sendRequest(request)
}
}
+3 -1
View File
@@ -1,3 +1,4 @@
import client.Client
import com.martiansoftware.jsap.FlaggedOption
import com.martiansoftware.jsap.JSAP
import java.util.*
@@ -21,7 +22,8 @@ fun main(args: Array<String>) {
val port = config.getInt("port")
val direction = config.getChar("direction")
val carControl = CarControl(host, port)
val client = Client(host, port)
val carControl = CarControl(client)
if (direction.equals('t', true)) {
initTextInterface(carControl)
} else if (correctDirectionValues.contains(direction)) {
@@ -9,12 +9,20 @@ import java.net.ConnectException
/**
* Created by user on 7/8/16.
*/
object Client {
class Client constructor(host: String, port: Int) {
fun sendRequest(request: HttpRequest, host: String, port: Int): Int {
val group = NioEventLoopGroup()
val host: String
val port: Int
init {
this.host = host
this.port = port
}
fun sendRequest(request: HttpRequest): Int {
val group = NioEventLoopGroup(1)
try {
val bootstrap: Bootstrap = Bootstrap()
val bootstrap = Bootstrap()
bootstrap.group(group).channel(NioSocketChannel().javaClass).handler(ClientInitializer())
val channelFuture = bootstrap.connect(host, port).sync()
val channel = channelFuture.channel()
@@ -24,7 +32,7 @@ object Client {
println("interrupted before request done")
return 2
} catch (e: ConnectException) {
println("connection error to $host:$port")
println("connection error")
return 1
} finally {
group.shutdownGracefully()