Skip to content
Snippets Groups Projects
Commit e7898c04 authored by Aylen Ricca's avatar Aylen Ricca
Browse files

modifications to configure ip port-with validation and welcome msg

parent 06c39701
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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
......
......@@ -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);
}
}
}
}
......
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