[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake-mobile/master] Added retrofit related interfaces, services



commit cc9b845b8f4e7b175016c3c13009da7649a68f79
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Tue Jun 9 14:23:56 2020 +0530

    Added retrofit related interfaces, services
---
 .../torproject/snowflake/MyPersistentService.java  | 12 +++++--
 .../org/torproject/snowflake/pojo/AnsResponse.java | 27 ++++++++++++++
 .../org/torproject/snowflake/pojo/AnswerBody.java  | 41 +++++++++++++++++++++
 .../torproject/snowflake/pojo/AnswerBodySDP.java   | 35 ++++++++++++++++++
 .../snowflake/pojo/OfferRequestBody.java           | 20 +++++++++++
 .../snowflake/pojo/SDPOfferResponse.java           | 42 ++++++++++++++++++++++
 .../snowflake/services/GetOfferService.java        | 15 ++++++++
 .../snowflake/services/RetroServiceGenerator.java  | 36 +++++++++++++++++++
 .../snowflake/services/SendAnswerService.java      | 15 ++++++++
 9 files changed, 240 insertions(+), 3 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
index 2a7c52d..4484a72 100644
--- a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
+++ b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
@@ -14,6 +14,8 @@ import androidx.annotation.Nullable;
 
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
 import org.torproject.snowflake.interfaces.PeerConnectionObserverCallback;
+import org.torproject.snowflake.pojo.OfferRequestBody;
+import org.torproject.snowflake.pojo.SDPOfferResponse;
 import org.webrtc.DataChannel;
 import org.webrtc.PeerConnection;
 import org.webrtc.PeerConnectionFactory;
@@ -23,6 +25,10 @@ import java.nio.charset.Charset;
 import java.util.LinkedList;
 import java.util.List;
 
+import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
+import io.reactivex.rxjava3.core.Observable;
+import io.reactivex.rxjava3.schedulers.Schedulers;
+
 /**
  * Main Snowflake implementation of foreground service to relay data in the background.
  */
@@ -181,7 +187,7 @@ public class MyPersistentService extends Service {
     private void startWebRTCConnection() {
         initializePeerConnectionFactory(); //Android Specific, you can Ignore.
         mainPeerConnection = createPeerConnection(factory); //Creating New Peer Connection.
-        //TODO: Fetch Offer from broker.
+        //TODO: Fetch offer from broker
     }
 
     /**
@@ -239,7 +245,7 @@ public class MyPersistentService extends Service {
             @Override
             public void iceConnectionFailed() {
                 Log.d(TAG, "iceConnectionFailed: ");
-                //Figuring out with trac ticket.
+                //TODO: Figuring out with trac ticket.
             }
 
             @Override
@@ -251,5 +257,5 @@ public class MyPersistentService extends Service {
         Log.d(TAG, "createPeerConnection: Finished creating peer connection.");
         return factory.createPeerConnection(rtcConfiguration, pcObserver);
     }
-    /////////////////////////////////////
+    /////////////// Network Calls ////////////////////////
 }
diff --git a/app/src/main/java/org/torproject/snowflake/pojo/AnsResponse.java b/app/src/main/java/org/torproject/snowflake/pojo/AnsResponse.java
new file mode 100644
index 0000000..9e5d8ac
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/pojo/AnsResponse.java
@@ -0,0 +1,27 @@
+package org.torproject.snowflake.pojo;
+
+import com.google.gson.annotations.SerializedName;
+
+/*If the client retrieved the answer: HTTP 200 OK
+
+        {
+        Status: "success"
+        }
+If the client left: HTTP 200 OK
+        {
+        Status: "client gone"
+        }
+3) If the request is malformed: HTTP 400 BadRequest*/
+
+public class AnsResponse {
+    @SerializedName("Status")
+    private String status;
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+}
diff --git a/app/src/main/java/org/torproject/snowflake/pojo/AnswerBody.java b/app/src/main/java/org/torproject/snowflake/pojo/AnswerBody.java
new file mode 100644
index 0000000..d657880
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/pojo/AnswerBody.java
@@ -0,0 +1,41 @@
+package org.torproject.snowflake.pojo;
+
+import com.google.gson.annotations.SerializedName;
+
+/*{
+  Sid: [generated session id of proxy],
+  Version: 1.1,
+  Answer:
+  {
+    type: answer,
+    sdp: [WebRTC SDP]
+ }*/
+public class AnswerBody {
+    @SerializedName("Version")
+    private final String VERSION = "1.1";
+    @SerializedName("Sid")
+    private String sid;
+    @SerializedName("Answer")
+    private String answerBodySDP;
+
+    public AnswerBody(String s, String sdp) {
+        sid = s;
+        answerBodySDP = sdp;
+    }
+
+    public String getSid() {
+        return sid;
+    }
+
+    public void setSid(String sid) {
+        this.sid = sid;
+    }
+
+    public String getAnswerBodySDP() {
+        return answerBodySDP;
+    }
+
+    public void setAnswerBodySDP(String answerBodySDP) {
+        this.answerBodySDP = answerBodySDP;
+    }
+}
diff --git a/app/src/main/java/org/torproject/snowflake/pojo/AnswerBodySDP.java b/app/src/main/java/org/torproject/snowflake/pojo/AnswerBodySDP.java
new file mode 100644
index 0000000..d111e57
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/pojo/AnswerBodySDP.java
@@ -0,0 +1,35 @@
+package org.torproject.snowflake.pojo;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/*SDP answer from AnswerBody.java
+Answer:
+{
+    type: answer,
+    sdp: [WebRTC SDP]
+}
+*/
+public class AnswerBodySDP {
+    private final String TYPE = "answer";
+    private String sdp;
+
+    public String getSdp() {
+        return sdp;
+    }
+
+    public void setSdp(String sdp) {
+        this.sdp = sdp;
+    }
+
+    public String toString(){
+        JSONObject jsonObject = new JSONObject();
+        try {
+            jsonObject.put("type", TYPE);
+            jsonObject.put("sdp", sdp);
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return jsonObject.toString();
+    }
+}
diff --git a/app/src/main/java/org/torproject/snowflake/pojo/OfferRequestBody.java b/app/src/main/java/org/torproject/snowflake/pojo/OfferRequestBody.java
new file mode 100644
index 0000000..ccc01ff
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/pojo/OfferRequestBody.java
@@ -0,0 +1,20 @@
+package org.torproject.snowflake.pojo;
+
+import com.google.gson.annotations.SerializedName;
+
+/*{
+  Sid: [generated session id of proxy],
+  Version: 1.1,
+  Type: "mobile"
+}*/
+public class OfferRequestBody {
+    private String sid;
+    @SerializedName("Version")
+    private final String VERSION = "1.1";
+    @SerializedName("Type")
+    private final String TYPE = "mobile";
+
+    public OfferRequestBody(String sid){
+        this.sid = sid;
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/torproject/snowflake/pojo/SDPOfferResponse.java b/app/src/main/java/org/torproject/snowflake/pojo/SDPOfferResponse.java
new file mode 100644
index 0000000..4b86c23
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/pojo/SDPOfferResponse.java
@@ -0,0 +1,42 @@
+package org.torproject.snowflake.pojo;
+
+import com.google.gson.annotations.SerializedName;
+
+/*If a client is matched:
+HTTP 200 OK
+{
+  Status: "client match",
+  {
+    type: offer,
+    sdp: [WebRTC SDP]
+  }
+}
+
+If a client is not matched:
+HTTP 200 OK
+{
+    Status: "no match"
+}*/
+public class SDPOfferResponse {
+    @SerializedName("Status")
+    private String status;
+
+    @SerializedName("Offer")
+    private String offer;
+
+    public String getStatus() {
+        return status;
+    }
+
+    public void setStatus(String status) {
+        this.status = status;
+    }
+
+    public String getOffer() {
+        return offer;
+    }
+
+    public void setOffer(String offer) {
+        this.offer = offer;
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java b/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java
new file mode 100644
index 0000000..317bcc5
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/services/GetOfferService.java
@@ -0,0 +1,15 @@
+package org.torproject.snowflake.services;
+
+import org.torproject.snowflake.pojo.OfferRequestBody;
+import org.torproject.snowflake.pojo.SDPOfferResponse;
+
+import io.reactivex.rxjava3.core.Observable;
+import retrofit2.http.Body;
+import retrofit2.http.Headers;
+import retrofit2.http.POST;
+
+public interface GetOfferService {
+    @POST("proxy")
+    @Headers({"Content-type: application/json", "Host: 10.0.2.2:8080"})
+    Observable<SDPOfferResponse> getOffer(@Body OfferRequestBody body);
+}
diff --git a/app/src/main/java/org/torproject/snowflake/services/RetroServiceGenerator.java b/app/src/main/java/org/torproject/snowflake/services/RetroServiceGenerator.java
new file mode 100644
index 0000000..74f1d9d
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/services/RetroServiceGenerator.java
@@ -0,0 +1,36 @@
+package org.torproject.snowflake.services;
+
+import org.torproject.snowflake.BuildConfig;
+import org.torproject.snowflake.GlobalApplication;
+
+import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory;
+import okhttp3.OkHttpClient;
+import okhttp3.logging.HttpLoggingInterceptor;
+import retrofit2.Retrofit;
+import retrofit2.converter.gson.GsonConverterFactory;
+
+public class RetroServiceGenerator {
+    public static <S> S createService(
+            Class<S> serviceClass) {
+
+        Retrofit.Builder builder =
+                new Retrofit.Builder()
+                        .baseUrl(GlobalApplication.getBrokerUrl())
+                        .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
+                        .addConverterFactory(GsonConverterFactory.create());
+
+        OkHttpClient.Builder httpClient =
+                new OkHttpClient.Builder();
+
+        if (BuildConfig.DEBUG) {
+            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
+            interceptor.level(HttpLoggingInterceptor.Level.BODY);
+            httpClient.addInterceptor(interceptor);
+        }
+
+        builder.client(httpClient.build());
+        Retrofit retrofit = builder.build();
+
+        return retrofit.create(serviceClass);
+    }
+}
diff --git a/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java b/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java
new file mode 100644
index 0000000..8d2f153
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/services/SendAnswerService.java
@@ -0,0 +1,15 @@
+package org.torproject.snowflake.services;
+
+import org.torproject.snowflake.pojo.AnsResponse;
+import org.torproject.snowflake.pojo.AnswerBody;
+
+import io.reactivex.rxjava3.core.Observable;
+import retrofit2.http.Body;
+import retrofit2.http.Headers;
+import retrofit2.http.POST;
+
+public interface SendAnswerService {
+    @POST("answer")
+    @Headers({"Content-type: application/json", "Host: 10.0.2.2:8080"})
+    Observable<AnsResponse> sendAnswer(@Body AnswerBody body);
+}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits