Skip to content
Snippets Groups Projects
Commit dc224876 authored by Neil's avatar Neil
Browse files

updated android NetworkInterface

parent db7a0871
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,7 @@ public class MainActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
network = NetworkInterface.getInstance();
network.connect();
network.setNetworkListener( new NetworkResponseListener() {
@Override
public void onData(final byte[] data) {
......
......@@ -13,6 +13,7 @@ import java.net.UnknownHostException;
* Usage:
*
* NetworkInterface network = NetworkInterface.getInstance();
* network.connect();
* network.setNetworkListener(
* new NetworkResponseListener() {
* onData( byte[] data ) {
......@@ -21,26 +22,24 @@ import java.net.UnknownHostException;
* }
* );
*
* TODO: might need to figure out how to get data back to a given thread.
*/
public class NetworkInterface {
private static NetworkInterface instance;
private Socket socket;
private NetworkResponseListener listener;
private boolean isConnected = false;
private Thread socketThread;
private static final String SERVER_IP = "52.24.152.96";
private static final int SERVERPORT = 3000;
private static final String SERVER_IP = "108.179.184.26";
/**
* Hide the constructor for singleton.
*/
private NetworkInterface() {
// Spin up the client thread
new Thread(new ClientThread()).start();
}
private NetworkInterface() {}
/**
* Made this a singleton.
* Get the instance of NetworkInterface (Singleton).
*/
public static NetworkInterface getInstance() {
if ( instance == null ) {
......@@ -49,6 +48,18 @@ public class NetworkInterface {
return instance;
}
/**
* Connect the NetworkInterface to the server.
*/
public void connect() {
// Make sure there is not already a thread up
if ( !isConnected && socketThread == null ) {
// Spin up the client thread
socketThread = new Thread(new ClientThread());
socketThread.start();
}
}
/**
* Sets the listener that will be notified when receiving data from the server.
* @param listener
......@@ -62,21 +73,26 @@ public class NetworkInterface {
* @param data
*/
public void sendData( String data ) {
if ( socket == null || socket.isClosed() ) throw new AssertionError("Error, socket has not been initialized or is closed.");
try {
Log.d("DEBUG", "Attempt to send: " + data );
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
out.write(data.getBytes());
out.flush();
} catch (UnknownHostException e) {
Log.d("DEBUG", e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("DEBUG", e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.d("DEBUG", e.toString());
e.printStackTrace();
if ( isConnected ) {
if (socket == null || socket.isClosed())
throw new AssertionError("Error, socket has not been initialized or is closed.");
try {
Log.d("DEBUG", "Attempt to send: " + data);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
out.write(data.getBytes());
out.flush();
} catch (UnknownHostException e) {
Log.d("DEBUG", e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d("DEBUG", e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.e("DEBUG", e.toString());
e.printStackTrace();
}
} else {
Log.e("DEBUG", "Tried to send data but socket is not connected.");
}
}
......@@ -92,6 +108,7 @@ public class NetworkInterface {
Log.d("DEBUG", "Attempting to connect to server at: " + serverAddress);
socket = new Socket(serverAddress, SERVERPORT);
socket.setKeepAlive(true);
isConnected = true;
byte[] buf = new byte[1024];
InputStream input = socket.getInputStream();
......@@ -104,9 +121,11 @@ public class NetworkInterface {
} catch (UnknownHostException e1) {
Log.d("DEBUG", e1.toString());
e1.printStackTrace();
isConnected = false;
} catch (IOException e1) {
Log.d("DEBUG", e1.toString());
e1.printStackTrace();
isConnected = false;
}
}
}
......
package edu.uw.cs.androidsockets;
/**
* Created by neilharlow on 5/4/15.
* Simple interface for Network response callback.
*/
public interface NetworkResponseListener {
void onData( byte[] data );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment