Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
375 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
mpush-api/src/main/java/com/shinemo/mpush/api/event/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
package com.shinemo.mpush.api.event; | |||
|
|||
/** | |||
* Created by ohun on 2015/12/29. | |||
*/ | |||
public interface Event { | |||
} |
16 changes: 16 additions & 0 deletions
16
mpush-api/src/main/java/com/shinemo/mpush/api/event/HandshakeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
package com.shinemo.mpush.api.event; | |||
|
|||
import com.shinemo.mpush.api.Connection; | |||
|
|||
/** | |||
* Created by ohun on 2015/12/29. | |||
*/ | |||
public class HandshakeEvent implements Event { | |||
public final Connection connection; | |||
public final int heartbeat; | |||
|
|||
public HandshakeEvent(Connection connection, int heartbeat) { | |||
this.connection = connection; | |||
this.heartbeat = heartbeat; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
mpush-core/src/main/java/com/shinemo/mpush/core/EventBus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
|
|||
package com.shinemo.mpush.core; | |||
|
|||
import com.google.common.eventbus.AsyncEventBus; | |||
import com.google.common.eventbus.SubscriberExceptionContext; | |||
import com.google.common.eventbus.SubscriberExceptionHandler; | |||
import com.shinemo.mpush.api.event.Event; | |||
import com.shinemo.mpush.tools.thread.ThreadPoolUtil; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
import java.util.concurrent.Executor; | |||
|
|||
/** | |||
* Created by ohun on 2015/12/29. | |||
*/ | |||
public class EventBus { | |||
private static final Logger LOGGER = LoggerFactory.getLogger(EventBus.class); | |||
public static final EventBus INSTANCE = new EventBus(); | |||
private final com.google.common.eventbus.EventBus eventBus; | |||
|
|||
public EventBus() { | |||
Executor executor = ThreadPoolUtil.getThreadPoolManager().getThreadExecutor("event-bus-pool", 4, 4); | |||
eventBus = new AsyncEventBus(executor, new SubscriberExceptionHandler() { | |||
@Override | |||
public void handleException(Throwable exception, SubscriberExceptionContext context) { | |||
LOGGER.error("event bus subscriber ex", exception); | |||
} | |||
}); | |||
} | |||
|
|||
public void post(Event event) { | |||
eventBus.post(event); | |||
} | |||
|
|||
|
|||
public void register(Object bean) { | |||
eventBus.register(bean); | |||
} | |||
|
|||
public void unregister(Object bean) { | |||
eventBus.unregister(bean); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerChannelHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
package com.shinemo.mpush.core.handler; | |||
|
|||
|
|||
import com.shinemo.mpush.api.Constants; | |||
import com.shinemo.mpush.api.protocol.Packet; | |||
import com.shinemo.mpush.api.Connection; | |||
import com.shinemo.mpush.api.Receiver; | |||
import com.shinemo.mpush.core.ConnectionManager; | |||
import com.shinemo.mpush.core.NettyConnection; | |||
|
|||
import io.netty.buffer.Unpooled; | |||
import io.netty.channel.ChannelHandler; | |||
import io.netty.channel.ChannelHandlerAdapter; | |||
import io.netty.channel.ChannelHandlerContext; | |||
|
|||
import io.netty.handler.timeout.IdleStateEvent; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
/** | |||
* Created by ohun on 2015/12/19. | |||
*/ | |||
@ChannelHandler.Sharable | |||
public class ServerChannelHandler extends ChannelHandlerAdapter { | |||
|
|||
private static final Logger LOGGER = LoggerFactory.getLogger(ServerChannelHandler.class); | |||
|
|||
private final Receiver receiver; | |||
|
|||
public ServerChannelHandler(Receiver receiver) { | |||
this.receiver = receiver; | |||
} | |||
|
|||
@Override | |||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | |||
Connection connection = ConnectionManager.INSTANCE.get(ctx.channel()); | |||
receiver.onReceive((Packet) msg, connection); | |||
} | |||
|
|||
@Override | |||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | |||
ConnectionManager.INSTANCE.remove(ctx.channel()); | |||
LOGGER.error(ctx.channel().remoteAddress() + ", exceptionCaught", cause); | |||
} | |||
|
|||
@Override | |||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | |||
LOGGER.warn(ctx.channel().remoteAddress() + ", channelActive"); | |||
Connection connection = new NettyConnection(); | |||
connection.init(ctx.channel()); | |||
ConnectionManager.INSTANCE.add(connection); | |||
} | |||
|
|||
@Override | |||
public void channelInactive(ChannelHandlerContext ctx) throws Exception { | |||
LOGGER.warn(ctx.channel().remoteAddress() + ", channelInactive"); | |||
ConnectionManager.INSTANCE.remove(ctx.channel()); | |||
} | |||
|
|||
@Override | |||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { | |||
if (evt instanceof IdleStateEvent) { | |||
IdleStateEvent stateEvent = (IdleStateEvent) evt; | |||
switch (stateEvent.state()) { | |||
case READER_IDLE: | |||
ConnectionManager.INSTANCE.remove(ctx.channel()); | |||
ctx.close(); | |||
LOGGER.warn("heartbeat read timeout, chanel closed!"); | |||
break; | |||
case WRITER_IDLE: | |||
ctx.writeAndFlush(Packet.getHBPacket()); | |||
LOGGER.warn("heartbeat write timeout, do write an EOL."); | |||
break; | |||
case ALL_IDLE: | |||
} | |||
} else { | |||
LOGGER.warn("One user event Triggered. evt=" + evt); | |||
super.userEventTriggered(ctx, evt); | |||
} | |||
} | |||
} |
55 changes: 0 additions & 55 deletions
55
mpush-core/src/main/java/com/shinemo/mpush/core/handler/ServerHandler.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.