Skip to content

Commit

Permalink
[Socket]Add ControlClient
Browse files Browse the repository at this point in the history
Remote control:
	1. Add ControlClient to fetch schedule config
	from remote server.

Signed-off-by: Ethan Shan <[email protected]>
  • Loading branch information
ethanshan committed Sep 5, 2014
1 parent 3848580 commit f98eb84
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CheeseBrowser_Studio/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:minSdkVersion="11"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -63,7 +63,7 @@
</receiver>

<!-- Setup server with TCP socket -->
<service android:name=".ControlServer"/>
<service android:name=".ControlClient"/>
</application>

</manifest>
4 changes: 2 additions & 2 deletions CheeseBrowser_Studio/CheeseBrowser_Studio.iml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="Android 4.2.2 Platform" jdkType="Android SDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*___Generated_by_IDEA___*/

/** Automatically generated file. DO NOT MODIFY */
package net.codingpark.cheesebrowser;

/* This stub is only used by the IDE. It is NOT the BuildConfig class actually packed into the APK */
public final class BuildConfig {
public final static boolean DEBUG = true;
public final static boolean DEBUG = Boolean.parseBoolean(null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public void onClick(View view) {
public void onClick(View view) {
Log.d(TAG, "start_server_button clicked");
Intent r_intent = new Intent();
r_intent.setClass(BrowserActivity.this, ControlServer.class);
r_intent.setClass(BrowserActivity.this, ControlClient.class);
BrowserActivity.this.startService(r_intent);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
Expand All @@ -20,11 +21,11 @@
* Used to obtain startup/shutdown schedule time, and
* receive shutdown/reboot command
*/
public class ControlServer extends Service {
public class ControlClient extends Service {

private static final String TAG = "ControlServer";
private static final String TAG = "ControlClient";

private static int SERVER_PORT = 5678;
private static int SERVER_PORT = 8888;

/**
* A stack that contains all connected control clients.
Expand All @@ -33,22 +34,62 @@ public class ControlServer extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting control server.");
try {
setupServer();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "Starting control client.");
//try {
//setupServer();
syncConfig();
//} catch (IOException e) {
//e.printStackTrace();
//}
return super.onStartCommand(intent, flags, startId);
}

public IBinder onBind(Intent intent) {
return null;
}


private void syncConfig() {
Thread thread = new Thread() {
@Override
public void run() {
super.run();
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try {
// Create socket to connect server
socket = new Socket("192.168.1.160", SERVER_PORT);
System.out.println("Socket=" + socket);
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
//pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
//socket.getOutputStream())));
String msg = br.readLine();
Log.d(TAG, "Receive message: \n" + msg);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
System.out.println("close......");
if (br != null)
br.close();
//pw.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
thread.start();
}

/**
* Sets up the TCP/IP server socket.
*/
/*
private void setupServer() throws IOException {
Log.d(TAG, "Setting up server...");
final ServerSocket socket = new ServerSocket(SERVER_PORT);
Expand Down Expand Up @@ -81,4 +122,5 @@ public void run() {
Log.d(TAG, "Finished setting up server...");
}
*/
}

0 comments on commit f98eb84

Please sign in to comment.