|
@@ -3,6 +3,7 @@ package de.tudarmstadt.informatik.hostage.sync.tracing;
|
|
|
import java.security.KeyStore;
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.HttpVersion;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
@@ -23,10 +24,11 @@ import android.app.IntentService;
|
|
|
import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
-import android.os.Handler;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.ResultReceiver;
|
|
|
import android.preference.PreferenceManager;
|
|
|
+import android.util.Log;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
|
|
|
-import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
|
|
|
import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
|
|
|
|
|
@@ -34,23 +36,33 @@ public class TracingSyncService extends IntentService{
|
|
|
|
|
|
public static final String REMOTE_DEVICE = "de.tudarmstadt.informatik.hostage.REMOTE_DEVICE";
|
|
|
|
|
|
+ public static final String ACTION_START_SYNC = "de.tudarmstadt.informatik.hostage.ACTION_START_SYNC";
|
|
|
+ public static final String EXTRA_RECEIVER = "de.tudarmstadt.informatik.hostage.EXTRA_HANDLER";
|
|
|
+
|
|
|
+ public static final String UPLOAD_SIZE = "de.tudarmstadt.informatik.hostage.UPLOAD_SIZE";
|
|
|
+ public static final String UPLOAD_PROGRESS = "de.tudarmstadt.informatik.hostage.UPLOAD_PROGRESS";
|
|
|
+
|
|
|
public static final int RECORD_UPLOADED = 0x00;
|
|
|
public static final int SYNC_COMPLETE = 0x01;
|
|
|
|
|
|
|
|
|
-
|
|
|
private HttpClient httpClient;
|
|
|
- private Handler mHandler;
|
|
|
+ private ResultReceiver receiver;
|
|
|
|
|
|
HostageDBOpenHelper dbh;
|
|
|
|
|
|
SharedPreferences pref;
|
|
|
Editor editor;
|
|
|
|
|
|
- public TracingSyncService(Handler handler) {
|
|
|
- super("TracingSyncService");
|
|
|
- mHandler = handler;
|
|
|
- httpClient = createHttpClient();
|
|
|
+ public TracingSyncService() {
|
|
|
+ super(TracingSyncService.class.getName());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
pref = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
editor = pref.edit();
|
|
|
dbh = new HostageDBOpenHelper(this);
|
|
@@ -63,10 +75,19 @@ public class TracingSyncService extends IntentService{
|
|
|
*/
|
|
|
@Override
|
|
|
protected void onHandleIntent(Intent intent) {
|
|
|
- //TODO Fallunterscheidung Intents
|
|
|
- uploadNewRecords();
|
|
|
- getRemoteData();
|
|
|
- dbh.clearSyncInfos();
|
|
|
+ if (intent != null) {
|
|
|
+ final String action = intent.getAction();
|
|
|
+ if (ACTION_START_SYNC.equals(action)) {
|
|
|
+ receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
|
|
|
+ uploadNewRecords();
|
|
|
+ getRemoteData();
|
|
|
+ //TODO add: dbh.clearSyncInfos();
|
|
|
+ if(receiver != null){
|
|
|
+ receiver.send(SYNC_COMPLETE, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
@@ -81,9 +102,16 @@ public class TracingSyncService extends IntentService{
|
|
|
int offset = 1;
|
|
|
for(AttackRecord record: recordList){
|
|
|
editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
|
|
|
- uploadSingleRecord(record, serverAddress);
|
|
|
- if(mHandler != null)
|
|
|
- mHandler.obtainMessage(RECORD_UPLOADED, offset, size);
|
|
|
+ editor.commit();
|
|
|
+
|
|
|
+ boolean success = uploadSingleRecord(record, serverAddress);
|
|
|
+ Log.i("Tracing upload", "Upload of record: " + offset +"/" + size + ((success) ? " successful.": " failed."));
|
|
|
+ if(receiver != null){
|
|
|
+ Bundle data = new Bundle();
|
|
|
+ data.putInt(UPLOAD_SIZE, size);
|
|
|
+ data.putInt(UPLOAD_PROGRESS, offset);
|
|
|
+ receiver.send(RECORD_UPLOADED, data);
|
|
|
+ }
|
|
|
offset++;
|
|
|
}
|
|
|
}
|
|
@@ -99,15 +127,17 @@ public class TracingSyncService extends IntentService{
|
|
|
// Create a https client. Uses MySSLSocketFactory to accept all certificates
|
|
|
HttpPost httppost;
|
|
|
try {
|
|
|
+ httpClient = createHttpClient();
|
|
|
// Create HttpPost
|
|
|
- httppost = new HttpPost();
|
|
|
+ httppost = new HttpPost(serverAddress);
|
|
|
// Create JSON String of Record
|
|
|
//TODO FIX ME
|
|
|
- StringEntity se = new StringEntity( null/*record.toString(TraCINgFormatter
|
|
|
- .getInstance()) */);
|
|
|
+ //StringEntity se = new StringEntity( record.toString(TraCINgFormatter.getInstance()) );
|
|
|
+ StringEntity se = new StringEntity(record.toString());
|
|
|
httppost.setEntity(se);
|
|
|
// Execute HttpPost
|
|
|
- httpClient.execute(httppost);
|
|
|
+ HttpResponse response = httpClient.execute(httppost);
|
|
|
+ Log.i("TracingSyncService", "Status Code: " + response.getStatusLine().getStatusCode());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return false;
|