Skip to content

Commit 9c100d0

Browse files
author
朱辉\harry
committed
添加启动
1 parent 3f9a871 commit 9c100d0

13 files changed

Lines changed: 372 additions & 66 deletions

File tree

‎docs/接口说明-暂定.txt‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11

2+
LocalClient: 它由LocalServer启动,不允许自己开进程启动
3+
LocalServer: 作为自动化本地服务器,和用户端通信,直接启动LocalClient,不能转发消息到RemoteClient
4+
RemoteClient: 它将连接RemoteServer,不能与LocalServer通信,作为一个单独的线程启动,通过RemoteServer中转来和用户端通信
5+
RemoteServer: 作为远程中转服务器
6+
7+
28
命令://内容
39
被 {} 包裹的内容,说明这个是JSON格式
410

5-
wait://{sn:"SerialNumber"}
6-
open://{sn:"SerialNumber"}
11+
wait://{sn:"SerialNumber", key:"xxxxxxx"}
12+
open://{sn:"SerialNumber", key:"xxxxxxx"}
713
start://{type:"minicap"|"minitouch", config:{rotate:xx, scale:xx}}
814
touch://minitouch的命令格式
915
input://要输入的文本

‎src/main/java/com/yeetor/Main.java‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.neovisionaries.ws.client.WebSocketException;
44
import com.yeetor.androidcontrol.RemoteClient;
5-
import com.yeetor.minicap.MinicapInstallException;
65
import com.yeetor.androidcontrol.LocalServer;
6+
import com.yeetor.androidcontrol.RemoteServer;
77

88
import java.io.IOException;
99
import java.security.InvalidParameterException;
@@ -18,54 +18,70 @@ public class Main {
1818
* [client ip port serialNumber]
1919
* [client ip port]
2020
*/
21-
public static void main(String[] args) throws MinicapInstallException {
21+
public static void main(String[] args) {
2222

2323
try {
2424
Config config = new Config(args);
25-
if (config.isLocal) {
26-
new LocalServer(config.port).start();
25+
26+
if (config.isClient) {
27+
new RemoteClient(config.ip, config.port, config.key, config.serialNumber);
2728
} else {
28-
new RemoteClient(config.ip, config.port, config.serialNumber);
29+
if (config.isLocal) {
30+
new LocalServer(config.port).start();
31+
} else {
32+
new RemoteServer(config.port).start();
33+
}
2934
}
3035
} catch (InvalidParameterException ex) {
31-
System.out.println("localserver <port>: 启动本地服务器(p2p)\nclient <ip> <port> [serialNumber] 启动客户端");
36+
System.out.println("localserver <port>: 启动本地服务器(p2p)\n remoteserver <port> 启动服务器 \nremoteclient <ip> <port> <key> [serialNumber] 启动客户端");
3237
System.exit(0);
3338
} catch (WebSocketException|InterruptedException e) {
3439
System.out.println("启动服务器失败: " + e.getMessage());
3540
System.exit(0);
36-
} catch (IOException e) {
41+
} catch (Exception e) {
3742
e.printStackTrace();
3843
System.exit(0);
3944
}
4045
}
4146

4247
static class Config {
4348
boolean isLocal = true;
49+
boolean isClient = false;
4450
String ip = "";
4551
int port = 6655;
4652
String serialNumber = "";
53+
String key = "";
4754

4855
Config(String[] args) throws InvalidParameterException {
4956
if (args.length > 0) {
50-
if ("remoteserver".equals(args[0])) {
57+
if ("localserver".equals(args[0])) {
58+
isLocal = true;
59+
isClient = false;
60+
} else if ("remoteserver".equals(args[0])) {
5161
isLocal = false;
62+
isClient = false;
63+
} else {
64+
isLocal = true;
65+
isClient = true;
5266
}
5367
}
5468

55-
if (isLocal) {
56-
if (args.length == 2) {
57-
port = Integer.parseInt(args[1]);
58-
} else {
59-
throw new InvalidParameterException("参数不正确");
60-
}
61-
} else {
62-
if (args.length == 3) {
69+
if (isClient) {
70+
if (args.length == 4) {
6371
ip = args[1];
6472
port = Integer.parseInt(args[2]);
65-
} else if (args.length == 4) {
73+
key = args[3];
74+
} else if (args.length == 5) {
6675
ip = args[1];
6776
port = Integer.parseInt(args[2]);
68-
serialNumber = args[3];
77+
key = args[3];
78+
serialNumber = args[4];
79+
} else {
80+
throw new InvalidParameterException("参数不正确");
81+
}
82+
} else {
83+
if (args.length == 2) {
84+
port = Integer.parseInt(args[1]);
6985
} else {
7086
throw new InvalidParameterException("参数不正确");
7187
}

‎src/main/java/com/yeetor/androidcontrol/ChildChannel.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.netty.channel.socket.SocketChannel;
55
import io.netty.handler.codec.http.HttpObjectAggregator;
66
import io.netty.handler.codec.http.HttpServerCodec;
7+
import io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
78
import io.netty.handler.stream.ChunkedWriteHandler;
89

910
/**

‎src/main/java/com/yeetor/androidcontrol/Command.java‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public enum Schem {
1414
WAIT("wait"),
1515
OPEN("open"),
1616
START("start"),
17-
WATTING("watting"),
17+
WAITTING("waitting"),
1818
TOUCH("touch"),
1919
DEVICES("devices"),
2020
KEYEVENT("keyevent"),
2121
INPUT("input"),
22-
SHOT("shot");
22+
SHOT("shot"),
23+
MINICAP("minicap"),
24+
MINITOUCH("minitouch");
2325

2426
private String schemStr;
2527

@@ -43,7 +45,6 @@ public Command(String command) throws InvalidParameterException {
4345
}
4446

4547
String schemStr = command.substring(0, splitIndex);
46-
4748
switch (schemStr) {
4849
case "wait":
4950
schem = Schem.WAIT;
@@ -55,7 +56,7 @@ public Command(String command) throws InvalidParameterException {
5556
schem = Schem.START;
5657
break;
5758
case "waitting":
58-
schem = Schem.WATTING;
59+
schem = Schem.WAITTING;
5960
break;
6061
case "touch":
6162
schem = Schem.TOUCH;
@@ -72,14 +73,20 @@ public Command(String command) throws InvalidParameterException {
7273
case "input":
7374
schem = Schem.INPUT;
7475
break;
76+
case "minicap":
77+
schem = Schem.MINICAP;
78+
break;
79+
case "minitouch":
80+
schem = Schem.MINITOUCH;
81+
break;
7582
default:
7683
throw new InvalidParameterException(command + " 未知的schem");
7784
}
7885

7986
String contentStr = command.substring(splitIndex + 3);
8087

8188
// minitouch keyevent,此消息不是json格式。其他都为json键值对
82-
if (!schem.equals(Schem.TOUCH) && !schem.equals(Schem.KEYEVENT) && !schem.equals(Schem.INPUT)) {
89+
if (!schem.equals(Schem.TOUCH) && !schem.equals(Schem.KEYEVENT) && !schem.equals(Schem.INPUT) && !schem.equals(Schem.MINICAP) && !schem.equals(Schem.MINITOUCH)) {
8390
try {
8491
this.content = parseContentJson(contentStr);
8592
} catch (JSONException e) {
@@ -106,6 +113,10 @@ public Schem getSchem() {
106113
return schem;
107114
}
108115

116+
public String getCommandString() {
117+
return schem.getSchemString() + "://" + getContent();
118+
}
119+
109120
public String getContent() {
110121
if (content != null) {
111122
if (content instanceof String) {

‎src/main/java/com/yeetor/androidcontrol/LocalClient.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public LocalClient(Protocol protocol) {
3030

3131

3232
public void executeCommand(ChannelHandlerContext ctx, Command command) {
33-
if (command.getSchem() != Command.Schem.WATTING) {
33+
if (command.getSchem() != Command.Schem.WAITTING) {
3434
System.out.println(command.getContent());
3535
}
3636
switch (command.getSchem()) {
@@ -39,7 +39,7 @@ public void executeCommand(ChannelHandlerContext ctx, Command command) {
3939
break;
4040
case TOUCH:
4141
touchCommand(ctx, command);
42-
case WATTING:
42+
case WAITTING:
4343
waittingCommand(ctx, command);
4444
break;
4545
case KEYEVENT:
@@ -202,7 +202,7 @@ private void startMinicap(Command command) {
202202
if (scale < 0.01) {scale = 0.01f;}
203203
if (scale > 1.0) {scale = 1.0f;}
204204
if (rotate == null) { rotate = 0.0f; }
205-
Minicap minicap = new Minicap(protocol.getKey());
205+
Minicap minicap = new Minicap(protocol.getSn());
206206
minicap.addEventListener(this);
207207
minicap.start(scale, rotate.intValue());
208208
protocol.setMinicap(minicap);
@@ -213,7 +213,7 @@ private void startMinitouch(Command command) {
213213
protocol.getMinitouch().kill();
214214
}
215215

216-
Minitouch minitouch = new Minitouch(protocol.getKey());
216+
Minitouch minitouch = new Minitouch(protocol.getSn());
217217
minitouch.addEventListener(this);
218218
minitouch.start();
219219
protocol.setMinitouch(minitouch);

‎src/main/java/com/yeetor/androidcontrol/LocalServer.java‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.yeetor.androidcontrol;
22

33
import com.alibaba.fastjson.JSON;
4+
import com.alibaba.fastjson.JSONObject;
45
import com.android.ddmlib.IDevice;
56
import com.yeetor.adb.AdbServer;
67
import com.yeetor.minicap.*;
@@ -12,6 +13,7 @@
1213
import io.netty.handler.codec.http.*;
1314
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
1415
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
16+
import org.apache.commons.lang3.StringUtils;
1517

1618
import java.util.ArrayList;
1719
import java.util.LinkedList;
@@ -43,7 +45,7 @@ public void start() throws InterruptedException {
4345
channel(NioServerSocketChannel.class).
4446
childOption(ChannelOption.SO_KEEPALIVE, true).
4547
childHandler(new ChildChannel(new LocalServerWebsocketEventImp()));
46-
System.out.println("Server will start at port: " + port);
48+
System.out.println("LocalServer will start at port: " + port);
4749
System.out.println("--------\r\n");
4850
ChannelFuture future = bootstrap.bind(port).sync();
4951
future.channel().closeFuture().sync();
@@ -77,14 +79,14 @@ public void onDisconnect(ChannelHandlerContext ctx) {
7779
@Override
7880
public void onTextMessage(ChannelHandlerContext ctx, String text) {
7981
Command command = Command.ParseCommand(text);
82+
System.out.println(text);
8083
if (command != null) {
81-
8284
switch (command.getSchem()) {
8385
case WAIT:
8486
initLocalClient(ctx, command);
8587
break;
8688
case START:
87-
case WATTING:
89+
case WAITTING:
8890
case TOUCH:
8991
case KEYEVENT:
9092
case INPUT:
@@ -120,15 +122,27 @@ DefaultFullHttpResponse onHttpRequest(ChannelHandlerContext ctx, FullHttpRequest
120122
void initLocalClient(final ChannelHandlerContext ctx, Command command) {
121123

122124
String sn = command.getString("sn", null);
123-
if (sn == null) {
124-
ctx.channel().close();
125-
return;
125+
String key = command.getString("key", null);
126+
127+
// 没有sn,默认第一个设备
128+
if (StringUtils.isEmpty(sn)) {
129+
IDevice iDevice = AdbServer.server().getFirstDevice();
130+
if (iDevice == null) {
131+
ctx.channel().close();
132+
return;
133+
}
134+
sn = iDevice.getSerialNumber();
126135
}
127-
System.out.println("open://");
128-
ctx.channel().writeAndFlush(new TextWebSocketFrame("open://" + sn));
136+
137+
JSONObject obj = new JSONObject();
138+
obj.put("sn", sn);
139+
obj.put("key", key);
140+
141+
ctx.channel().writeAndFlush(new TextWebSocketFrame("open://" + obj.toJSONString()));
129142

130143
Protocol protocol = new Protocol();
131-
protocol.setKey(sn);
144+
protocol.setSn(sn);
145+
protocol.setKey(key);
132146
protocol.setBroswerSocket(ctx);
133147
protocolList.add(protocol);
134148

‎src/main/java/com/yeetor/androidcontrol/Protocol.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Protocol {
1212
public ChannelHandlerContext broswerSocket;
1313
public ChannelHandlerContext clientSocket;
1414
public String key;
15+
public String sn;
1516
public Minicap minicap;
1617
public Minitouch minitouch;
1718
public LocalClient localClient;
@@ -36,6 +37,10 @@ public void setClientSocket(ChannelHandlerContext clientSocket) {
3637
this.clientSocket = clientSocket;
3738
}
3839

40+
public void setSn(String sn) {
41+
this.sn = sn;
42+
}
43+
3944
public void setKey(String key) {
4045
this.key = key;
4146
}
@@ -64,6 +69,11 @@ public String getKey() {
6469
return key;
6570
}
6671

72+
73+
public String getSn() {
74+
return sn;
75+
}
76+
6777
public Minicap getMinicap() {
6878
return minicap;
6979
}

0 commit comments

Comments
 (0)