server almost finished (but untested)

This commit is contained in:
Senad Uka
2014-01-21 07:11:49 +01:00
parent ebbc345a9e
commit ab14271505
2 changed files with 106 additions and 11 deletions

View File

@@ -4,35 +4,85 @@ import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.app.Activity;
import android.util.Log;
public class RemoteControlServer implements Runnable {
public class RemoteControlServer{
public static final String SERVERIP = "0.0.0.0";
public static final int SERVERPORT = 4444;
@Override
private Activity activity;
private OnCommandRecieved onCommandRecievedListener;
private String lastCommand = "";
public OnCommandRecieved getOnCommandRecievedListener() {
return onCommandRecievedListener;
}
public void setOnCommandRecievedListener(
OnCommandRecieved onCommandRecievedListener) {
this.onCommandRecievedListener = onCommandRecievedListener;
}
public RemoteControlServer(Activity activity) {
super();
this.activity = activity;
setOnCommandRecievedListener(new OnCommandRecieved() {
@Override
public void recieve(String command) {
// dummy implementation to be sure there is something to call
}
});
}
public void run() {
DatagramSocket socket = null;
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
socket = new DatagramSocket(SERVERPORT, serverAddr);
byte[] buf = new byte[17];
for(;;) {
// samples of commands, UTF8 encoded:
// S114A286 - surah 114, ayah 286 (nonexistent - but contains "max"
// values)
// S1A7 - surah 1, ayah 7
// F225 - font size 225
byte[] buf = new byte[20];
for (;;) {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
if (packet != null) {
lastCommand = new String(packet.getData(), "UTF-8");
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
getOnCommandRecievedListener().recieve(lastCommand);
}
});
}
}
} catch (Exception e) {
Log.e("SERVER", "Something is wrong with server: " + e.toString());
}
finally {
if(socket != null) socket.close();
} finally {
if (socket != null)
socket.close();
}
}
public interface OnCommandRecieved {
public void recieve(String command);
}
}

View File

@@ -1,9 +1,11 @@
package com.mhalka.qurantranslationdisplay;
import com.mhalka.qurantranslationdisplay.RemoteControlServer.OnCommandRecieved;
import com.mhalka.qurantranslationdisplay.VerticalMarqueeTextView.ScrollingStoppedListener;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
@@ -28,6 +30,10 @@ public class ShowAyah extends Activity {
private TextView chapterName;
int currentChapter=1;
int currentVerse=1;
private RemoteControlServer rcs;
private ServerTask server;
@Override
@@ -115,6 +121,8 @@ public class ShowAyah extends Activity {
}
@Override
protected void onResume() {
startServer();
// Start or restart the Marquee if paused.
if (ayah.isPaused()) {
@@ -128,6 +136,7 @@ public class ShowAyah extends Activity {
// Pause the Marquee when the Activity pauses.
ayah.pauseMarquee();
stopServer();
super.onPause();
}
@@ -137,5 +146,41 @@ public class ShowAyah extends Activity {
ayah.stopMarquee();
super.onDestroy();
}
private void startServer() {
server = new ServerTask();
rcs = new RemoteControlServer((ShowAyah.this));
rcs.setOnCommandRecievedListener(new OnCommandRecieved() {
@Override
public void recieve(String command) {
// TODO parse the command and do it !
}
});
server.execute("");
}
private void stopServer() {
server.cancel(true);
}
private class ServerTask extends AsyncTask<String,String, String> {
@Override
protected String doInBackground(String... params) {
rcs.run();
return "yes!";
}
};
}