/*
 * Copyright 2015 the original author or authors.
 *
 * 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 net.kuujo.copycat.examples.server;

import java.net.InetAddress;

import net.kuujo.copycat.Copycat;
import net.kuujo.copycat.CopycatServer;
import net.kuujo.copycat.cluster.NettyCluster;
import net.kuujo.copycat.cluster.NettyMember;
import net.kuujo.copycat.raft.log.Log;
import net.kuujo.copycat.raft.log.StorageLevel;

/**
 * Server example.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
public class ServerExample {

  /**
   * Starts the server.
   */
  public static void main(String[] args) throws Exception {
    if (args.length < 2)
      throw new IllegalArgumentException("must supply a serverId:port and at least one remoteId:host:port triple");

    String[] parts = args[0].split(":");
    int serverId = Integer.valueOf(parts[0]);
    int port = Integer.valueOf(parts[1]);

    NettyCluster.Builder builder = NettyCluster.builder()
      .withMemberId(serverId)
      .withHost(InetAddress.getLocalHost().getHostName())
      .withPort(port);

    for (int i = 1; i < args.length; i++) {
      parts = args[i].split(":");
      builder.addMember(NettyMember.builder()
        .withId(1)
        .withId(Integer.valueOf(parts[0]))
        .withHost(InetAddress.getByName(parts[1]).getHostName())
        .withPort(Integer.valueOf(parts[2]))
        .build());
    }

    Copycat copycat = CopycatServer.builder()
      .withCluster(builder.build())
      .withLog(Log.builder().withStorageLevel(StorageLevel.MEMORY).build())
      .build();

    copycat.open().join();

    while (copycat.isOpen()) {
      Thread.sleep(1000);
    }
  }

}

/*
 * Copyright 2015 the original author or authors.
 *
 * 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 net.kuujo.copycat.examples.server;

import net.kuujo.copycat.Copycat;
import net.kuujo.copycat.CopycatServer;
import net.kuujo.copycat.cluster.NettyCluster;
import net.kuujo.copycat.cluster.NettyMember;
import net.kuujo.copycat.raft.log.Log;
import net.kuujo.copycat.raft.log.StorageLevel;

import java.net.InetAddress;

/**
 * Server example.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
public class ServerExample {

  /**
   * Starts the server.
   */
  public static void main(String[] args) throws Exception {
    if (args.length < 2)
      throw new IllegalArgumentException("must supply a serverId:port and at least one remoteId:host:port triple");

    String[] parts = args[0].split(":");
    int serverId = Integer.valueOf(parts[0]);
    int port = Integer.valueOf(parts[1]);

    NettyCluster.Builder builder = NettyCluster.builder()
      .withMemberId(serverId)
      .addMember(NettyMember.builder()
        .withId(serverId)
        .withHost(InetAddress.getLocalHost().getHostName())
        .withPort(port)
        .build());

    for (int i = 1; i < args.length; i++) {
      parts = args[i].split(":");
      builder.addMember(NettyMember.builder()
        .withId(Integer.valueOf(parts[0]))
        .withHost(InetAddress.getByName(parts[1]).getHostName())
        .withPort(Integer.valueOf(parts[2]))
        .build());
    }

    Copycat copycat = CopycatServer.builder()
      .withCluster(builder.build())
      .withLog(Log.builder().withStorageLevel(StorageLevel.MEMORY).build())
      .build();

    copycat.open().join();

    while (copycat.isOpen()) {
      Thread.sleep(1000);
    }
  }

}

Fix server example.