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 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);
    		ListenerDispatcher.INSTANCE.subscribe(getOfflineChannel(), 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) {
    }
}

@@ -35,16 +35,74 @@
 outer;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import c
@@ -145,24 +145,24 @@
 tContainer;%0A
-
 import com.s
@@ -562,16 +562,106 @@
 tener %7B%0A
+%09%0A%09private static final Logger log = LoggerFactory.getLogger(UserChangeListener.class);%0A%09%0A
     publ
@@ -1018,24 +1018,24 @@
 l(), this);%0A
-
     %09%09Listen
@@ -1090,24 +1090,183 @@
 l(), this);%0A
+    %09%7Delse%7B%0A    %09%09log.error(%22UserChangeListener is not localhost,required:%7B%7D,but:%7B%7D%22,ConfigCenter.holder.onlineAndOfflineListenerIp(),MPushUtil.getLocalIp());%0A
     %09%7D%0A    %7D

add log