Skip to content

Commit d043966

Browse files
authored
Merge pull request #28 from BashlikovV/develop
Develop
2 parents 380e46d + 5b523e5 commit d043966

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

messenger.db

0 Bytes
Binary file not shown.

src/main/kotlin/Repository.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object Repository {
22

33
// const val IP_ADDRESS = "192.168.100.7"
44
const val PORT = 8080
5-
const val IP_ADDRESS = "192.168.237.112"
5+
const val IP_ADDRESS = "192.168.87.112"
66

77
// const val IP_ADDRESS = "172.17.96.89"
88

src/main/kotlin/model/messages/SQLiteMessagesRepository.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ class SQLiteMessagesRepository(
6969
}
7070

7171
override fun addImage(body: AddImageRequestBody): AddImageResponseBody {
72+
val fileName = if (body.owner.decodeToString().contains("@")) {
73+
"${body.owner.decodeToString()}.jpg"
74+
} else {
75+
"image${messengerRepository.getMaxId() + 1}.jpg"
76+
}
77+
7278
return try {
73-
val fileName = if (body.owner.decodeToString().contains("@")) {
74-
"${body.owner.decodeToString()}.jpg"
75-
} else {
76-
"image${messengerRepository.getMaxId() + 1}.jpg"
77-
}
7879
messengerRepository.addImage(fileName)
7980

8081
val file = File(
@@ -100,7 +101,7 @@ class SQLiteMessagesRepository(
100101
AddImageResponseBody(fileName.encodeToByteArray())
101102
} catch (e: Exception) {
102103
e.printStackTrace()
103-
AddImageResponseBody("".encodeToByteArray())
104+
AddImageResponseBody(fileName.encodeToByteArray())
104105
}
105106
}
106107

src/main/kotlin/server/HttpRequest.kt

+1-7
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ class HttpRequest(
4040
this.headers = Collections.unmodifiableMap(map)
4141

4242
this.body = if (parts.size > 1) {
43-
try {
44-
String(
45-
parts.last().toByteArray().filter { it != 0.toByte() }.toByteArray()
46-
)
47-
} catch (_: Exception) {
48-
""
49-
}
43+
String(parts.last().toByteArray())
5044
} else {
5145
""
5246
}

src/main/kotlin/server/Server.kt

+26-15
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package server
22

33
import Repository
44
import database.SQLiteContract
5-
import okio.ByteString.Companion.toByteString
65
import java.io.ByteArrayOutputStream
76
import java.io.IOException
87
import java.net.InetSocketAddress
98
import java.net.ServerSocket
109
import java.net.Socket
11-
import java.nio.charset.StandardCharsets
1210
import java.util.concurrent.ExecutionException
1311
import java.util.concurrent.TimeoutException
12+
import java.util.concurrent.atomic.AtomicInteger
1413

1514
class Server(
1615
private val ip: String = Repository.IP_ADDRESS,
@@ -21,6 +20,8 @@ class Server(
2120

2221
private lateinit var server : ServerSocket
2322

23+
private var count = AtomicInteger(0)
24+
2425
companion object {
2526
private const val BUFFER_SIZE = 256
2627
}
@@ -31,7 +32,7 @@ class Server(
3132
server.bind(InetSocketAddress(ip, port))
3233
while (true) {
3334
val socket = server.accept()
34-
ClientHandler(socket)
35+
ClientHandler(socket).start()
3536
}
3637
} catch (e: IOException) {
3738
e.printStackTrace()
@@ -49,7 +50,7 @@ class Server(
4950
) : Thread() {
5051

5152
init {
52-
start()
53+
count.incrementAndGet()
5354
}
5455

5556
override fun run() {
@@ -62,42 +63,50 @@ class Server(
6263
while (!socket.isClosed) {
6364
val buffer = ByteArray(BUFFER_SIZE)
6465
val builder = StringBuilder()
66+
val clearInput = ByteArrayOutputStream()
6567
var keepReading = true
6668

69+
// while (keepReading) {
70+
// val readResult = inputStream.read(buffer)
71+
//
72+
// keepReading = readResult == BUFFER_SIZE
73+
// val charBuffer = StandardCharsets.UTF_8.decode(buffer.toByteString().asByteBuffer())
74+
// builder.append(charBuffer)
75+
// buffer.fill(0)
76+
// }
77+
6778
while (keepReading) {
6879
val readResult = inputStream.read(buffer)
80+
clearInput.write(buffer, 0, readResult)
6981

7082
keepReading = readResult == BUFFER_SIZE
71-
val charBuffer = StandardCharsets.UTF_8.decode(buffer.toByteString().asByteBuffer())
72-
builder.append(charBuffer)
73-
buffer.fill(0)
7483
}
84+
val charBuffer = clearInput.toByteArray().decodeToString()
85+
// val charBuffer = StandardCharsets.UTF_8.decode(clearInput.toByteArray().toByteString().asByteBuffer())
86+
builder.append(charBuffer)
87+
clearInput.reset()
7588

7689
if (builder.toString().contains("multipart/mixed")) {
7790
val time = System.currentTimeMillis()
7891
val tmp = HttpRequest(builder.toString(), true)
7992
val size = tmp.headers["Content-Length"]!!.toInt() - tmp.length
8093
val bytes = ByteArray(262144)
81-
val value = ByteArrayOutputStream()
8294
var readSize = 0
8395
while (readSize < size) {
8496
val count = inputStream.read(bytes)
8597
if (count > 0) {
86-
value.write(bytes, 0, count)
98+
clearInput.write(bytes, 0, count)
8799
readSize += count
88100
}
89101
}
90102
builder.apply {
91-
append(value.toByteArray().decodeToString().substringBeforeLast("}"))
103+
append(clearInput.toByteArray().decodeToString().substringBeforeLast("}"))
92104
append("}")
93105
}
94106
println(System.currentTimeMillis() - time)
95107
}
96108

97109
val str = builder.toString()
98-
if (str == ByteArray(BUFFER_SIZE, init = { 0 }).decodeToString()) {
99-
continue
100-
}
101110
val request = HttpRequest(str, false)
102111
val response = HttpResponse(databaseUrl)
103112

@@ -112,7 +121,7 @@ class Server(
112121
e.printStackTrace()
113122
} finally {
114123
stream.run {
115-
flush()
124+
// flush()
116125
close()
117126
}
118127
this.interrupt()
@@ -143,10 +152,12 @@ class Server(
143152
if (request.method != HttpMethod.GET) {
144153
stream.write(response.getBytes())
145154
}
146-
stream.flush()
155+
// stream.flush()
147156
stream.close()
148157
socket.close()
149158
}
159+
count.decrementAndGet()
160+
println(count.get())
150161
this.interrupt()
151162
}
152163
}

0 commit comments

Comments
 (0)