diff --git a/scarlet-protocol-stomp/build.gradle b/scarlet-protocol-stomp/build.gradle index 239d8406..88a730b0 100644 --- a/scarlet-protocol-stomp/build.gradle +++ b/scarlet-protocol-stomp/build.gradle @@ -8,7 +8,6 @@ apply plugin: 'java-library' dependencies { api project(':scarlet-core') - implementation files('libs/gozirra-client-0.4.1.jar') implementation project(':scarlet-core-internal') implementation rootProject.ext.rxJava diff --git a/scarlet-protocol-stomp/src/main/java/com/tinder/scarlet/stomp/GozirraStompClient.kt b/scarlet-protocol-stomp/src/main/java/com/tinder/scarlet/stomp/GozirraStompClient.kt deleted file mode 100644 index f09961dd..00000000 --- a/scarlet-protocol-stomp/src/main/java/com/tinder/scarlet/stomp/GozirraStompClient.kt +++ /dev/null @@ -1,195 +0,0 @@ -/* - * © 2018 Match Group, LLC. - */ - -package com.tinder.scarlet.stomp - -import com.tinder.scarlet.Channel -import com.tinder.scarlet.Message -import com.tinder.scarlet.MessageQueue -import com.tinder.scarlet.Protocol -import com.tinder.scarlet.ProtocolSpecificEventAdapter -import com.tinder.scarlet.utils.SimpleChannelFactory -import com.tinder.scarlet.utils.SimpleProtocolOpenRequestFactory -import net.ser1.stomp.Client -import javax.security.auth.login.LoginException - -class GozirraStompClient( - private val openRequestFactory: RequestFactory -) : Protocol { - - override fun createChannelFactory(): Channel.Factory { - return SimpleChannelFactory { listener, _ -> - StompMainChannel(listener) - } - } - - override fun createOpenRequestFactory(channel: Channel): Protocol.OpenRequest.Factory { - return SimpleProtocolOpenRequestFactory { - openRequestFactory.createClientOpenRequest() - } - } - - override fun createEventAdapterFactory(): ProtocolSpecificEventAdapter.Factory { - return object : ProtocolSpecificEventAdapter.Factory {} - } - - interface RequestFactory { - fun createClientOpenRequest(): ClientOpenRequest - } - - open class SimpleRequestFactory( - private val createClientOpenRequestCallable: () -> ClientOpenRequest - ) : RequestFactory { - override fun createClientOpenRequest(): ClientOpenRequest { - return createClientOpenRequestCallable() - } - } - - data class ClientOpenRequest( - val url: String, - val port: Int, - val login: String, - val password: String - ) : Protocol.OpenRequest - - data class MessageMetaData(val headers: Map) : Protocol.MessageMetaData -} - -class GozirraStompDestination( - val destination: String, - private val openRequestFactory: RequestFactory -) : Protocol { - - override fun createChannelFactory(): Channel.Factory { - return SimpleChannelFactory { listener, parent -> - require(parent is StompMainChannel) - StompMessageChannel(parent as StompMainChannel, destination, listener) - } - } - - override fun createOpenRequestFactory(channel: Channel): Protocol.OpenRequest.Factory { - return SimpleProtocolOpenRequestFactory { - DestinationOpenRequest( - openRequestFactory.createDestinationOpenRequestHeader(destination) - ) - } - } - - override fun createEventAdapterFactory(): ProtocolSpecificEventAdapter.Factory { - return object : ProtocolSpecificEventAdapter.Factory {} - } - - interface RequestFactory { - fun createDestinationOpenRequestHeader(destination: String): Map - } - - open class SimpleRequestFactory( - private val createDestinationOpenRequestHeaderCallable: (String) -> Map - ) : RequestFactory { - - override fun createDestinationOpenRequestHeader(destination: String): Map { - return createDestinationOpenRequestHeaderCallable(destination) - } - } - - data class DestinationOpenRequest( - val headers: Map - ) : Protocol.OpenRequest -} - -class StompMainChannel( - private val listener: Channel.Listener -) : Channel { - var client: Client? = null - - override fun open(openRequest: Protocol.OpenRequest) { - val (url, port, login, password) = openRequest as GozirraStompClient.ClientOpenRequest - try { - val client = Client(url, port, login, password) - client.addErrorListener { _, _ -> - listener.onFailed(this, true, null) - } - this.client = client - listener.onOpened(this) - } catch (e: LoginException) { - listener.onFailed(this, true, e) - } catch (e: Throwable) { - listener.onFailed(this, true, e) - } - } - - override fun close(closeRequest: Protocol.CloseRequest) { - forceClose() - } - - override fun forceClose() { - try { - client?.disconnect() - client = null - listener.onClosed(this) - } catch (e: LoginException) { - listener.onFailed(this, true, e) - } catch (e: Throwable) { - listener.onFailed(this, true, e) - } - } - - override fun createMessageQueue(listener: MessageQueue.Listener): MessageQueue? { - return null - } -} - -class StompMessageChannel( - private val mainChannel: StompMainChannel, - private val destination: String, - private val listener: Channel.Listener -) : Channel, MessageQueue { - - private var client: Client? = null - private var messageQueueListener: MessageQueue.Listener? = null - - override fun open(openRequest: Protocol.OpenRequest) { - val destinationOpenRequest = openRequest as GozirraStompDestination.DestinationOpenRequest - client = mainChannel.client - client?.subscribe( - destination, - { headers, message -> - messageQueueListener?.onMessageReceived( - this, - this, - Message.Text(message), - GozirraStompClient.MessageMetaData(headers as Map) - ) - }, - // This map is modified by gozirra internally - destinationOpenRequest.headers.toMutableMap() - ) - listener.onOpened(this) - } - - override fun close(closeRequest: Protocol.CloseRequest) { - forceClose() - } - - override fun forceClose() { - client?.unsubscribe(destination) - client = null - listener.onClosed(this) - } - - override fun createMessageQueue(listener: MessageQueue.Listener): MessageQueue { - require(messageQueueListener == null) - messageQueueListener = listener - return this - } - - override fun send(message: Message, messageMetaData: Protocol.MessageMetaData): Boolean { - val client = client ?: return false - when (message) { - is Message.Text -> client.send(destination, message.value) - is Message.Bytes -> throw IllegalArgumentException("Bytes are not supported") - } - return true - } -}