Android Open Source - roodroid Connection






From Project

Back to project page roodroid.

License

The source code is released under:

Copyright (c) 2011, Jonathan Perichon & Lucas Gerbeaux Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"...

If you think the Android project roodroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package fr.utbm.roodroid;
/*from www  . j a v a 2s.c  o  m*/
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.Runnable;

import android.os.Handler;
import android.util.Log;

/**
 * Connection
 * Abstract class that defines the global behavior of a connection.
 * This class is used by all the connection types (Wifi & Bluetooth) and 
 * for both sides (client & server).
 * 
 * It has methods to write packets in the connection.
 * Implements Runnable to have his own thread for reading the incoming connection,
 * and then routing the read packets to a handler.
 * 
 * @author Jonathan Perichon <jonathan.perichon@gmail.com>
 * @author Lucas Gerbeaux <lucas.gerbeaux@gmail.com>
 *
 */
public abstract class Connection implements Runnable, Serializable {
  private static final long serialVersionUID = 6234567955793245005L;
  
  protected InputStream inStream;
    protected OutputStream outStream;
    private final Handler handler;
    protected boolean isRunning;
    
    public Connection(Handler handler) {
      this.isRunning = true;
        this.inStream = null;
        this.outStream = null;
      this.handler = handler;
    }
    
    public abstract String getAddressSource();
    public abstract void disconnect();
  
  protected synchronized boolean isRunning() {
    return isRunning;
  }
  
    public void write(String id, TCPCommandType commandType, Serializable obj) throws IOException {
      this.write(new PacketClient(id, commandType, obj));
    }
    
    public void write(TCPCommandType commandType, Serializable obj) throws IOException {
      this.write(new Packet(commandType, obj));
    }
    
    public void write(TCPCommandType commandType) throws IOException {
      this.write(new Packet(commandType, null));
    }

    public void write(String id, TCPCommandType commandType) throws IOException {
    this.write(new PacketClient(id, commandType, null));
  }
    
  private void write(Packet packet) throws IOException {
    ObjectOutputStream os = new ObjectOutputStream(this.outStream);
    os.writeObject(packet);
  }
  
  @Override
  public void run() {
        while (isRunning()) {
            try {
              ObjectInputStream in = new ObjectInputStream(inStream);
        Packet p = (Packet)in.readObject();
        p.setAddressSource(getAddressSource());
                handler.obtainMessage(p.getCommandType().ordinal(), p).sendToTarget();
                ApplicationManager.appendLog(Log.INFO, "Run() Connection", "running");

          } catch (IOException e) {
              ApplicationManager.appendLog(Log.ERROR, "Connection", "io exception");
              e.printStackTrace();
              handler.obtainMessage(TCPCommandType.CONNECTION_EXIT.ordinal(), new PacketClient("", TCPCommandType.CONNECTION_EXIT, this)).sendToTarget();
              this.disconnect();
            } catch (ClassNotFoundException ce) {
              ApplicationManager.appendLog(Log.ERROR, "Connection", ce.getMessage());
            }
        }
  }

}




Java Source Code List

fr.utbm.roodroid.ApplicationManager.java
fr.utbm.roodroid.ConnectionBluetooth.java
fr.utbm.roodroid.ConnectionWifi.java
fr.utbm.roodroid.Connection.java
fr.utbm.roodroid.Conversation.java
fr.utbm.roodroid.Message.java
fr.utbm.roodroid.PacketClient.java
fr.utbm.roodroid.Packet.java
fr.utbm.roodroid.TCPCommandType.java
fr.utbm.roodroid.TextMessage.java
fr.utbm.roodroid.activity.AuthorizedUsernamesAdapter.java
fr.utbm.roodroid.activity.BluetoothDiscovery.java
fr.utbm.roodroid.activity.ClientBluetoothSettings.java
fr.utbm.roodroid.activity.ClientWifiSettings.java
fr.utbm.roodroid.activity.ConversationsAdapter.java
fr.utbm.roodroid.activity.ConversationsList.java
fr.utbm.roodroid.activity.LogPage.java
fr.utbm.roodroid.activity.MessagesAdapter.java
fr.utbm.roodroid.activity.MessagesList.java
fr.utbm.roodroid.activity.ProfileTypeChooser.java
fr.utbm.roodroid.activity.ServerAdvancedSettings.java
fr.utbm.roodroid.activity.ServerBluetoothMain.java
fr.utbm.roodroid.activity.ServerBluetoothSettings.java
fr.utbm.roodroid.activity.ServerWifiMain.java
fr.utbm.roodroid.activity.ServerWifiSettings.java
fr.utbm.roodroid.client.ClientBluetooth.java
fr.utbm.roodroid.client.ClientWifi.java
fr.utbm.roodroid.client.Client.java
fr.utbm.roodroid.client.ConversationsDataSource.java
fr.utbm.roodroid.client.ConversationsHelper.java
fr.utbm.roodroid.server.AuthByID.java
fr.utbm.roodroid.server.AuthByPassword.java
fr.utbm.roodroid.server.AuthMethod.java
fr.utbm.roodroid.server.AuthNone.java
fr.utbm.roodroid.server.ServerBluetooth.java
fr.utbm.roodroid.server.ServerWifi.java
fr.utbm.roodroid.server.Server.java