Skip to content

Commit 19655b7

Browse files
committed
Add a new option to Params: startupCommands
startupCommands is an array of beanshell commands which will be executed on the interpeter for each telnet client before the client is handed control
1 parent 6327529 commit 19655b7

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

app/src/main/java/jwf/debugport/app/MainActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3737
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
3838
TextView status = (TextView) findViewById(R.id.server_status);
3939
if (isChecked) {
40-
Params params = new Params();
40+
Params params = new Params()
41+
.setStartupCommands(new String[]{
42+
"import android.os.*;",
43+
"import java.util.*;",
44+
"x = 1+1;",
45+
});
4146
DebugPortService.start(this, params);
4247
if (status != null) {
4348
status.setText(getString(R.string.server_status, getIpAddress(), params.getPort()));

library/src/main/java/jwf/debugport/Params.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,51 @@
44
import android.os.Parcelable;
55

66
/**
7-
*
7+
* Configuration parameters for the {@link DebugPortService}
88
*/
99
public class Params implements Parcelable {
1010
public static final int DEFAULT_PORT = 8562;
11+
private String[] mStartupCommands;
1112
private int mPort;
1213

1314
public Params() {
1415
mPort = DEFAULT_PORT;
16+
mStartupCommands = new String[0];
1517
}
1618

19+
/**
20+
* Set the port on which the {@link jwf.debugport.internal.TelnetServer} should be made available.
21+
*/
1722
public Params setPort(int port) {
1823
mPort = port;
1924
return this;
2025
}
2126

27+
/**
28+
* Get the port on which the {@link jwf.debugport.internal.TelnetServer} will be made available.
29+
*/
2230
public int getPort() {
2331
return mPort;
2432
}
2533

34+
/**
35+
* Set an array of commands which should be executed on the interpreter before the telnet client
36+
* is given control. This can be useful if you need to run a bunch of <code>import</code>
37+
* statements, or configure some state.
38+
*/
39+
public Params setStartupCommands(String[] commands) {
40+
mStartupCommands = commands;
41+
return this;
42+
}
43+
44+
/**
45+
* Get the startup commands which will be executed on the interpreter before each telnet client
46+
* is given control.
47+
*/
48+
public String[] getStartupCommands() {
49+
return mStartupCommands;
50+
}
51+
2652
@Override
2753
public int describeContents() {
2854
return hashCode();
@@ -31,13 +57,20 @@ public int describeContents() {
3157
@Override
3258
public void writeToParcel(Parcel dest, int flags) {
3359
dest.writeInt(mPort);
60+
dest.writeInt(mStartupCommands.length);
61+
dest.writeStringArray(mStartupCommands);
3462
}
3563

3664
public static final Creator<Params> CREATOR = new Creator<Params>() {
3765
@Override
3866
public Params createFromParcel(Parcel source) {
3967
Params p = new Params();
4068
p.setPort(source.readInt());
69+
70+
int startupCommandsLength = source.readInt();
71+
String[] startupCommands = new String[startupCommandsLength];
72+
source.readStringArray(startupCommands);
73+
p.setStartupCommands(startupCommands);
4174
return p;
4275
}
4376

library/src/main/java/jwf/debugport/internal/ClientConnection.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ class ClientConnection implements Runnable, Commands.ExitListener {
1919
private final Socket mClient;
2020
private final WeakReference<TelnetServer> mParent;
2121
private final Application mApp;
22+
private final String[] mStartupCommands;
2223
private TelnetConsoleInterface mConsole;
2324

24-
public ClientConnection(Context context, Socket client, TelnetServer parent) {
25+
public ClientConnection(Context context, Socket client, TelnetServer parent, String[] startupCommands) {
2526
mApp = (Application) context.getApplicationContext();
2627
mClient = client;
2728
mParent = new WeakReference<>(parent);
29+
mStartupCommands = startupCommands;
2830
}
2931

3032
public void close() {
@@ -49,6 +51,11 @@ public void run() {
4951
interpreter.set("cmd", new Commands(this));
5052
interpreter.set("app", mApp);
5153
interpreter.eval("importCommands(\"jwf.debugport.commands\")");
54+
55+
for (String startupCommand : mStartupCommands) {
56+
interpreter.eval(startupCommand);
57+
}
58+
5259
interpreter.setExitOnEOF(false);
5360
interpreter.run();
5461
} catch (Exception e) {

library/src/main/java/jwf/debugport/internal/TelnetServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void run() {
8585
}
8686

8787
synchronized (mLock) {
88-
clientConn = new ClientConnection(mApp, client, this);
88+
clientConn = new ClientConnection(mApp, client, this, mParams.getStartupCommands());
8989
mClients.add(clientConn);
9090
}
9191
new Thread(clientConn).start();

0 commit comments

Comments
 (0)