-
Notifications
You must be signed in to change notification settings - Fork 59
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
12 changed files
with
572 additions
and
28 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
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 com.m3u.data.net.jetty | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
import org.zeromq.SocketType | ||
import org.zeromq.ZContext | ||
import org.zeromq.ZMQ | ||
|
||
class JettyClient(private val port: Int) { | ||
private val context: ZContext by lazy { ZContext() } | ||
private var socket: ZMQ.Socket? = null | ||
private val flow = MutableSharedFlow<String>() | ||
private val coroutineScope = CoroutineScope(Dispatchers.IO) | ||
|
||
fun start() { | ||
socket?.disconnect("tcp://localhost:$port") | ||
socket = context.createSocket(SocketType.REQ).apply { | ||
connect("tcp://localhost:$port") | ||
while (!Thread.currentThread().isInterrupted) { | ||
val reply: ByteArray = recv(0) | ||
coroutineScope.launch { | ||
flow.emit(String(reply, ZMQ.CHARSET)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun observe(): SharedFlow<String> = flow.asSharedFlow() | ||
|
||
fun send(body: String) { | ||
socket?.send(body) | ||
} | ||
|
||
fun send(bytes: ByteArray) { | ||
socket?.send(bytes) | ||
} | ||
} |
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,5 @@ | ||
package com.m3u.data.net.jetty | ||
|
||
object JettyContracts { | ||
const val PORT = 2239 | ||
} |
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 com.m3u.data.net.jetty | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.launch | ||
import org.zeromq.SocketType | ||
import org.zeromq.ZContext | ||
import org.zeromq.ZMQ | ||
|
||
class JettyServer(private val port: Int) { | ||
private val context: ZContext by lazy { ZContext() } | ||
private var socket: ZMQ.Socket? = null | ||
private val flow = MutableSharedFlow<String>() | ||
private val coroutineScope = CoroutineScope(Dispatchers.IO) | ||
|
||
fun start() { | ||
socket?.unbind("tcp://*:$port") | ||
socket = context.createSocket(SocketType.REP).apply { | ||
bind("tcp://*:$port") | ||
while (!Thread.currentThread().isInterrupted) { | ||
val reply = recv(0) | ||
coroutineScope.launch { | ||
flow.emit(String(reply, ZMQ.CHARSET)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun observe(): SharedFlow<String> = flow.asSharedFlow() | ||
|
||
fun send(body: String) { | ||
socket?.send(body) | ||
} | ||
|
||
fun send(bytes: ByteArray) { | ||
socket?.send(bytes) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
data/src/main/java/com/m3u/data/net/jetty/di/JettyModule.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,22 @@ | ||
package com.m3u.data.net.jetty.di | ||
|
||
import com.m3u.data.net.jetty.JettyClient | ||
import com.m3u.data.net.jetty.JettyContracts | ||
import com.m3u.data.net.jetty.JettyServer | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object JettyModule { | ||
@Provides | ||
@Singleton | ||
fun provideJettyServer(): JettyServer = JettyServer(JettyContracts.PORT) | ||
|
||
@Provides | ||
@Singleton | ||
fun provideJettyClient(): JettyClient = JettyClient(JettyContracts.PORT) | ||
} |
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
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
Oops, something went wrong.