created test server project

This commit is contained in:
MaximZaitsev
2016-07-06 19:07:17 +03:00
parent 1be4ad3732
commit 5f2dfa4c99
20 changed files with 281 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
.idea/workspace.xml
out
+1
View File
@@ -0,0 +1 @@
Server
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
+3
View File
@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>
+12
View File
@@ -0,0 +1,12 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime.jar!/" />
<root url="jar://$PROJECT_DIR$/lib/kotlin-reflect.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime-sources.jar!/" />
</SOURCES>
</library>
</component>
+11
View File
@@ -0,0 +1,11 @@
<component name="libraryTable">
<library name="netty-all-4.1.2.Final-sources">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/netty-all-4.1.2.Final.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$PROJECT_DIR$/lib/netty-all-4.1.2.Final-sources.jar!/" />
</SOURCES>
</library>
</component>
+57
View File
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ClientPropertiesManager">
<properties class="javax.swing.AbstractButton">
<property name="hideActionText" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JComponent">
<property name="html.disable" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JEditorPane">
<property name="JEditorPane.w3cLengthUnits" class="java.lang.Boolean" />
<property name="JEditorPane.honorDisplayProperties" class="java.lang.Boolean" />
<property name="charset" class="java.lang.String" />
</properties>
<properties class="javax.swing.JList">
<property name="List.isFileList" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JPasswordField">
<property name="JPasswordField.cutCopyAllowed" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JSlider">
<property name="Slider.paintThumbArrowShape" class="java.lang.Boolean" />
<property name="JSlider.isFilled" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTable">
<property name="Table.isFileList" class="java.lang.Boolean" />
<property name="JTable.autoStartsEdit" class="java.lang.Boolean" />
<property name="terminateEditOnFocusLost" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JToolBar">
<property name="JToolBar.isRollover" class="java.lang.Boolean" />
</properties>
<properties class="javax.swing.JTree">
<property name="JTree.lineStyle" class="java.lang.String" />
</properties>
<properties class="javax.swing.text.JTextComponent">
<property name="caretAspectRatio" class="java.lang.Double" />
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/carEmulator/carEmulator.iml" filepath="$PROJECT_DIR$/carEmulator/carEmulator.iml" />
<module fileurl="file://$PROJECT_DIR$/server.iml" filepath="$PROJECT_DIR$/server.iml" />
</modules>
</component>
</project>
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
+7
View File
@@ -0,0 +1,7 @@
/**
* Created by user on 7/6/16.
*/
fun main(args: Array<String>) {
println("test")
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="netty-all-4.1.2.Final-sources" level="project" />
</component>
</module>
+14
View File
@@ -0,0 +1,14 @@
import Server.Server
/**
* Created by user on 7/6/16.
*/
val port:Int = 7925;
fun main(args: Array<String>) {
println("server started")
val server = Server(port)
val serverThread = Thread(server);
serverThread.start()
}
+42
View File
@@ -0,0 +1,42 @@
package Server
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
import io.netty.handler.logging.LogLevel
import io.netty.handler.logging.LoggingHandler
/**
* Created by user on 7/6/16.
*/
class Server : Runnable {
val port: Int;
constructor(port: Int) {
this.port = port
}
override fun run() {
val bossGroup = NioEventLoopGroup(1)
val workerGroup = NioEventLoopGroup()
val b = ServerBootstrap()
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel().javaClass)
.handler(LoggingHandler(LogLevel.INFO))
.childHandler(ServerInitializer())
try {
val channel = b.bind(port).sync().channel()
channel.closeFuture().sync()
} catch (e: InterruptedException) {
e.printStackTrace()
//todo
} finally {
bossGroup.shutdownGracefully()
workerGroup.shutdownGracefully()
}
}
}
+50
View File
@@ -0,0 +1,50 @@
package Server
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.codec.http.*
import io.netty.util.CharsetUtil
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
/**
* Created by user on 7/6/16.
*/
class ServerHandler : SimpleChannelInboundHandler<Any>() {
val sb: StringBuilder = StringBuilder();
object static {
val stf = SimpleDateFormat("HH-mm-ss");
}
override fun channelReadComplete(ctx: ChannelHandlerContext) {
val dateTime = ServerHandler.static.stf.format(Date(System.currentTimeMillis()))
val fileName: String = "request_$dateTime.log";
val response = DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes())
ctx.writeAndFlush(response)
writeToFile(sb.toString(), fileName);
}
override fun channelRead0(ctx: ChannelHandlerContext, msg: Any) {
if (msg is HttpRequest) {
sb.append("" + msg.method() + " " + msg.protocolVersion() + "\n")
for (header in msg.headers()) {
sb.append(header.key + ":" + header.value + "\n")
}
}
if (msg is DefaultHttpContent) {
sb.append(msg.content().toString(CharsetUtil.UTF_8))
}
}
fun writeToFile(toFile: String, fileName: String) {
val f = File(fileName)
if (!f.exists()) f.createNewFile()
f.writeText(toFile);
}
}
+26
View File
@@ -0,0 +1,26 @@
package Server
import Server.ServerHandler
import io.netty.channel.ChannelInitializer
import io.netty.channel.ChannelPipeline
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.HttpRequestDecoder
import io.netty.handler.codec.http.HttpResponseEncoder
/**
* Created by user on 7/6/16.
*/
class ServerInitializer : ChannelInitializer<SocketChannel> {
constructor() {
}
override fun initChannel(channel: SocketChannel) {
println("init")
val p: ChannelPipeline = channel.pipeline()
p.addLast(HttpRequestDecoder())
p.addLast(HttpResponseEncoder())
p.addLast(ServerHandler())
}
}