use of io.transport.core.exception.TransportException in project transporter by wang4ever.
the class AbstractChannelMessageHandler method processAccpetConnect.
/**
* 新建/注册客户端连接处理
*
* @param ctx
* @param msg
*/
protected void processAccpetConnect(ChannelHandlerContext ctx, ConnectMessage msg) {
// 1.0 Check device logged in.
if (this.client == null)
throw new TransportException("No new socket channel(client is null).");
else {
String cliDeviceId = this.client.getDeviceInfo().getDeviceId();
String reqDeviceId = msg.getDeviceInfo().getDeviceId();
// 同一deviceId单点登录控制:当前channel已登录或当前deviceId已有已登录的channel(重复创建了channel)
if (!StringUtils.isEmpty(cliDeviceId) || this.repository.contains(reqDeviceId))
throw new TransportConnectLimitException("Already loggedin. deviceId(client)=" + cliDeviceId + ", deviceId(req)=" + reqDeviceId);
}
//
// 1.1 Check parameter.
msg.validation();
//
// 1.2 Check of appId connections limit.
this.accpetValidation(msg.getAppId());
//
// 2.1 Authentication appId/secret.
this.login(msg);
this.client.setDeviceInfo(msg.getDeviceInfo());
this.repository.putClient(msg.getAppId(), this.client);
if (logger.isInfoEnabled())
logger.info("新建客户端. {}, {}", ctx.channel(), this.client.getDeviceInfo().getDeviceId());
//
// 3.1 Echo result.
ConnectRespMessage resp = new ConnectRespMessage();
// 3.2 Get active cluster node info.
Map<String, NodeInfo> nodes = this.clusterService.clusterNodes(true, null);
if (nodes != null) {
for (NodeInfo node : nodes.values()) {
try {
boolean isWsCh = TransportProcessors.isWSChannel(ctx.channel());
if (isWsCh)
resp.getHostAndPorts().add(node.getHost() + ":" + node.getWsPort());
else
resp.getHostAndPorts().add(node.getHost() + ":" + node.getRpcPort());
} catch (Exception e) {
logger.error("返回节点信息错误.", e);
}
}
}
if (logger.isInfoEnabled())
logger.info("节点列表. {}", resp.getHostAndPorts());
this.echoWrite(ctx, resp);
}
use of io.transport.core.exception.TransportException in project transporter by wang4ever.
the class ConnectMessage method validation.
public void validation() {
boolean ret = (StringUtils.isEmpty(this.getAppId()));
if (ret)
throw new TransportException("'appId'不允许为空.");
this.getDeviceInfo().validation();
}
use of io.transport.core.exception.TransportException in project transporter by wang4ever.
the class ByteBufs method coverFixedBuf.
/**
* 扩充定长字节数组缓冲区
*
* @param orgin
* 参数
* @param totalLen
* 总长度
* @return
* @sine
*/
public static byte[] coverFixedBuf(byte[] orgin, int totalLen) {
if (totalLen < orgin.length)
throw new TransportException("The buffer array 'orgin' is over long and can only be 'totalLen'.");
byte[] buf = new byte[totalLen];
System.arraycopy(orgin, 0, buf, 0, orgin.length);
return buf;
}
use of io.transport.core.exception.TransportException in project transporter by wang4ever.
the class PushTopicHandler method execute.
@Override
public void execute(String topic, byte[] payload) {
if (logger.isDebugEnabled())
logger.debug("Push handler on message: topic={}, payload={}", topic, ByteBufs.toString(payload));
try {
// 当使用MQTT broker转发时,topic包含clientId的信息(topic -> toDeviceId).
String toDeviceId = TopicType.getSubTopicAfter(topic);
TransportMessage message = TransportProcessors.build(payload);
// toDeviceId -> actorName
String actorName = this.config.getCtlDeviceIdActorPkey() + toDeviceId;
this.actorManager.actorTell(actorName, message);
if (logger.isDebugEnabled())
logger.debug("MQTT.device消息传送(Actor). actorName={}", actorName);
} catch (Exception e) {
if (e instanceof TransportException)
logger.error("MQTT.device消费传送(Actor)失败. {}", ExceptionUtils.getRootCauseMessage(e));
else
logger.error("MQTT.device消费传送(Actor)异常. ", e);
}
}
use of io.transport.core.exception.TransportException in project transporter by wang4ever.
the class TransportMessageHandler method processDeviceRegisted.
/**
* 注册前端设备连接认证信息处理
*
* @param ctx
* @param msg
*/
private void processDeviceRegisted(ChannelHandlerContext ctx, DeviceRegistMessage msg) {
// 1.1 Check deviceId.
for (Object deviceId : msg.getClientDeviceIds()) {
if (ByteBufs.toBytes(String.valueOf(deviceId)).length != DeviceInfo.ID_LEN)
throw new TransportException("Protocol error, parameter 'deviceId' length can only be " + DeviceInfo.ID_LEN);
}
// 1.2 保存注册前端设备连接认证信息Redis
for (Object deviceId : msg.getClientDeviceIds()) this.jedisService.set(String.valueOf(deviceId), msg.getAppId(), msg.getExpired());
if (logger.isInfoEnabled())
logger.info("新注册认证设备: msg={}", JSON.toJSONString(msg));
// 1.3 Echo客户端
DeviceRegistRespMessage resp = new DeviceRegistRespMessage();
this.echoWrite(ctx, resp);
if (logger.isDebugEnabled())
logger.debug("Echo message. {}", resp);
}
Aggregations