Android app updated (now the Android app acts even as client using the

device's accelerometers).
This commit is contained in:
Daniele Verducci
2014-03-16 12:43:55 +01:00
parent ecb44abd7a
commit 8aa13953ee
38 changed files with 418 additions and 207 deletions

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ExplorerBotCommonLib</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,23 @@
/*
Copyright 2014 Daniele Verducci
This file is part of ExplorerBot.
ExplorerBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ExplorerBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ExplorerBot. If not, see <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.explorerbot;
public class AppConfiguration {
public static final int PORT=6787; //Server default port
}

View File

@@ -0,0 +1,102 @@
/*
Copyright 2014 Daniele Verducci
This file is part of ExplorerBot.
ExplorerBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ExplorerBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ExplorerBot. If not, see <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.explorerbot.client;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import it.danieleverducci.explorerbot.AppConfiguration;
import it.danieleverducci.explorerbot.objects.GamepadPosition;
public class ClientNetworkCommunicationThread extends Thread {
private String ipAddress;
private boolean mustExit=false;
private GamepadPosition lastKnownPosition;
public ClientNetworkCommunicationThread(String ipAddress) {
super();
this.ipAddress = ipAddress;
}
@Override
public void run() {
try {
//Setup connection
Socket socket = new Socket(ipAddress, AppConfiguration.PORT);
OutputStream os = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
//Send gamepad position every 100msec
while(!mustExit){
if(lastKnownPosition!=null){
//Send to network
try{
oos.writeObject(lastKnownPosition);
oos.flush();
System.out.println(lastKnownPosition.getX()+" - "+lastKnownPosition.getY());
lastKnownPosition=null;
oos.reset();
}catch(SocketException e){
System.out.println("Connection interrupted by server. Exiting.");
System.exit(0);
}
}
//Wait 100ms
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
socket.close();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* Set @param mustExit to true to make the thread shutdown
*/
public void setMustExit(boolean mustExit) {
this.mustExit = mustExit;
}
/**
* @return the last gamepad's known position
*/
public GamepadPosition getLastKnownPosition() {
return lastKnownPosition;
}
/**
* Sets the @param lastKnownPosition to be sent to the server
*/
public void setLastKnownPosition(GamepadPosition lastKnownPosition) {
this.lastKnownPosition = lastKnownPosition;
}
}

View File

@@ -0,0 +1,26 @@
/*
Copyright 2014 Daniele Verducci
This file is part of ExplorerBot.
ExplorerBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ExplorerBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ExplorerBot. If not, see <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.explorerbot.interfaces;
import it.danieleverducci.explorerbot.objects.GamepadPosition;
public interface OnControllerPolledListener {
public void onControllerPolled(GamepadPosition position);
}

View File

@@ -0,0 +1,45 @@
/*
Copyright 2014 Daniele Verducci
This file is part of ExplorerBot.
ExplorerBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ExplorerBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ExplorerBot. If not, see <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.explorerbot.objects;
import java.io.Serializable;
/**
* Object representing the gamepad's analog stick position
*/
public class GamepadPosition implements Serializable {
private static final long serialVersionUID = 8066726751180370259L;
private float x;
private float y;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
}

View File

@@ -0,0 +1,72 @@
/*
Copyright 2014 Daniele Verducci
This file is part of ExplorerBot.
ExplorerBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ExplorerBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ExplorerBot. If not, see <http://www.gnu.org/licenses/>.
*/
package it.danieleverducci.explorerbot.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import it.danieleverducci.explorerbot.AppConfiguration;
import it.danieleverducci.explorerbot.interfaces.OnControllerPolledListener;
import it.danieleverducci.explorerbot.objects.GamepadPosition;
public class ServerNetworkCommunicationThread extends Thread {
private OnControllerPolledListener listener;
private boolean mustExit=false;
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(AppConfiguration.PORT);
System.out.println("Server started on port "+AppConfiguration.PORT);
Socket client = server.accept();
System.out.println("Client connected");
server.close(); //Once the client is connected, closing the server denies other clients connections
InputStream is = client.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
while(!mustExit){
//Read object and notify new controller position (readObject will return only when there is something to read)
GamepadPosition pos = (GamepadPosition)ois.readObject();
listener.onControllerPolled(pos);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Register the @param listener to be notified about controller events received through the network
*/
public void setOnControllerPolledListener(OnControllerPolledListener listener) {
this.listener = listener;
}
/**
* Set @param mustStop to true to make the thread shutdown
*/
public void setMustExit(boolean mustExit) {
this.mustExit = mustExit;
}
}