添加日志打印

master
liuqingkun 3 years ago
parent b3dfeeabf8
commit d6ceae9cbc
  1. 23
      src/main/java/cn/org/hentai/jtt1078/app/websocket/WebsocketClientHandler.java
  2. 23
      src/main/java/cn/org/hentai/jtt1078/server/Jtt1078Handler.java
  3. 25
      src/main/java/cn/org/hentai/jtt1078/server/SessionManager.java
  4. 70
      src/main/java/cn/org/hentai/jtt1078/test/AudioTest.java

@ -12,6 +12,8 @@ import io.netty.util.CharsetUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
@ -80,15 +82,32 @@ public class WebsocketClientHandler extends SimpleChannelInboundHandler<Object>
sendMsgTimer.schedule(new TimerTask() { sendMsgTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
String clientId = PublishManager.getInstance().getCurPushDevice(); String tag = PublishManager.getInstance().getCurPushDevice();
try { try {
WebsocketClient.getWsInstance().write(clientId); WebsocketClient.getWsInstance().write(tag);
// 每分钟的0s, 打印当前推流通道
LocalTime localTime = LocalTime.now();
if (localTime.getSecond() == 0) {
log.info(BusinessConstant.LOGGER_PREFIX + " : jt808当前推流通道 : tag = {}", tag);
}
} catch (MyException e) { } catch (MyException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}, 10, 1000); }, 10, 1000);
// printTimer = new Timer();
// log.info(BusinessConstant.LOGGER_PREFIX + " : jt808服务websocket连接成功, 启动打印当前通道定时任务");
// sendMsgTimer.schedule(new TimerTask() {
// @Override
// public void run() {
// String tag = PublishManager.getInstance().getCurPushDevice();
//
// log.info(BusinessConstant.LOGGER_PREFIX + " : jt808当前推流通道 : tag = {}", tag);
// }
// }, 10, 60000);
} }
/** /**

@ -74,10 +74,14 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
} }
} }
/**
* 连接断开触发
* @param ctx
*/
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) { public void channelInactive(ChannelHandlerContext ctx) {
try { try {
logger.info(BusinessConstant.LOGGER_PREFIX + " : channelInactive : tag = {} ", ctx.channel() == null ? "null" : ctx.channel().id()); logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接断开触发 channelInactive : tag = {} ", ctx.channel() == null ? "null" : ctx.channel().id());
super.channelInactive(ctx); super.channelInactive(ctx);
release(ctx.channel()); release(ctx.channel());
} catch (Exception e) { } catch (Exception e) {
@ -85,9 +89,15 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
} }
} }
/**
* 连接异常触发
* @param ctx
* @param cause
* @throws Exception
*/
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.info(BusinessConstant.LOGGER_PREFIX + " : exceptionCaught : channel = {} ", ctx.channel() == null ? "null" : ctx.channel().id()); logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接异常触发 exceptionCaught : channel = {} ", ctx.channel() == null ? "null" : ctx.channel().id());
// super.exceptionCaught(ctx, cause); // super.exceptionCaught(ctx, cause);
cause.printStackTrace(); cause.printStackTrace();
@ -97,7 +107,7 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
@Override @Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
logger.info(BusinessConstant.LOGGER_PREFIX + " : userEventTriggered : channel = {} ", ctx.channel() == null ? "null" : ctx.channel().id()); logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接超时 userEventTriggered : channel = {} ", ctx.channel() == null ? "null" : ctx.channel().id());
if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) { if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
IdleStateEvent event = (IdleStateEvent) evt; IdleStateEvent event = (IdleStateEvent) evt;
@ -109,9 +119,16 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
} }
} }
/**
* 释放通道
*
* @param channel
*/
private void release(io.netty.channel.Channel channel) { private void release(io.netty.channel.Channel channel) {
logger.info(BusinessConstant.LOGGER_PREFIX + " : release : channel = {} ", channel.id()); logger.info(BusinessConstant.LOGGER_PREFIX + " : release : channel = {} ", channel.id());
SessionManager.printMapping();
String tag = SessionManager.get(channel, "tag"); String tag = SessionManager.get(channel, "tag");
logger.info(BusinessConstant.LOGGER_PREFIX + " : release : tag = {} ", tag);
if (tag != null) { if (tag != null) {
logger.info("close netty channel: {}", tag); logger.info("close netty channel: {}", tag);
PublishManager.getInstance().close(tag); PublishManager.getInstance().close(tag);

@ -1,31 +1,36 @@
package cn.org.hentai.jtt1078.server; package cn.org.hentai.jtt1078.server;
import cn.org.hentai.jtt1078.util.BusinessConstant;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import org.apache.commons.collections.map.HashedMap; import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map; import java.util.Map;
public final class SessionManager public final class SessionManager {
{ static Logger logger = LoggerFactory.getLogger(SessionManager.class);
private static final Map<String, Object> mappings = new HashedMap(); private static final Map<String, Object> mappings = new HashedMap();
public static void init() public static void init() {
{
// ... // ...
} }
public static <T> T get(Channel channel, String key) public static <T> T get(Channel channel, String key) {
{
return (T) mappings.get(channel.id().asLongText() + key); return (T) mappings.get(channel.id().asLongText() + key);
} }
public static void set(Channel channel, String key, Object value) public static void set(Channel channel, String key, Object value) {
{
mappings.put(channel.id().asLongText() + key, value); mappings.put(channel.id().asLongText() + key, value);
} }
public static boolean contains(Channel channel, String key) public static boolean contains(Channel channel, String key) {
{
return mappings.containsKey(channel.id().asLongText() + key); return mappings.containsKey(channel.id().asLongText() + key);
} }
public static void printMapping() {
logger.info(BusinessConstant.LOGGER_PREFIX + " : SessionManager : mapping keys = {} ", mappings.keySet().toString());
logger.info(BusinessConstant.LOGGER_PREFIX + " : SessionManager : mapping values = {} ", mappings.values().toString());
}
} }

@ -11,39 +11,41 @@ import java.io.FileOutputStream;
/** /**
* Created by matrixy on 2019/12/21. * Created by matrixy on 2019/12/21.
*/ */
public class AudioTest public class AudioTest {
{ public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception String s1 = new String("");
{ String s2 = new String("");
int len = -1; System.out.println(s1 == s2);
byte[] block = new byte[1024]; System.out.println(s1.equals(s2));
FileInputStream fis = new FileInputStream("e:\\test\\streaming.hex"); // int len = -1;
Jtt1078Decoder decoder = new Jtt1078Decoder(); // byte[] block = new byte[1024];
G711Codec codec = new G711Codec(); // FileInputStream fis = new FileInputStream("e:\\test\\streaming.hex");
FileOutputStream fos = new FileOutputStream("e:\\test\\fuckfuckfuck1111.pcm"); // Jtt1078Decoder decoder = new Jtt1078Decoder();
while ((len = fis.read(block)) > -1) // G711Codec codec = new G711Codec();
{ // FileOutputStream fos = new FileOutputStream("e:\\test\\fuckfuckfuck1111.pcm");
decoder.write(block, 0, len); // while ((len = fis.read(block)) > -1)
while (true) // {
{ // decoder.write(block, 0, len);
Packet p = decoder.decode(); // while (true)
if (p == null) break; // {
// Packet p = decoder.decode();
int lengthOffset = 28; // if (p == null) break;
int dataType = (p.seek(15).nextByte() >> 4) & 0x0f; //
// 透传数据类型:0100,没有后面的时间以及Last I Frame Interval和Last Frame Interval字段 // int lengthOffset = 28;
if (dataType == 0x04) lengthOffset = 28 - 8 - 2 - 2; // int dataType = (p.seek(15).nextByte() >> 4) & 0x0f;
else if (dataType == 0x03) lengthOffset = 28 - 4; // // 透传数据类型:0100,没有后面的时间以及Last I Frame Interval和Last Frame Interval字段
// if (dataType == 0x04) lengthOffset = 28 - 8 - 2 - 2;
if (dataType == 0x03) // else if (dataType == 0x03) lengthOffset = 28 - 4;
{ //
byte[] pcmData = codec.toPCM(p.seek(lengthOffset + 2).nextBytes()); // if (dataType == 0x03)
fos.write(pcmData); // {
fos.flush(); // byte[] pcmData = codec.toPCM(p.seek(lengthOffset + 2).nextBytes());
} // fos.write(pcmData);
} // fos.flush();
} // }
fos.close(); // }
fis.close(); // }
// fos.close();
// fis.close();
} }
} }

Loading…
Cancel
Save