Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DataServiceTest {
@ClassRule
public static final ServiceTestRule mServiceRule = new ServiceTestRule();

private static DataService.LocalBinder binder;
private static DataService.DataServiceBinder binder;
private static UUID userID;


Expand All @@ -61,7 +61,7 @@ public static void setBinder() throws TimeoutException {
DataService.class);

// Bind the service and grab a reference to the binder.
binder = (DataService.LocalBinder) mServiceRule.bindService(serviceIntent);
binder = (DataService.DataServiceBinder) mServiceRule.bindService(serviceIntent);
}


Expand Down
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@
android:parentActivityName=".MainActivity" />

<service
android:name="ch.ethz.inf.vs.fstreun.network.SessionPublishService"
android:name="ch.ethz.inf.vs.fstreun.network.SessionPublish.Server.GroupPublishService"
android:enabled="true"
android:exported="true" />
<service
android:name="ch.ethz.inf.vs.fstreun.network.SessionSubscribeService"
android:name="ch.ethz.inf.vs.fstreun.network.SessionPublish.Client.GroupSubscribeService"
android:enabled="true"
android:exported="true" />

<service android:name=".filemanager.DataService" />

<service android:name="ch.ethz.inf.vs.fstreun.network.DataSyncSubscribeService" android:stopWithTask="true"/>
<service android:name="ch.ethz.inf.vs.fstreun.network.DataSyncPublishService" />
<service android:name="ch.ethz.inf.vs.fstreun.network.DataSync.Client.DataSyncSubscribeService" android:stopWithTask="true"/>
<service android:name="ch.ethz.inf.vs.fstreun.network.DataSync.Server.DataSyncPublishService" />

<activity android:name=".TransactionListActivity"
android:parentActivityName=".GroupActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package ch.ethz.inf.vs.fstreun.network.DataSync.Client;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import ch.ethz.inf.vs.fstreun.network.HttpParser;
import ch.ethz.inf.vs.fstreun.network.NetworkKeys;
import ch.ethz.inf.vs.fstreun.payapp.filemanager.DataService;

/**
* Created by fabio on 12/25/17.
*
*/

public class DataSyncSubscribe implements Runnable{

private final String TAG = "DataSyncSubscribe";

private final DataService.SessionNetworkAccess mSessionAccess;
private final Socket mSocket;

DataSyncSubscribe(DataService.SessionNetworkAccess sessionAccess, Socket socket){
mSessionAccess = sessionAccess;
mSocket = socket;
}

@Override
public void run() {
try {
if (mSessionAccess != null) {
handleCommunication();
}
} catch (IOException e) {
Log.e(TAG, "handleSocket throws IOException", e);
}

if (mSocket != null){
try {
mSocket.close();
Log.d(TAG, "closed socket");
} catch (IOException e) {
Log.e(TAG, "failed closing socket");
}
}
}

private void handleCommunication() throws IOException {

String requestBody;
try {
requestBody = generateRequestBody();
} catch (JSONException e) {
// generation of Request failed
Log.e(TAG, "Request generation failed!");
return;
}

String request = generateRequest(mSocket.getInetAddress().toString(), mSocket.getPort(), requestBody);
Log.d(TAG, "Request: " + request);

// get output stream and print writer
OutputStream outputStream = mSocket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);

printWriter.print(request);
printWriter.flush();

// get input buffer
BufferedReader input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));

// parse http response
HttpParser httpParser = new HttpParser(input);
Log.d(TAG, "request: " + httpParser);

if (httpParser.isError()){
// error occurred druing http parsing.
Log.e(TAG, "HTTP Parsing error occurred");

input.close();
printWriter.close();
return;
}

// get http body
String bodyString = httpParser.getBody();

handleResponse(bodyString);

// clean up streams and buffers
input.close();
printWriter.close();

}

private void handleResponse(String responseBody) {

try {
JSONObject object = new JSONObject(responseBody);

Boolean success = object.getBoolean(NetworkKeys.SUCCESS);
if (!success){
// not successfull or wrong format
return;
}

UUID sessionId = UUID.fromString(object.getString(NetworkKeys.SESSIONID));
if (!sessionId.equals(mSessionAccess.getSessionID())){
// data not supposed to be for this session
return;
}

JSONObject data = object.getJSONObject(NetworkKeys.DATA);

Map<UUID, Integer> expected = new HashMap<>();
JSONArray jsonMap = object.getJSONArray(NetworkKeys.LENGTHMAP);

for (int i = 0; i < jsonMap.length(); i++) {
JSONObject item = (JSONObject) jsonMap.get(i);
UUID mUUid = UUID.fromString( item.getString(NetworkKeys.DEVICEID));
Integer mLength = Integer.valueOf(item.getString(NetworkKeys.LENGHT));
expected.put(mUUid, mLength);
}

// save data
mSessionAccess.putData(data, expected);

} catch (JSONException e) {
// response body is not json
Log.e(TAG, "Response body handling failed, due to JSONException", e);
}


}


private String generateRequest(String host, int port, String body) {
String path = "/dataSync";
String accept = "text/plain";
String connect = "Closed";


return "GET " + path + " HTTP/1.1\r\n"
+ "Host: " + host + ":" + port + "\r\n"
+ "Accept: " + accept + "\r\n"
+ "Connection: " + connect + "\r\n\r\n"
+ body + "\r\n"
+ "\r\n";
}


private String generateRequestBody() throws JSONException {

// get data for request (UUID sessionId, Map<UUID, Int> start)
UUID sessionId = mSessionAccess.getSessionID();
Map<UUID, Integer> mData = mSessionAccess.getLength();

// create json request
JSONObject jsonRequest = new JSONObject();
jsonRequest.put(NetworkKeys.SESSIONID, sessionId.toString());
jsonRequest.put(NetworkKeys.COMMAND, "start");
JSONArray mJsonMap = new JSONArray();

for (Map.Entry<UUID, Integer> item : mData.entrySet()) {
UUID deviceId = item.getKey();
Integer length = item.getValue();

JSONObject mJsonItem = new JSONObject();
mJsonItem.put(NetworkKeys.DEVICEID, deviceId.toString());
mJsonItem.put(NetworkKeys.LENGHT, length.toString());
mJsonMap.put(mJsonItem);
}

jsonRequest.put(NetworkKeys.LENGTHMAP, mJsonMap);

return jsonRequest.toString();
}
}
Loading