123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package de.tudarmstadt.informatik.hostage.sync.wifi_direct;
- import android.util.Log;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- /**
- * Created by Julien on 07.01.2015.
- */
- public class WiFiP2pClientTask extends BackgroundTask {
- private String hostIP;
- private Socket socket;
- public String getHostIP() {
- return hostIP;
- }
- public void setHostIP(String hostIP) {
- this.hostIP = hostIP;
- }
- public int port(){
- return 8988;
- }
- public int time_out(){
- return 1000;
- }
- public WiFiP2pClientTask(String hostIP, BackgroundTaskCompletionListener l){
- super(l);
- this.hostIP = hostIP;
- }
- @Override
- public void interrupt(boolean b){
- super.interrupt(b);
- if (b && this.socket != null) {
- try {
- this.socket.close();
- } catch (IOException e) {
- Log.e("WiFiP2pClientTask", e.getMessage());
- }
- }
- }
- @Override
- public boolean performInBackground(){
- int tryNum = 0;
- while (!this.isInterrupted()){
- this.socket = new Socket();
- try {
- Log.d("WiFiP2pClientTask", "Opening client socket - ");
- socket.bind(null);
- socket.connect((new InetSocketAddress(hostIP, this.port())), this.time_out());
- Log.d("WiFiP2pClientTask", "Client socket - " + socket.isConnected());
- this.handleConnection(socket);
- Log.d("WiFiP2pClientTask", "Client: Data written");
- } catch (ClassNotFoundException e){
- Log.e("WiFiP2pClientTask", e.getMessage());
- return false;
- } catch (IOException e) {
- Log.e("WiFiP2pClientTask", e.getMessage());
- if(tryNum >= 5) {
- this.interrupt(true);
- break;
- }
- 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);
- } catch (InterruptedException ie){
- ie.printStackTrace();
- }
- } finally {
- if (socket != null) {
- if (socket.isConnected()) {
- try {
- socket.close();
- } catch (IOException e) {
- // Give up
- e.printStackTrace();
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- private void handleConnection(Socket client) throws IOException, ClassNotFoundException {
- InputStream is = client.getInputStream();
- ObjectInputStream ois = new ObjectInputStream(is);
- OutputStream os = client.getOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(os);
- Object obj = null;
- do {
- WiFiP2pSerializableObject receivedObj = ( WiFiP2pSerializableObject) obj;
- obj = null;
- WiFiP2pSerializableObject toSend = this.handleReceivedObject(receivedObj);
- if (toSend != null) {
- oos.writeObject(toSend);
- oos.flush();
- oos.reset();
- obj = ois.readObject();
- }
- } while (obj != null && obj instanceof WiFiP2pSerializableObject);
- oos.close();
- os.close();
- ois.close();
- is.close();
- this.interrupt(true);
- }
- public WiFiP2pSerializableObject handleReceivedObject(WiFiP2pSerializableObject receivedObj){
- return null;
- }
- }
|