Kaynağa Gözat

synchronization stuff

Alexander Brakowski 10 yıl önce
ebeveyn
işleme
66c461a517

+ 4 - 1
src/de/tudarmstadt/informatik/hostage/logging/Record.java

@@ -1,5 +1,7 @@
 package de.tudarmstadt.informatik.hostage.logging;
 
+import java.io.Serializable;
+
 import de.tudarmstadt.informatik.hostage.logging.MessageRecord.TYPE;
 import de.tudarmstadt.informatik.hostage.logging.formatter.Formatter;
 
@@ -7,7 +9,8 @@ import de.tudarmstadt.informatik.hostage.logging.formatter.Formatter;
  * Record that holds all information of a message including full attack and network information.
  * This class should be avoided but is necessary due to inter group complications.
  */
-public class Record {
+public class Record implements Serializable {
+    private static final long serialVersionUID = 1L;
 
 	//
 	private MessageRecord message;

+ 80 - 50
src/de/tudarmstadt/informatik/hostage/sync/p2p/P2PSyncActivity.java

@@ -1,6 +1,7 @@
 package de.tudarmstadt.informatik.hostage.sync.p2p;
 
 import android.app.Activity;
+import android.app.ProgressDialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.IntentFilter;
@@ -27,6 +28,8 @@ import android.widget.ViewAnimator;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.UnsupportedEncodingException;
@@ -39,6 +42,10 @@ import java.util.ArrayList;
 import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.R;
+import de.tudarmstadt.informatik.hostage.logging.Record;
+import de.tudarmstadt.informatik.hostage.logging.SyncData;
+import de.tudarmstadt.informatik.hostage.logging.SyncInfo;
+import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
 
 public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInfoListener, WifiP2pManager.PeerListListener, WifiP2pManager.ChannelListener, AdapterView.OnItemClickListener, WifiP2pManager.ConnectionInfoListener {
     private WifiP2pManager mManager;
@@ -329,9 +336,10 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
         }
     }
 
-    public static class ServerAsyncTask extends AsyncTask<Void, Void, String> {
+    public static class ServerAsyncTask extends AsyncTask<String, Integer, Integer> {
         private Activity context;
         private int port;
+        private final ProgressDialog progressDialog;
 
         /**
          * @param context
@@ -339,76 +347,98 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInf
         public ServerAsyncTask(Activity context, int port) {
             this.context = context;
             this.port = port;
+            this.progressDialog = new ProgressDialog(context);
         }
+
         @Override
-        protected String doInBackground(Void... params) {
+        protected void onPreExecute() {
+            super.onPreExecute();
+
+            this.progressDialog.setMessage("Synchronizing data with other device...");
+            this.progressDialog.setIndeterminate(false);
+            this.progressDialog.setMax(100);
+            this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+            this.progressDialog.setCancelable(true);
+            this.progressDialog.show();
+        }
+
+        @Override
+        protected Integer doInBackground(String... params) {
             try {
                 ServerSocket serverSocket = new ServerSocket(this.port);
-
                 Log.i("server", "Waiting for clients!");
 
                 Socket client = serverSocket.accept();
-
                 Log.i("server", "Client connected!");
-                /*final File f = new File(Environment.getExternalStorageDirectory() + "/"
-                        + context.getPackageName() + "/wifip2pshared-" + System.currentTimeMillis()
-                        + ".txt");
-                File dirs = new File(f.getParent());
-                if (!dirs.exists())
-                    dirs.mkdirs();
-                f.createNewFile();*/
-                InputStream inputstream = client.getInputStream();
-                readToToast(inputstream);
 
+                publishProgress(1);
+
+                ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
+                ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
+
+                // -- client speaks first, receive syncinfo
+                SyncInfo otherSyncInfo = null;
+
+                try {
+                    otherSyncInfo = (SyncInfo) ois.readObject();
+                } catch (ClassNotFoundException e) {
+                    e.printStackTrace();
+                }
+
+                publishProgress(2);
+
+                // --- 1. write first message - syncinfo
                 Log.i("server", "writing to client");
-                client.getOutputStream().write(("HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am server").getBytes());
+
+                SyncInfo thisSyncInfo = new SyncInfo();
+                thisSyncInfo.deviceMap.put("dev1", 12L);
+                thisSyncInfo.deviceMap.put("dev2", 13L);
+
+
+                oos.writeObject(thisSyncInfo);
+                oos.flush();
+                publishProgress(3);
+
+                // --- 2. read sync data
+                SyncData syncData = null;
+
+                try {
+                    syncData = (SyncData) ois.readObject();
+                } catch (ClassNotFoundException e) {
+                    e.printStackTrace();
+                }
+                publishProgress(4);
+
+                // --- 3. send sync data
+                SyncData mySyncData = new SyncData();
+                mySyncData.records.add(new Record());
+
+                oos.writeObject(mySyncData);
+                oos.flush();
+                publishProgress(5);
+
+                // --- 4. We are done!
+                ois.close();
+                oos.close();
 
                 //copyFile(inputstream, new FileOutputStream(f));
-                inputstream.close();
                 serverSocket.close();
 
-                return "";
+                return 0;
             } catch (IOException e) {
-                return null;
+                return -1;
             }
         }
 
-
-        public void readToToast(InputStream inputStream){
-            final String received = inputStreamToString(inputStream, 2048);
-
-            Log.i("server", "Received: " + received);
-
-            context.runOnUiThread(new Runnable() {
-                public void run() {
-                    Toast.makeText(context, "Received: " + received, Toast.LENGTH_LONG).show();
-                }
-            });
+        @Override
+        protected void onProgressUpdate(Integer... progress) {
+            this.progressDialog.setProgress(progress[0] * (100 / 5));
         }
 
-        public static String inputStreamToString(final InputStream is, final int bufferSize)
-        {
-            final char[] buffer = new char[bufferSize];
-            final StringBuilder out = new StringBuilder();
-            try {
-                final Reader in = new InputStreamReader(is, "UTF-8");
-                try {
-                    for (;;) {
-                        int rsz = in.read(buffer, 0, buffer.length);
-                        if (rsz < 0)
-                            break;
-                        out.append(buffer, 0, rsz);
-                    }
-                } finally {
-                    in.close();
-                }
-            } catch (UnsupportedEncodingException ex) {
-                ex.printStackTrace();
-            } catch (IOException ex) {
-                ex.printStackTrace();
-            }
 
-            return out.toString();
+        @Override
+        protected void onPostExecute(Integer unused) {
+            this.progressDialog.dismiss();
         }
     }