From e7898c04063ed9294c72e8c39565dd16326cbc9c Mon Sep 17 00:00:00 2001 From: Aylen Ricca <aricca@fing.edu.uy> Date: Wed, 23 Jul 2014 11:41:05 -0300 Subject: [PATCH] modifications to configure ip port-with validation and welcome msg --- .../edu/chesstrack/communication/Client.java | 33 +++++++++--- .../fing/chesstrack/ChessTrackActivity.java | 53 +++++++++++++++++-- cte-serv/server.java | 6 ++- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java index 3636e4a..246e230 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/chesstrack/communication/Client.java @@ -10,22 +10,39 @@ import android.util.Log; public class Client { private static final String TAG = "CLIENT"; + private static Client _clientInstance; + private Socket serverSocket; + private int _serverPort; + private String _serverIp; + //private BufferedReader input; private DataOutputStream output; - public static final int SERVERPORT = 5555; + protected Client(){ + + } + + public static Client getInstance(){ + if(_clientInstance == null){ + _clientInstance = new Client(); + } + return _clientInstance; + } - public Client(String serverIp) { + public void EstablishConnection(String serverIp, int serverPort) { Log.i(TAG, "init client-server communication"); + this._serverIp = serverIp; + this._serverPort = serverPort; + try { - InetAddress serverAddr = InetAddress.getByName(serverIp); - serverSocket = new Socket(serverAddr, SERVERPORT); - Log.i(TAG, "Server on " + serverIp + ":" + SERVERPORT); + InetAddress serverAddr = InetAddress.getByName(this._serverIp); + serverSocket = new Socket(serverAddr, _serverPort); + Log.i(TAG, "Server on " + this._serverIp + ":" + _serverPort); // get stream to send data this.output = new DataOutputStream(this.serverSocket.getOutputStream()); - this.SendData(new String("Aylen Ricca Cambon\n")); + // get stream to receive data //this.input = new BufferedReader(new InputStreamReader(this.serverSocket.getInputStream())); } catch (IOException e) { @@ -57,9 +74,9 @@ public class Client { public void Stop() { try { - serverSocket.close(); - output.close(); //input.close(); + output.close(); + serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } diff --git a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/ChessTrackActivity.java b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/ChessTrackActivity.java index 7a0224e..953e901 100644 --- a/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/ChessTrackActivity.java +++ b/ProyectoAndroid/ChessTrack/src/uy/edu/fing/chesstrack/ChessTrackActivity.java @@ -29,6 +29,14 @@ import android.widget.EditText; public class ChessTrackActivity extends Activity implements CvCameraViewListener { private static final String TAG = "CHESSTRACK::Activity"; + private static final String IPV4_REGEX ="^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; + private static final String PORT_REGEX ="^(\\d{4,5})$"; + private static final String welcomeMSG = "" + + "--------------------------------------------------------------------------\n" + + " .:: CHESSTRACK ::. seguimiento de una partida de Ajedrez\n" + + "\n" + + " Aylen Ricca - Nicolas Furquez\n" + + "--------------------------------------------------------------------------\n"; private CameraBridgeViewBase mOpenCvCameraView; @@ -92,15 +100,21 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener alert.setTitle("Communication Setup"); alert.setMessage("Set server ip"); - // Set an EditText view to get user input final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Editable value = input.getText(); - Log.i(TAG,"INPUT=" + value.toString()); - ClientCommunication = new Client(value.toString()); + if (value == null || value.toString().equals("") || !value.toString().matches(IPV4_REGEX)) { + Log.i(TAG,"INPUT= not valid IP" + value.toString()); + getServerIp(); + } else { + Log.i(TAG,"INPUT=" + value.toString()); + + Log.i(TAG,"IP=" + value.toString()); + getServerPort(value.toString()); + } } }); @@ -115,6 +129,35 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener alert.show(); } + public void getServerPort(final String ip){ + AlertDialog.Builder alert = new AlertDialog.Builder(this); + alert.setTitle("Communication Setup"); + alert.setMessage("Set server port"); + + final EditText input = new EditText(this); + alert.setView(input); + + alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + Editable value = input.getText(); + Log.i(TAG,"INPUT=" + value.toString()); + if (value == null || value.toString().equals("") || !value.toString().matches(PORT_REGEX)) { + Log.i(TAG,"INPUT= not valid PORT" + value.toString()); + getServerPort(ip); + } else { + Log.i(TAG,"INPUT=" + value.toString()); + + int port = Integer.parseInt(value.toString()); + Log.i(TAG,"PORT=" + port); + ClientCommunication = Client.getInstance(); + ClientCommunication.EstablishConnection(ip, port); + ClientCommunication.SendData(welcomeMSG); + } + } + }); + + alert.show(); + } public void addListenerOnButton() { btnCalibrar = (Button) findViewById(R.id.btn_calibrar); @@ -162,7 +205,9 @@ public class ChessTrackActivity extends Activity implements CvCameraViewListener if (mOpenCvCameraView != null) { mOpenCvCameraView.disableView(); } - this.ClientCommunication.Stop(); + if (this.ClientCommunication != null) { + this.ClientCommunication.Stop(); + } } @Override diff --git a/cte-serv/server.java b/cte-serv/server.java index b5adea1..c06b878 100644 --- a/cte-serv/server.java +++ b/cte-serv/server.java @@ -8,7 +8,7 @@ class TCPServer { String clientSentence; String capitalizedSentence; - ServerSocket welcomeSocket = new ServerSocket(5555); + ServerSocket welcomeSocket = new ServerSocket(5556); Socket connectionSocket = welcomeSocket.accept(); @@ -25,7 +25,9 @@ class TCPServer { //capitalizedSentence = clientSentence.toUpperCase() + '\n'; //outToClient.writeBytes(capitalizedSentence); - System.out.println(clientSentence); + if (clientSentence != null){ + System.out.println(clientSentence); + } } } } -- GitLab