【Java开发】命令解析框架CommandX
生活随笔
收集整理的這篇文章主要介紹了
【Java开发】命令解析框架CommandX
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
雛形代碼結(jié)構(gòu)
主要涉及到命令的分發(fā),解析,數(shù)據(jù)報文的解析
public abstract class BaseProcessor<T> implements CommandHandler {protected final Dispatcher dispatcher;protected String command = "";protected T commandData;public BaseProcessor(Dispatcher dispatcher) {this.dispatcher = dispatcher;}@Overridepublic void handle(String command) {this.command = command;T data = parseCommand(command);if (data != null) {commandData = data;}responseCommand();executeCommand();}@NonNullpublic abstract String targetCommand();public abstract T parseCommand(String command);public abstract void responseCommand();public abstract void executeCommand();}分發(fā)器
public class Dispatcher {private final Map<String, BaseProcessor<?>> processorMap = new HashMap<>(8);private final PriorityBlockingQueue<String> commandQueue = new PriorityBlockingQueue<>(8);private final Runnable workTask = () -> {while (true) {// Traverse the command queue.String command = commandQueue.remove();if (command != null) {dispatch(command);}}};private Thread workThread;public Dispatcher() {initProcessorMap();loopWork();}private void initProcessorMap() {TestCommandProcessor testCommandProcessor = new TestCommandProcessor(this);GetPropertyCommandProcessor getPropertyCommandProcessor = new GetPropertyCommandProcessor(this);processorMap.put(testCommandProcessor.targetCommand(), testCommandProcessor);processorMap.put(getPropertyCommandProcessor.targetCommand(), getPropertyCommandProcessor);}@Nullablepublic BaseProcessor<?> getProcessor(@NonNull String command) {Set<Map.Entry<String, BaseProcessor<?>>> entrySet = processorMap.entrySet();for (Map.Entry<String, BaseProcessor<?>> entry : entrySet) {//if (command.startsWith(entry.getKey())) {return entry.getValue();}}return null;}public void addCommand(String command) {commandQueue.add(command);}public void stop() {if (workThread.isAlive() || !workThread.isInterrupted()) {workThread.interrupt();}}private void dispatch(String command) {// put command to dispatch queue.BaseProcessor<?> processor = getProcessor(command);if (processor == null) {return;}processor.handle(command);}private void loopWork() {workThread = new Thread(workTask, "DispatchThread");workThread.start();} }具體某個CommandProcessor
public class GetPropertyCommandProcessor extends BaseProcessor<GetPropertyCommandProcessor.GetPropertyCommandData> {public GetPropertyCommandProcessor(Dispatcher dispatcher) {super(dispatcher);}@NonNull@Overridepublic String targetCommand() {return "GET_PROPERTY=";}@Overridepublic GetPropertyCommandData parseCommand(String command) {// GET_PROPERTY=ABC,DEF\nString sub = command.substring(targetCommand().length(), command.indexOf('\n'));// Parse sub string.String[] data = sub.split(",");return new GetPropertyCommandData(data);}@Overridepublic void responseCommand() {//}@Overridepublic void executeCommand() {String response = String.format(Locale.US, "POST_PROPERTY=%s,%s,%d\n",commandData.firstValue, commandData.secondValue, 88);Log.d("Test", response);}public static class GetPropertyCommandData {public final String firstValue;public final String secondValue;public GetPropertyCommandData(String[] data) {firstValue = data[0];secondValue = data[1];}} }接口
public interface CommandHandler {void handle(String command);}總結(jié)
以上是生活随笔為你收集整理的【Java开发】命令解析框架CommandX的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jenkins 命令执行 (CVE-20
- 下一篇: 小峰峰的pat甲级刷题记录1030