use of io.transport.core.protocol.message.internal.TransportMessage in project transporter by wang4ever.
the class TransportMessageHandler method dispatch.
/**
* 转发给具体业务方法
*
* 【方法描述】
*
* @param msg
* @see: 【参考链接】
*/
@Override
protected void dispatch(ChannelHandlerContext ctx, Object msg) {
// 1.1 请求连接
if (msg instanceof ConnectMessage) {
if (logger.isDebugEnabled())
logger.debug("On connect. msg={}", msg);
super.processAccpetConnect(ctx, (ConnectMessage) msg);
return;
}
// 1.2 连接认证检查
this.authentication();
// 1.3 连接关闭
if (msg instanceof ClosingMessage) {
if (logger.isDebugEnabled())
logger.debug("On close. msg={}", msg);
// Close处理
super.closeClient();
return;
}
// 1.4 链路检测
if (msg instanceof ActiveMessage) {
if (logger.isDebugEnabled())
logger.debug("On Active. msg={}", msg);
// Reply client link detection.
this.echoWrite(ctx, new ActiveRespMessage());
return;
} else // 1.5 消息传送
if (msg instanceof TransportMessage) {
if (logger.isDebugEnabled())
logger.debug("On transport. msg={}", msg);
// 数据传输处理
this.processTransport(ctx, (TransportMessage) msg);
return;
} else // 1.6 注册前端设备
if (msg instanceof DeviceRegistMessage) {
if (logger.isDebugEnabled())
logger.debug("On frontend device register. msg={}", msg);
// 注册前端设备连接认证信息处理
this.processDeviceRegisted(ctx, (DeviceRegistMessage) msg);
return;
}
// 2.1 非法连接client close.
this.closeClient();
logger.warn("未知类型的消息.{}", JSON.toJSONString(msg));
return;
}
use of io.transport.core.protocol.message.internal.TransportMessage 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.protocol.message.internal.TransportMessage in project transporter by wang4ever.
the class AccpetActor method dispatch.
/**
* 转发路由消息
*
* @param msg
*/
private void dispatch(Object msg) {
if (msg instanceof MemberUp) {
MemberUp mUp = (MemberUp) msg;
logger.warn("Member is Up: {}", mUp.member());
// Join cluster node.
this.clusterService.joining(this.getHostAndPort(mUp.member()));
} else if (msg instanceof UnreachableMember) {
UnreachableMember mUnreachable = (UnreachableMember) msg;
logger.warn("Member detected as unreachable: {}", mUnreachable.member());
// Leaving cluster node.
this.clusterService.leaving(this.getHostAndPort(mUnreachable.member()));
} else if (msg instanceof MemberRemoved) {
MemberRemoved mRemoved = (MemberRemoved) msg;
logger.warn("Member is Removed: {}", mRemoved.member());
// Remove cluster node.
this.clusterService.leaving(this.getHostAndPort(mRemoved.member()));
} else if (msg instanceof MemberEvent) {
MemberRemoved mEvent = (MemberRemoved) msg;
logger.warn("Member is event: {}", mEvent.member());
} else if (msg instanceof TransportMessage) {
TransportMessage tmsg = (TransportMessage) msg;
// Routing by point to point or point pairs.
if (!StringUtils.isEmpty(tmsg.getToGroupId())) {
TransportProcessors.sentGroupMsg(channelRepository, tmsg);
if (logger.isInfoEnabled())
logger.info("Actor Forward to toGroupId={}.", tmsg.getToGroupId());
} else if (!StringUtils.isEmpty(tmsg.getToDeviceId())) {
TransportProcessors.sentMsg(channelRepository, tmsg);
if (logger.isInfoEnabled())
logger.info("Actor Forward to toDeviceId={}.", tmsg.getToDeviceId());
}
} else {
this.unhandled(msg);
}
}
use of io.transport.core.protocol.message.internal.TransportMessage in project transporter by wang4ever.
the class TransportProcessors method build.
/**
* 构建消息
*
* @param c
* @param msg
*/
public static TransportMessage build(byte[] buf) {
TransportMessage msg = JSON.parseObject(ByteBufs.toString(buf), TransportMessage.class);
// 校正输出action类型
msg.getHead().setActionId(MsgType.TRANSPORT_OUT.getActionId());
return msg;
}
use of io.transport.core.protocol.message.internal.TransportMessage in project transporter by wang4ever.
the class PushGroupTopicHandler method execute.
@Override
public void execute(String topic, byte[] payload) {
if (logger.isDebugEnabled())
logger.debug("Push.group handler on message: topic={}, payload={}", topic, ByteBufs.toString(payload));
try {
// 当使用MQTT broker转发时,topic包含clientId的信息(topic -> toGroupId).
String toGroupId = TopicType.getSubTopicAfter(topic);
TransportMessage message = TransportProcessors.build(payload);
// 发送给toGroupId下所有deviceId
Collection<Client> clients = this.channelRepository.getClients(toGroupId);
if (clients != null && !clients.isEmpty()) {
for (Client c : clients) {
// toDeviceId -> actorName
String actorName = this.config.getCtlDeviceIdActorPkey() + c.getDeviceInfo().getDeviceId();
this.actorManager.actorTell(actorName, message);
if (logger.isDebugEnabled())
logger.debug("MQTT.group消息传送(Actor). actorName={}", actorName);
}
} else
throw new TransportException("toGroupId=" + toGroupId + " clients is null.");
} catch (Exception e) {
if (e instanceof TransportException)
logger.error("MQTT.group消费传送(Actor)失败. {}", ExceptionUtils.getRootCauseMessage(e));
else
logger.error("MQTT.group消费传送(Actor)异常. ", e);
}
}
Aggregations