|
@@ -11,26 +11,25 @@ import android.net.wifi.p2p.WifiP2pGroup;
|
|
|
import android.net.wifi.p2p.WifiP2pInfo;
|
|
|
import android.net.wifi.p2p.WifiP2pManager;
|
|
|
import android.os.AsyncTask;
|
|
|
-import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
-import android.os.Environment;
|
|
|
+import android.util.Log;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
import android.widget.AdapterView;
|
|
|
import android.widget.ArrayAdapter;
|
|
|
-import android.widget.Button;
|
|
|
import android.widget.ListView;
|
|
|
import android.widget.RelativeLayout;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
import android.widget.ViewAnimator;
|
|
|
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
import java.io.OutputStream;
|
|
|
+import java.io.Reader;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.net.InetSocketAddress;
|
|
@@ -41,7 +40,7 @@ import java.util.List;
|
|
|
|
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
|
|
|
|
-public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerListListener, WifiP2pManager.ChannelListener, AdapterView.OnItemClickListener, WifiP2pManager.ConnectionInfoListener {
|
|
|
+public class P2PSyncActivity extends Activity implements WifiP2pManager.GroupInfoListener, WifiP2pManager.PeerListListener, WifiP2pManager.ChannelListener, AdapterView.OnItemClickListener, WifiP2pManager.ConnectionInfoListener {
|
|
|
private WifiP2pManager mManager;
|
|
|
private WifiP2pManager.Channel mChannel;
|
|
|
private BroadcastReceiver mReceiver;
|
|
@@ -66,6 +65,9 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
private final int OWNER_SERVER_PORT = 8988;
|
|
|
private final int CLIENT_SERVER_PORT = 8989;
|
|
|
|
|
|
+ private ClientAsyncTask clientAsync;
|
|
|
+ private ServerAsyncTask serverAsync;
|
|
|
+
|
|
|
private void extractFromView(){
|
|
|
mTxtP2PDeviceName = (TextView) findViewById(R.id.txt_p2p_device_name);
|
|
|
mTxtP2PDeviceStatus = (TextView) findViewById(R.id.txt_p2p_device_status);
|
|
@@ -105,6 +107,9 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
public void setWifiDirectNotAvailable(){
|
|
|
//mBtnP2PSearch.setVisibility(View.GONE);
|
|
|
mTxtP2PNotAvailable.setVisibility(View.VISIBLE);
|
|
|
+ peers.clear();
|
|
|
+ ((WiFiPeerListAdapter) mLstP2PDevices.getAdapter()).notifyDataSetChanged();
|
|
|
+ mDevice = null;
|
|
|
}
|
|
|
|
|
|
public void setWifiDirectAvailable(){
|
|
@@ -112,6 +117,16 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
mTxtP2PNotAvailable.setVisibility(View.GONE);
|
|
|
}
|
|
|
|
|
|
+ public void closeConnection(){
|
|
|
+ if(this.serverAsync != null && !this.serverAsync.isCancelled()){
|
|
|
+ this.serverAsync.cancel(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(this.clientAsync != null && !this.clientAsync.isCancelled()){
|
|
|
+ this.clientAsync.cancel(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void registerListeners(){
|
|
|
mTxtP2PChangeDeviceName.setOnClickListener(new View.OnClickListener() {
|
|
|
@Override
|
|
@@ -185,6 +200,16 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private WifiP2pDevice getConnectedPeer(){
|
|
|
+ for(WifiP2pDevice device: peers){
|
|
|
+ if(device.status == WifiP2pDevice.CONNECTED){
|
|
|
+ return device;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private static String getDeviceStatus(int deviceStatus) {
|
|
|
switch (deviceStatus) {
|
|
|
case WifiP2pDevice.AVAILABLE:
|
|
@@ -211,7 +236,7 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
|
|
|
@Override
|
|
|
public void onChannelDisconnected() {
|
|
|
-
|
|
|
+ closeConnection();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -241,11 +266,20 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
//if(mOtherDevice == null) return;
|
|
|
|
|
|
if(info.groupFormed && info.isGroupOwner){
|
|
|
- new FileServerAsyncTask(this, OWNER_SERVER_PORT).execute();
|
|
|
- //new FileTransferAsyncTask(this, mOtherDevice.deviceAddress, CLIENT_SERVER_PORT).execute();
|
|
|
+ Log.i("server", "I am the group owner!");
|
|
|
+ WifiP2pDevice connectedPeer = getConnectedPeer();
|
|
|
+ if(connectedPeer != null && mInfo != null){
|
|
|
+ Log.i("peers", mInfo.groupFormed + " - " + mInfo.isGroupOwner);
|
|
|
+ Log.i("server", "Starting async server!");
|
|
|
+ serverAsync = new ServerAsyncTask(this, OWNER_SERVER_PORT);
|
|
|
+ serverAsync.execute();
|
|
|
+ }
|
|
|
+ //new ClientAsyncTask(this, mOtherDevice.deviceAddress, CLIENT_SERVER_PORT).execute();
|
|
|
} else if(info.groupFormed){
|
|
|
- //new FileServerAsyncTask(this, CLIENT_SERVER_PORT).execute();
|
|
|
- new FileTransferAsyncTask(this, mInfo.groupOwnerAddress.getHostAddress(), OWNER_SERVER_PORT).execute();
|
|
|
+ //new ServerAsyncTask(this, CLIENT_SERVER_PORT).execute();
|
|
|
+ Log.i("client", "Starting async client!");
|
|
|
+ clientAsync = new ClientAsyncTask(this, mInfo.groupOwnerAddress.getHostAddress(), OWNER_SERVER_PORT);
|
|
|
+ clientAsync.execute();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -253,6 +287,11 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onGroupInfoAvailable(WifiP2pGroup group) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Array adapter for ListFragment that maintains WifiP2pDevice list.
|
|
|
*/
|
|
@@ -283,9 +322,6 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
top.setText(device.deviceName);
|
|
|
}
|
|
|
if (bottom != null) {
|
|
|
- if(device.status == WifiP2pDevice.CONNECTED){
|
|
|
- mOtherDevice = device;
|
|
|
- }
|
|
|
bottom.setText(getDeviceStatus(device.status));
|
|
|
}
|
|
|
}
|
|
@@ -293,14 +329,14 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
|
|
|
- private Context context;
|
|
|
+ public static class ServerAsyncTask extends AsyncTask<Void, Void, String> {
|
|
|
+ private Activity context;
|
|
|
private int port;
|
|
|
|
|
|
/**
|
|
|
* @param context
|
|
|
*/
|
|
|
- public FileServerAsyncTask(Context context, int port) {
|
|
|
+ public ServerAsyncTask(Activity context, int port) {
|
|
|
this.context = context;
|
|
|
this.port = port;
|
|
|
}
|
|
@@ -309,7 +345,11 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
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");
|
|
@@ -320,10 +360,13 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
InputStream inputstream = client.getInputStream();
|
|
|
readToToast(inputstream);
|
|
|
|
|
|
+ Log.i("server", "writing to client");
|
|
|
client.getOutputStream().write(("HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am server").getBytes());
|
|
|
|
|
|
//copyFile(inputstream, new FileOutputStream(f));
|
|
|
+ inputstream.close();
|
|
|
serverSocket.close();
|
|
|
+
|
|
|
return "";
|
|
|
} catch (IOException e) {
|
|
|
return null;
|
|
@@ -332,34 +375,54 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
|
|
|
|
|
|
public void readToToast(InputStream inputStream){
|
|
|
- byte buf[] = new byte[1024];
|
|
|
- StringBuffer bf = new StringBuffer();
|
|
|
+ final String received = inputStreamToString(inputStream, 2048);
|
|
|
|
|
|
- int len;
|
|
|
- try {
|
|
|
- while ((len = inputStream.read(buf)) != -1) {
|
|
|
- bf.append(buf);
|
|
|
+ Log.i("server", "Received: " + received);
|
|
|
|
|
|
+ context.runOnUiThread(new Runnable() {
|
|
|
+ public void run() {
|
|
|
+ Toast.makeText(context, "Received: " + received, Toast.LENGTH_LONG).show();
|
|
|
}
|
|
|
- inputStream.close();
|
|
|
- } catch (IOException e) {
|
|
|
- return;
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ 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();
|
|
|
}
|
|
|
|
|
|
- Toast.makeText(context, "Received: " + bf.toString(), Toast.LENGTH_LONG).show();
|
|
|
+ return out.toString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static class FileTransferAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
+ public static class ClientAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
private Context context;
|
|
|
private int port;
|
|
|
private String host;
|
|
|
- private static final int SOCKET_TIMEOUT = 5000;
|
|
|
+ private static final int SOCKET_TIMEOUT = 10000;
|
|
|
+ private int tryNum = 0;
|
|
|
|
|
|
/**
|
|
|
* @param context
|
|
|
*/
|
|
|
- public FileTransferAsyncTask(Context context, String host, int port) {
|
|
|
+ public ClientAsyncTask(Context context, String host, int port) {
|
|
|
this.context = context;
|
|
|
this.host = host;
|
|
|
this.port = port;
|
|
@@ -368,16 +431,35 @@ public class P2PSyncActivity extends Activity implements WifiP2pManager.PeerList
|
|
|
@Override
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
Socket socket = new Socket();
|
|
|
+ tryNum++;
|
|
|
|
|
|
try {
|
|
|
+ Log.i("client", "trying to connect to " + host + ":" + port);
|
|
|
socket.bind(null);
|
|
|
socket.connect(new InetSocketAddress(host, port), SOCKET_TIMEOUT);
|
|
|
|
|
|
+ Log.i("client", "connected to server");
|
|
|
OutputStream stream = socket.getOutputStream();
|
|
|
|
|
|
+ Log.i("client", "sending: " + "HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am " + host);
|
|
|
stream.write(("HELLO I HOPE THIS WILL REACH IT'S DESTINATION. I am " + host).getBytes());
|
|
|
+ stream.flush();
|
|
|
+ stream.close();
|
|
|
+ socket.close();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ if(tryNum >= 12) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ long seconds_to_wait = (long) Math.min(60, Math.pow(2, tryNum));
|
|
|
+ Log.i("client", "could not connect to server. Will try again in " + seconds_to_wait + "s");
|
|
|
+
|
|
|
+ try {
|
|
|
+ Thread.sleep(seconds_to_wait * 1000);
|
|
|
+ doInBackground(params);
|
|
|
+ } catch (InterruptedException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return null;
|