Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Tinder/Scarlet into vishesh/upgra…
Browse files Browse the repository at this point in the history
…de-gradle-plugins

# Conflicts:
#	build.gradle
#	gradle/wrapper/gradle-wrapper.properties
  • Loading branch information
VisheshVadhera committed Aug 24, 2018
2 parents be26508 + 9df9e76 commit 5986509
Show file tree
Hide file tree
Showing 189 changed files with 630 additions and 348 deletions.
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 2

jobs:
build:
docker:
- image: circleci/android:api-27-alpha
environment:
TERM: dumb
GRADLE_OPTS: '-Dorg.gradle.jvmargs="-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap" -Dorg.gradle.parallel=false -Dorg.gradle.daemon=true'
CIRCLE_TEST_REPORTS: test-reports
resource_class: xlarge
parallelism: 4
steps:
- checkout
- run:
name: Disable PreDexing
command: echo "disablePreDex" >> gradle.properties
- run: if [ -e ./gradlew ]; then ./gradlew dependencies;else gradle dependencies;fi
- run: ./gradlew test
- run: mkdir -p $CIRCLE_TEST_REPORTS/junit/
- run: find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} $CIRCLE_TEST_REPORTS/junit/ \;
112 changes: 62 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
Scarlet
===
[![CircleCI](https://circleci.com/gh/Tinder/Scarlet.svg?style=svg)](https://circleci.com/gh/Tinder/Scarlet)
[![Release](https://jitpack.io/v/tinder/scarlet.svg)](https://jitpack.io/#tinder/scarlet)

A Retrofit inspired WebSocket client for Kotlin, Java, and Android.

This README is still **WIP**. Please see the [tutorial][tutorial] for more information.

Tutorial
---
- [Taming WebSocket with Scarlet][tutorial]
- [A talk][slides] at [Conference for Kotliners][kotliners]

Usage
---
In this example, we are reading the realtime Bitcoin price from [Gdax WebSocket Feed][gdax-websocket-feed].
In this example, we read the realtime Bitcoin price from [Gdax WebSocket Feed][gdax-websocket-feed].
For more information, please check out the [demo app][demo-app].

Declare a WebSocket client using an interface.
Declare a WebSocket client using an interface:

~~~ kotlin
interface GdaxService {
@Receive
fun observeOnConnectionOpenedEvent(): Flowable<WebSocket.Event.OnConnectionOpen<*>>
@Send
fun sendSubscribe(subscribe: Subscribe)
@Receive
fun observeTicker(): Flowable<Ticker>
@Receive
fun observeOnConnectionOpenedEvent(): Flowable<WebSocket.Event.OnConnectionOpen<*>>
}
val gdaxService = scarlet.create<GdaxService>()
~~~

Use Scarlet to create an implementation.
Use Scarlet to create an implementation:

~~~ kotlin
val okHttpClient = OkHttpClient.Builder().build()

val scarletInstance = Scarlet.Builder()
.webSocketFactory(okHttpClient.newWebSocketFactory("wss://ws-feed.gdax.com"))
.addMessageAdapterFactory(MoshiMessageAdapter.Factory())
Expand All @@ -42,7 +40,9 @@ val scarletInstance = Scarlet.Builder()
val gdaxService = scarletInstance.create<GdaxService>()
~~~

Send a `Subscribe` message upon connection open so that the server will start streaming tickers, which contain the latest price.
Send a `Subscribe` message upon connection open and the server will start streaming tickers which contain the latest price.


~~~ kotlin
val BITCOIN_TICKER_SUBSCRIBE_MESSAGE = Subscribe(
productIds = listOf("BTC-USD"),
Expand All @@ -53,64 +53,72 @@ gdaxService.observeOnConnectionOpenedEvent()
.subscribe({
gdaxService.sendSubscribe(BITCOIN_TICKER_SUBSCRIBE_MESSAGE)
})
~~~

Start observing realtime tickers.
~~~ kotlin
gdaxService.observeTicker()
.subscribe({ ticker ->
Log.d("Bitcoin price is ${ticker.price} at ${ticker.time}")
})
~~~

### Built-in Plugins
`WebSocket.Factory`
- `OkHttpClient`
- `MockHttpServer`

`MessageAdapter.Factory`
- moshi
- gson
- protobuf

`StreamAdapter.Factory`
- RxJava2
- RxJava1
### Android
Scarlet is driven by a [StateMachine][state-machine].

`Lifecycle`
- AndroidLifecycle
<img width="600 px" src="/example/scarlet-state-machine.png"/>

`BackoffStrategy`
- Linear
- Exponential
- ExponentialWithJitter

### Android
TODO:
- `AndroidLifecycle`


### State Machine
Scarlet is driven by a [StateMachine](https://github.com/Tinder/StateMachine).
![State Diagram](./example/scarlet-state-machine.png)
TODO

Download
--------
**TODO: make the jar public**
While we are working on Bintray support, Scarlet is available via [JitPack][jitpack].

Download [the latest JAR][latest-jar] or grab via Maven:
##### Maven:
```xml
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.tinder.scarlet</groupId>
<artifactId>scarlet</artifactId>
<version>0.1.0</version>
<groupId>com.github.tinder.scarlet</groupId>
<artifactId>scarlet</artifactId>
<version>0.1.4</version>
</dependency>
```
or Gradle:

##### Gradle:
```groovy
implementation 'com.tinder.scarlet:scarlet:0.1.0'
repositories {
// ...
maven { url "https://jitpack.io" }
}
implementation 'com.github.tinder.scarlet:scarlet:0.1.4'
```

### Plug-in Roadmap
`WebSocket.Factory`
- [x] `OkHttpClient`
- [x] `MockHttpServer`

`MessageAdapter.Factory`
- [x] `moshi`
- [x] `gson`
- [x] `protobuf`
- [ ] `jackson`
- [ ] `simple-xml`

`StreamAdapter.Factory`
- [x] `RxJava2`
- [x] `RxJava1`
- [x] `Kotlin Coroutine`

`Lifecycle`
- [x] `AndroidLifecycle`

`BackoffStrategy`
- [x] `Linear`
- [x] `Exponential`
- [x] `ExponentialWithJitter`

Copyright
---
~~~
Expand Down Expand Up @@ -142,5 +150,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

[gdax-websocket-feed]: https://docs.gdax.com/#websocket-feed
[latest-jar]: https://tinder.jfrog.io/tinder/webapp/#/artifacts/browse/tree/General/libs-release-local/com/tinder/scarlet/scarlet
[demo-app]: https://github.com/Tinder/Scarlet/tree/master/demo/src/main/java/com/tinder/app
[demo-app]: /demo/src/main/java/com/tinder/app
[tutorial]: https://tech.gotinder.com/taming-websocket-with-scarlet/
[slides]: https://speakerdeck.com/zhxnlai/taming-websocket-with-scarlet
[kotliners]: https://www.conferenceforkotliners.com/
[state-machine]: https://github.com/Tinder/StateMachine
[jitpack]: https://jitpack.io/#tinder/scarlet
15 changes: 5 additions & 10 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,23 @@ buildscript {
}

plugins {
id 'org.jlleitschuh.gradle.ktlint' version '2.2.0'
id 'org.jlleitschuh.gradle.ktlint' version '4.1.0'
}

ktlint {
version = '0.9.2'
version = '0.24.0'
}

allprojects {
subprojects {
apply plugin: 'org.kordamp.gradle.stats'
apply plugin: "com.jfrog.artifactory"
group = 'com.tinder.scarlet'
// group = 'com.tinder.scarlet' TODO use Bintray
group = 'com.github.tinder'

repositories {
mavenCentral()
maven { url "https://jitpack.io" }
google()
jcenter()
}
}

clean {
delete rootProject.buildDir
}

apply from: rootProject.file('dependencies.gradle')
3 changes: 1 addition & 2 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

repositories {
mavenCentral()
google()
maven { url 'https://jitpack.io' }
}

android {
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
~ © 2018 Match Group, LLC.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.api
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/com/tinder/app/echo/api/EchoService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.api
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.domain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.domain
Expand All @@ -14,10 +14,9 @@ class AuthStatusRepository @Inject constructor() {

private val authStatusProcessor = BehaviorProcessor.createDefault<AuthStatus>(AuthStatus.LOGGED_IN)

fun getAuthStatus(): AuthStatus = authStatusProcessor.value
fun getAuthStatus(): AuthStatus = authStatusProcessor.value!!

fun observeAuthStatus(): Flowable<AuthStatus> = authStatusProcessor

fun updateAuthStatus(authStatus: AuthStatus) = authStatusProcessor.onNext(authStatus)

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.domain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.domain
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.domain
Expand Down Expand Up @@ -27,5 +27,4 @@ class LoggedInLifecycle constructor(
}
.subscribe(lifecycleRegistry)
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.inject
Expand Down Expand Up @@ -67,5 +67,4 @@ interface EchoBotComponent {
interface ComponentProvider {
val echoBotComponent: EchoBotComponent
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.presenter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.target
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.echo.view
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/com/tinder/app/gdax/api/GdaxService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.gdax.api
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.gdax.api
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.gdax.api.model
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.gdax.api.model
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/java/com/tinder/app/gdax/api/model/Ticker.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* © 2013 - 2018 Tinder, Inc., ALL RIGHTS RESERVED
* © 2018 Match Group, LLC.
*/

package com.tinder.app.gdax.api.model
Expand Down
Loading

0 comments on commit 5986509

Please sign in to comment.