package com.shinemo.mpush.common.router;

import com.shinemo.mpush.common.AbstractEventContainer;
import com.shinemo.mpush.tools.MPushUtil;
import com.shinemo.mpush.tools.config.ConfigCenter;
import com.shinemo.mpush.tools.redis.listener.ListenerDispatcher;
import com.shinemo.mpush.tools.redis.listener.MessageListener;
import com.shinemo.mpush.tools.redis.manage.RedisManage;

/**
 * Created by ohun on 2016/1/4.
 */
public final class UserChangeListener extends AbstractEventContainer implements MessageListener {
    public static final String ONLINE_CHANNEL = "/mpush/online/";
    
    public static final String OFFLINE_CHANNEL = "/mpush/offline/";

    //只需要一台机器注册online、offline 消息通道
    public UserChangeListener() {
    	if(ConfigCenter.holder.onlineAndOfflineListenerIp().equals(MPushUtil.getLocalIp())){
    		ListenerDispatcher.INSTANCE.subscribe(getOnlineChannel(), this);
    	}
    }

    public String getOnlineChannel() {
        return ONLINE_CHANNEL;
    }
    
    public String getOfflineChannel() {
        return OFFLINE_CHANNEL;
    }
    
    public void userOnline(String userId) {
        RedisManage.publish(getOnlineChannel(), userId);
    }
    
    public void userOffline(String userId){
    	RedisManage.publish(getOnlineChannel(), userId);
    }

    @Override
    public void onMessage(String channel, String message) {
//        if (getKickChannel().equals(channel)) {
//            KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class);
//            if (msg != null) {
//                onReceiveKickRemoteMsg(msg);
//            } else {
//            	LoggerManage.info(LogType.CONNECTION, "receive an error kick message=%s", message);
//            }
//        } else {
//        	LoggerManage.info(LogType.CONNECTION, "receive an error redis channel=%s",channel);
//        }
    }
}

@@ -419,22 +419,16 @@
 %0Apublic 
-final 
 class Us
@@ -504,16 +504,16 @@
 tener %7B%0A
+
     publ
@@ -874,16 +874,88 @@
  this);%0A
+    %09%09ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), this);%0A
     %09%7D%0A 
@@ -1355,16 +1355,16 @@
 verride%0A
+
     publ
@@ -1419,482 +1419,8 @@
 ) %7B%0A
-//        if (getKickChannel().equals(channel)) %7B%0A//            KickRemoteMsg msg = Jsons.fromJson(message, KickRemoteMsg.class);%0A//            if (msg != null) %7B%0A//                onReceiveKickRemoteMsg(msg);%0A//            %7D else %7B%0A//            %09LoggerManage.info(LogType.CONNECTION, %22receive an error kick message=%25s%22, message);%0A//            %7D%0A//        %7D else %7B%0A//        %09LoggerManage.info(LogType.CONNECTION, %22receive an error redis channel=%25s%22,channel);%0A//        %7D%0A
     

remove final