16 changed files with 329 additions and 9 deletions
@ -0,0 +1,21 @@ |
|||||||
|
package net.sopod.soim.client.cmd; |
||||||
|
|
||||||
|
import com.beust.jcommander.Parameter; |
||||||
|
import lombok.Data; |
||||||
|
|
||||||
|
/** |
||||||
|
* ArgsLogin |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:42 |
||||||
|
*/ |
||||||
|
@Data |
||||||
|
public class ArgsLogin { |
||||||
|
|
||||||
|
@Parameter(names = {"-u", "-account"}, description = "账号") |
||||||
|
private String account; |
||||||
|
|
||||||
|
@Parameter(names = {"-p", "-password"}, description = "密码", password = true) |
||||||
|
private String password; |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,52 @@ |
|||||||
|
package net.sopod.soim.client.cmd; |
||||||
|
|
||||||
|
import com.beust.jcommander.JCommander; |
||||||
|
import com.google.inject.Injector; |
||||||
|
import net.sopod.soim.client.logger.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* CommandDispatcher |
||||||
|
* |
||||||
|
* auth -u prometheus -p 123456 |
||||||
|
* send zeus hello |
||||||
|
* send prometheus hi! |
||||||
|
* users |
||||||
|
* groups |
||||||
|
* |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:28 |
||||||
|
*/ |
||||||
|
public class CmdDispatcher { |
||||||
|
|
||||||
|
private final Injector injector; |
||||||
|
|
||||||
|
public CmdDispatcher(Injector injector) { |
||||||
|
this.injector = injector; |
||||||
|
} |
||||||
|
|
||||||
|
public void dispatchCmd(String cmdStr) { |
||||||
|
Logger.info("dispatch cmd: {}", cmdStr); |
||||||
|
int idx = cmdStr.indexOf(" "); |
||||||
|
String cmd = idx == -1 ? cmdStr : cmdStr.substring(0, idx); |
||||||
|
String argsStr = idx == -1 ? "" : cmdStr.substring(idx + 1); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
public static void main(String[] args) { |
||||||
|
String cmdStr = "auth -u prometheus -p 123456"; |
||||||
|
int idx = cmdStr.indexOf(" "); |
||||||
|
String cmd = idx == -1 ? cmdStr : cmdStr.substring(0, idx); |
||||||
|
String argsStr = idx == -1 ? "" : cmdStr.substring(idx + 1); |
||||||
|
|
||||||
|
|
||||||
|
ArgsLogin argsLogin = new ArgsLogin(); |
||||||
|
String[] argv = {"-u", "zeus", "-p", "123456"}; |
||||||
|
JCommander.newBuilder() |
||||||
|
.addObject(argsLogin) |
||||||
|
.build() |
||||||
|
.parse(argv); |
||||||
|
System.out.println(argsLogin); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
package net.sopod.soim.client.cmd; |
||||||
|
|
||||||
|
/** |
||||||
|
* Commands |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:51 |
||||||
|
*/ |
||||||
|
public enum CmdEnum { |
||||||
|
|
||||||
|
/** 登录 */ |
||||||
|
login, |
||||||
|
|
||||||
|
/** 发送消息 */ |
||||||
|
send, |
||||||
|
|
||||||
|
/** 用户列表 */ |
||||||
|
users, |
||||||
|
|
||||||
|
/** 用户分组列表 */ |
||||||
|
groups, |
||||||
|
|
||||||
|
/** 退出 */ |
||||||
|
exit; |
||||||
|
|
||||||
|
|
||||||
|
CmdEnum() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
package net.sopod.soim.client.cmd; |
||||||
|
|
||||||
|
import com.google.inject.Injector; |
||||||
|
import net.sopod.soim.client.logger.Logger; |
||||||
|
|
||||||
|
import java.util.Scanner; |
||||||
|
|
||||||
|
/** |
||||||
|
* Args |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:28 |
||||||
|
*/ |
||||||
|
public class CmdStarter { |
||||||
|
|
||||||
|
private final CmdDispatcher cmdDispatcher; |
||||||
|
|
||||||
|
public CmdStarter(CmdDispatcher cmdDispatcher) { |
||||||
|
this.cmdDispatcher = cmdDispatcher; |
||||||
|
} |
||||||
|
|
||||||
|
public void start() { |
||||||
|
Scanner scanner = new Scanner(System.in); |
||||||
|
Logger.pre("[client]: "); |
||||||
|
while(scanner.hasNextLine()) { |
||||||
|
String cmd = scanner.nextLine(); |
||||||
|
cmdDispatcher.dispatchCmd(cmd); |
||||||
|
Logger.pre("[client]: "); |
||||||
|
} |
||||||
|
|
||||||
|
scanner.close(); |
||||||
|
} |
||||||
|
|
||||||
|
public void exit() { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,43 @@ |
|||||||
|
package net.sopod.soim.client.config; |
||||||
|
|
||||||
|
import com.google.inject.AbstractModule; |
||||||
|
import com.google.inject.Singleton; |
||||||
|
import org.reflections.Reflections; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
/** |
||||||
|
* Ioc |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 11:02 |
||||||
|
*/ |
||||||
|
public class GuiceIoc extends AbstractModule { |
||||||
|
|
||||||
|
private final String rootPackage; |
||||||
|
|
||||||
|
private Set<Class<?>> beanTypes = Collections.emptySet(); |
||||||
|
|
||||||
|
public GuiceIoc(Class<?> rootClass) { |
||||||
|
this.rootPackage = rootClass.getPackage().getName(); |
||||||
|
this.scanRootPackage(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void configure() { |
||||||
|
for (Class<?> beanType : this.beanTypes) { |
||||||
|
bind(beanType); |
||||||
|
} |
||||||
|
this.beanTypes = Collections.emptySet(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 扫描需要注入的类 |
||||||
|
*/ |
||||||
|
private void scanRootPackage() { |
||||||
|
Reflections reflect = new Reflections(rootPackage); |
||||||
|
this.beanTypes = reflect.getTypesAnnotatedWith(Singleton.class); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,15 @@ |
|||||||
|
package net.sopod.soim.client.handler; |
||||||
|
|
||||||
|
/** |
||||||
|
* CmdHandler |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:56 |
||||||
|
*/ |
||||||
|
public interface CmdHandler<T> { |
||||||
|
|
||||||
|
T newArgsInstance(); |
||||||
|
|
||||||
|
void handleArgs(T args); |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
package net.sopod.soim.client.handler; |
||||||
|
|
||||||
|
import com.google.inject.Singleton; |
||||||
|
|
||||||
|
/** |
||||||
|
* ExitHandler |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 11:53 |
||||||
|
*/ |
||||||
|
@Singleton |
||||||
|
public class ExitHandler extends NonArgsHandler { |
||||||
|
|
||||||
|
@Override |
||||||
|
public void handle() { |
||||||
|
System.exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,25 @@ |
|||||||
|
package net.sopod.soim.client.handler; |
||||||
|
|
||||||
|
import com.google.inject.Singleton; |
||||||
|
import net.sopod.soim.client.cmd.ArgsLogin; |
||||||
|
|
||||||
|
/** |
||||||
|
* LoginCmdHandler |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 10:53 |
||||||
|
*/ |
||||||
|
@Singleton |
||||||
|
public class LoginCmdHandler implements CmdHandler<ArgsLogin> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public ArgsLogin newArgsInstance() { |
||||||
|
return new ArgsLogin(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void handleArgs(ArgsLogin args) { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
package net.sopod.soim.client.handler; |
||||||
|
|
||||||
|
/** |
||||||
|
* NoArgsHandler |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 12:00 |
||||||
|
*/ |
||||||
|
public abstract class NonArgsHandler implements CmdHandler<Object> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public final Object newArgsInstance() { |
||||||
|
return new Object(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public final void handleArgs(Object args) { |
||||||
|
this.handle(); |
||||||
|
} |
||||||
|
|
||||||
|
abstract void handle(); |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
package net.sopod.soim.client.logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* Logger |
||||||
|
* |
||||||
|
* @author tmy |
||||||
|
* @date 2022-04-25 11:44 |
||||||
|
*/ |
||||||
|
public class Logger { |
||||||
|
|
||||||
|
public static void info(String msg, Object...args) { |
||||||
|
msg = msg.replaceAll("\\{}", "%s"); |
||||||
|
for (int i = 0; i < args.length; i++) { |
||||||
|
args[i] = String.valueOf(args[i]); |
||||||
|
} |
||||||
|
System.out.println("[info]: " + String.format(msg, args)); |
||||||
|
} |
||||||
|
|
||||||
|
public static void pre(String pre) { |
||||||
|
System.out.print(pre); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue