将消息传递方式改为文件传输, 以便于 docker 容器之间传输
This commit is contained in:
parent
2e9ab75bdf
commit
b313f11eb4
|
@ -8,7 +8,12 @@ import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class Consumer extends Thread {
|
public class Consumer extends Thread {
|
||||||
|
@ -41,11 +46,19 @@ public class Consumer extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// 在 Code 中的 Bot 类名后面," implements com.kob.botrunningsystem.utils.BotInterface" 之前加上 uid
|
// 在 Code 中的 Bot 类名后面," implements com.kob.botrunningsystem.utils.BotInterface" 之前加上 uid
|
||||||
private String addUid(String code, String uid) {
|
private String addUid(String code, String uid) {
|
||||||
int k = code.indexOf(" implements com.kob.botrunningsystem.utils.BotInterface");
|
int k = code.indexOf(" implements com.kob.botrunningsystem.utils.BotInterface");
|
||||||
return code.substring(0, k) + uid + code.substring(k);
|
return code.substring(0, k) + uid + code.substring(k);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 在 Code 中的 Bot 类名后面," implements com.kob.botrunningsystem.utils.BotInterface" 之前加上 uid
|
||||||
|
private String addUid(String code, String uid) {
|
||||||
|
int k = code.indexOf(" implements java.util.function.Supplier<Integer>");
|
||||||
|
return code.substring(0, k) + uid + code.substring(k);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -56,14 +69,37 @@ public class Consumer extends Thread {
|
||||||
// Reflect 来自 JOOR 依赖: 可以动态的编译 java 代码
|
// Reflect 来自 JOOR 依赖: 可以动态的编译 java 代码
|
||||||
// 重名类只会编译一遍,为了每次新线程都重新编译,
|
// 重名类只会编译一遍,为了每次新线程都重新编译,
|
||||||
// 在类名后加一个随机字符串 UUID(每次返回一个不一样的ID)
|
// 在类名后加一个随机字符串 UUID(每次返回一个不一样的ID)
|
||||||
|
/*
|
||||||
BotInterface botInterface = Reflect.compile(
|
BotInterface botInterface = Reflect.compile(
|
||||||
"com.kob.botrunningsystem.utils.Bot" + uid,
|
"com.kob.botrunningsystem.utils.Bot" + uid,
|
||||||
addUid(bot.getBotCode(), uid)
|
addUid(bot.getBotCode(), uid)
|
||||||
).create().get(); // compile(编译) 完这个类之后 create(创建) 一个类,并 get(获取) 到这个类
|
).create().get(); // compile(编译) 完这个类之后 create(创建) 一个类,并 get(获取) 到这个类
|
||||||
|
*/
|
||||||
|
// 替换接口类型:修改为文件传输形式
|
||||||
|
Supplier<Integer> botInterface = Reflect.compile(
|
||||||
|
"com.kob.botrunningsystem.utils.Bot" + uid,
|
||||||
|
addUid(bot.getBotCode(), uid)
|
||||||
|
).create().get(); // compile(编译) 完这个类之后 create(创建) 一个类,并 get(获取) 到这个类
|
||||||
|
|
||||||
|
|
||||||
|
// 将微服务之间的数据包通过文件传输,便于在 docker 容器之间进行数据交互
|
||||||
|
File file = new File("input.txt"); // 创建输出文件
|
||||||
|
try(PrintWriter fileOut = new PrintWriter(file)){
|
||||||
|
fileOut.println(bot.getInput()); // 将 bot.getInput() 输出出去
|
||||||
|
fileOut.flush(); // 清空缓冲区
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// 存储获取到的方向
|
// 存储获取到的方向
|
||||||
Integer direction = botInterface.nextMove(bot.getInput());
|
Integer direction = botInterface.nextMove(bot.getInput());
|
||||||
System.out.println("move-direction: " + bot.getUserId() + " " + direction); // 根据动态编译结果返回输入
|
System.out.println("move-direction: " + bot.getUserId() + " " + direction); // 根据动态编译结果返回输入
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 同步修改为文件传输
|
||||||
|
Integer direction = botInterface.get();
|
||||||
|
System.out.println("move-direction: " + bot.getUserId() + " " + direction); // 根据动态编译结果返回输入
|
||||||
|
|
||||||
// 打包信息
|
// 打包信息
|
||||||
MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
|
MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
// Bot 自动化代码示例
|
// Bot 自动化代码示例: 用于写前端 Bot 代码的辅助类, 在后端不参与运行
|
||||||
package com.kob.botrunningsystem.utils;
|
package com.kob.botrunningsystem.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Bot implements com.kob.botrunningsystem.utils.BotInterface {
|
//public class Bot implements com.kob.botrunningsystem.utils.BotInterface {
|
||||||
|
// 修改接口
|
||||||
|
public class Bot implements java.util.function.Supplier<Integer> {
|
||||||
static class Cell {
|
static class Cell {
|
||||||
public int x, y;
|
public int x, y;
|
||||||
|
|
||||||
public Cell(int x, int y) {
|
public Cell(int x, int y) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
@ -38,7 +44,7 @@ public class Bot implements com.kob.botrunningsystem.utils.BotInterface {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public Integer nextMove(String input) {
|
public Integer nextMove(String input) {
|
||||||
String[] strs = input.split("#");
|
String[] strs = input.split("#");
|
||||||
int rows = 18;
|
int rows = 18;
|
||||||
|
@ -72,4 +78,15 @@ public class Bot implements com.kob.botrunningsystem.utils.BotInterface {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer get() {
|
||||||
|
File file = new File("input.txt");
|
||||||
|
try {
|
||||||
|
Scanner sc = new Scanner(file);
|
||||||
|
return nextMove(sc.next()); // 读入字符串
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
11111111111111100000000000011100001001100110010010000101110000000000011100010000000110000000000001100000001000111000000000001110100001001001100110010000111000000000000111111111111111#1#12#(22222300303221221221)#11#1#(00000100000111111111)
|
Loading…
Reference in New Issue