/*
 * Copyright (C) 2010-2101 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.otter.shared.arbitrate.impl.manage;

import java.util.ArrayList;
import java.util.List;

import org.I0Itec.zkclient.exception.ZkException;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.apache.zookeeper.CreateMode;

import com.alibaba.otter.shared.arbitrate.exception.ArbitrateException;
import com.alibaba.otter.shared.arbitrate.impl.ArbitrateConstants;
import com.alibaba.otter.shared.arbitrate.impl.ArbitrateEvent;
import com.alibaba.otter.shared.arbitrate.impl.manage.helper.ManagePathUtils;
import com.alibaba.otter.shared.arbitrate.impl.zookeeper.ZooKeeperClient;
import com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx;

/**
 * 机器node节点的相关信号
 * 
 * @author jianghang 2011-8-31 下午07:26:02
 */
public class NodeArbitrateEvent implements ArbitrateEvent {

    private ZkClientx zookeeper = ZooKeeperClient.getInstance();

    /**
     * 创建相应的node节点，说明：node节点的生命周期为EPHEMERAL
     * 
     * <pre>
     * 1. 是个同步调用
     * </pre>
     */
    public void init(Long nid) {
        String path = ManagePathUtils.getNode(nid);

        try {
            zookeeper.create(path, new byte[0], CreateMode.EPHEMERAL);// 创建为临时节点
        } catch (ZkException e) {
            throw new ArbitrateException("Node_init", nid.toString(), e);
        }
    }

    /**
     * 销毁的node节点
     * 
     * <pre>
     * 1. 是个同步调用
     * </pre>
     */
    public void destory(Long nid) {
        String path = ManagePathUtils.getNode(nid);

        try {
            zookeeper.delete(path); // 删除节点，不关心版本
        } catch (ZkNoNodeException e) {
            // 如果节点已经不存在，则不抛异常
            // ignore
        } catch (ZkException e) {
            throw new ArbitrateException("Node_destory", nid.toString(), e);
        }
    }

    /**
     * 获取当前存活的节点列表
     */
    public List<Long> liveNodes() {
        String path = ArbitrateConstants.NODE_NID_ROOT;
        try {
            List<String> nids = zookeeper.getChildren(path);
            List<Long> result = new ArrayList<Long>();
            for (String nid : nids) {
                result.add(Long.valueOf(nid));
            }

            return result;
        } catch (ZkException e) {
            throw new ArbitrateException("liveNodes", e);
        }
    }

}

@@ -1303,20 +1303,22 @@
 **%0A * %E6%9C%BA%E5%99%A8
+ 
 node
+ 
 %E8%8A%82%E7%82%B9%E7%9A%84%E7%9B%B8%E5%85%B3%E4%BF%A1%E5%8F%B7%0A
@@ -1354,16 +1354,17 @@
 -8-31 %E4%B8%8B%E5%8D%88
+ 
 07:26:02
@@ -1515,20 +1515,22 @@
  * %E5%88%9B%E5%BB%BA%E7%9B%B8%E5%BA%94%E7%9A%84
+ 
 node
+ 
 %E8%8A%82%E7%82%B9%EF%BC%8C%E8%AF%B4%E6%98%8E%EF%BC%9Ano
@@ -1535,16 +1535,17 @@
 node
+ 
 %E8%8A%82%E7%82%B9%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E4%B8%BA
 EPHE
@@ -1540,16 +1540,17 @@
 %E8%8A%82%E7%82%B9%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E4%B8%BA
+ 
 EPHEMERA
@@ -1703,32 +1703,74 @@
 %0A%0A        try %7B%0A
+          if (!zookeeper.exists(path))%7B%0A  
             zook
@@ -1834,16 +1834,30 @@
 %E5%88%9B%E5%BB%BA%E4%B8%BA%E4%B8%B4%E6%97%B6%E8%8A%82%E7%82%B9%0A
+            %7D%0A
         
@@ -1991,20 +1991,22 @@
    * %E9%94%80%E6%AF%81%E7%9A%84
+ 
 node
+ 
 %E8%8A%82%E7%82%B9%0A     

check already join into zk before create new one (#972)