001package gu.dtalk.client; 002 003import com.google.common.base.Predicates; 004import com.google.common.base.Strings; 005 006import gu.dtalk.ConnectReq; 007import gu.simplemq.Channel; 008import gu.simplemq.IMessageQueueFactory; 009import gu.simplemq.MessageQueueConfigManagers; 010import gu.simplemq.MessageQueueType; 011import gu.simplemq.exceptions.SmqNotFoundConnectionException; 012import net.gdface.cli.BaseAppConfig; 013import net.gdface.utils.BinaryUtils; 014 015import static com.google.common.base.Preconditions.checkNotNull; 016 017/** 018 * 简单字符终端实现 019 * @author guyadong 020 * 021 */ 022public class SampleConsole extends BaseConsole implements SampleConsoleConstants { 023 024 public SampleConsole(String devmac, IMessageQueueFactory factory) throws SmqNotFoundConnectionException { 025 super(devmac, factory); 026 } 027 /** 028 * 使用密码验证连接合法性<br> 029 * 向dtalk引擎发送包含连接密码和本机mac地址的json连接请求字符串({@link ConnectReq}), 030 * 收到回复的请求通道名,即连接成功 031 * @see gu.dtalk.client.BaseConsole#authorize() 032 */ 033 @Override 034 protected boolean authorize() { 035 System.out.println("Input password of Device,default password is last 4 character of device MAC address(lowercase):"); 036 ConnectReq req = new ConnectReq(); 037 req.mac = BinaryUtils.toHex(temminalMac); 038 Channel<ConnectReq> conch = new Channel<>(connchname, ConnectReq.class); 039 String pwd = null; 040 while ((reqChannel == null) && !(pwd=scanLine(Predicates.<String>alwaysTrue())).isEmpty()) { 041 req.pwd = BinaryUtils.getMD5String(pwd.getBytes()); 042 syncPublish(conch,req); 043 } 044 if(reqChannel != null){ 045 System.out.println("PASSWORD validate passed"); 046 return true; 047 } 048 return false; 049 } 050 static void run(BaseAppConfig config,String []args){ 051 config.parseCommandLine(args); 052 MessageQueueType implType =checkNotNull( (MessageQueueType)config.getConstant(IMPL_TYPE),"NOT DEFINED %s",IMPL_TYPE); 053 String devmac = config.valueOf("mac"); 054 boolean trace = config.isTrace(); 055 System.out.printf("Text terminal for Redis %s Talk is starting(设备(%s)交互字符终端启动)\n",implType,implType); 056 // 否则提示输入命令行参数 057 if(Strings.isNullOrEmpty(devmac)){ 058 devmac = inputMac(); 059 } 060 try { 061 SampleConsole client = 062 BaseConsole.makeConsole(SampleConsole.class, devmac, MessageQueueConfigManagers.getManager(implType)); 063 client.setStackTrace(trace).start(); 064 } catch (SmqNotFoundConnectionException e) { 065 if(trace){ 066 logger.error(e.getMessage(),e); 067 }else{ 068 System.out.println(e.getMessage()); 069 } 070 } 071 } 072}