001: package org.space4j.demos.phonebook;
002:
003: import org.space4j.implementation.*;
004: import org.space4j.commands.*;
005: import org.space4j.*;
006:
007: import java.util.*;
008: import java.io.*;
009: import java.net.*;
010:
011: /**
012: * This is to show you how simple it is to make a Space4J cluster!
013: * To make a cluster, open up two shells (or DOS) and execute in the first:<br>
014: * java org.space4j.demos.phonebook.PhoneBookCluster master<br><br>
015: * and in the second:<br>
016: * java org.space4j.demos.PhoneBookCluster slave 127.0.0.1<br><br>
017: * You can also use two different machines. Just pass the master IP address to the slave instead of 127.0.0.1.<br>
018: * IMPORTANT: You must have the main Space4J dir (space4j_db) mounted, so both machines can have access to it.
019: */
020: public class PhoneBookCluster extends PhoneBook {
021:
022: public PhoneBookCluster(boolean master, String master_ip)
023: throws LoggerException, CommandException,
024: UnknownHostException, IOException, ClassNotFoundException {
025:
026: // Initializes the system, passing the dir where the logs and snapshots are stored.
027: // NOTE: The system will load everything in memory here. Snapshot and last commands.
028: if (master) {
029: space4j = new MasterSpace4J("PhoneBook");
030: } else {
031: space4j = new SlaveSpace4J("PhoneBook", master_ip);
032: }
033:
034: // Start the system...
035: space4j.start();
036:
037: // Grab the space where all data resides...
038: space = space4j.getSpace();
039:
040: // If this is the first time, create our main hashmap...
041: // Note: To avoid any race-condition, only the master will try to do this...
042: if (master && space.getObject("phonenumbers") == null) {
043: space4j.executeCommand(new MapCreateCmd("phonenumbers",
044: new HashMap()));
045: }
046: }
047:
048: // This has nothing to do with Space4J.
049: // Just a simple logic for a minimal PhoneBook application.
050: // ToDo: Make it a Swing application. (Anyone?)
051: public static void main(String[] args) throws Exception {
052: boolean master = false;
053: String master_ip = null;
054:
055: if (args[0].equals("master"))
056: master = true;
057: else if (args[0].equals("slave")) {
058: master = false;
059: if (args.length > 1)
060: master_ip = args[1];
061: else
062: master_ip = "127.0.0.1";
063: }
064:
065: PhoneBookCluster book = new PhoneBookCluster(master, master_ip);
066: BufferedReader input = new BufferedReader(
067: new InputStreamReader(System.in));
068: System.out
069: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
070: while (true) {
071: String cmd = input.readLine();
072: System.out.println();
073: if (cmd.equalsIgnoreCase("l")) {
074: ArrayList list = book.getNames();
075: if (list == null || list.size() == 0)
076: System.out.println("No entries in phone book!\n");
077: else {
078: Iterator iter = list.iterator();
079: while (iter.hasNext()) {
080: String key = (String) iter.next();
081: String value = book.getNumber(key);
082: System.out.println(key + ": " + value);
083: }
084: System.out
085: .println("\nTotal: " + list.size() + "\n");
086: }
087: } else if (cmd.equalsIgnoreCase("a")) {
088: System.out.print("Name: ");
089: String name = input.readLine();
090: System.out.print("Tel: ");
091: String tel = input.readLine();
092: if (!name.trim().equals("") && !tel.trim().equals("")) {
093: book.addNumber(name, tel);
094: System.out.println(name + ": " + tel + " added!\n");
095: }
096: } else if (cmd.equalsIgnoreCase("f")) {
097: System.out.print("Name: ");
098: String name = input.readLine();
099: String tel = book.getNumber(name);
100: if (tel != null) {
101: System.out.println("Number: " + tel + "\n");
102: } else {
103: System.out.println("Nothing found!\n");
104: }
105: } else if (cmd.equalsIgnoreCase("r")) {
106: System.out.print("Name: ");
107: String name = input.readLine();
108: if (book.delNumber(name)) {
109: System.out.println("Item removed!\n");
110: } else {
111: System.out.println("Not found!\n");
112: }
113: } else if (cmd.equalsIgnoreCase("s")) {
114: book.executeSnapshot();
115: System.out.println("Snapshot taken!\n");
116: } else if (cmd.equalsIgnoreCase("q")) {
117: break;
118: }
119: System.out
120: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
121: }
122: }
123: }
|