提交 01e6519d 编写于 作者: B bessani@gmail.com

No commit message

No commit message
上级 31ceb8fe
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package navigators.smart.tom.demo.keyvalue;
import java.util.HashMap;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import navigators.smart.tom.ServiceProxy;
import java.io.Console;
import java.io.Serializable;
/**
*
* @author sweta
*/
public class BFTMAPUtil implements Serializable {
static final int TAB_CREATE = 1;
static final int TAB_REMOVE = 2;
static final int SIZE_TABLE=3;
static final int TAB_CREATE_CHECK = 10;
static final int PUT = 4;
static final int GET = 5;
static final int SIZE = 6;
static final int REMOVE = 7;
static final int CHECK = 8;
static final int GET_TABLE = 9;
//private HashMap<String,byte[]> info = null;
private HashMap<String,HashMap<String,byte[]>> info1 = null;
//private HashMap<String,byte[]> info = null;
public BFTMAPUtil() {
info1=new HashMap<String,HashMap<String,byte[]>>();
}
public HashMap<String,byte[]> addTable(String key,HashMap data) {
return info1.put(key, data);
}
public byte[] addData(String tableName,String key,byte[] value) {
System.out.println("Table name"+tableName);
System.out.println("Table id"+key);
System.out.println("Value"+ new String(value));
HashMap<String,byte[]> info= info1.get(tableName);
if (info == null) System.out.println("This is null");
System.out.println("Table"+info);
byte[] ret = info.put(key,value);
// byte[] ret = info.get(key);
// System.out.println("Value"+ new String(ret));
return ret;
}
public HashMap<String,byte[]> getName(String id) {
return info1.get(id);
}
public byte[] getEntry(String tableName, String id) {
System.out.println("Table name"+tableName);
System.out.println("Table id"+id);
HashMap<String,byte[]> info= info1.get(tableName);
System.out.println("Table"+info);
return info.get(id);
}
public int getSizeofTable() {
return info1.size();
}
public int getSize(String tableName) {
HashMap<String,byte[]> info= info1.get(tableName);
return info.size();
}
public HashMap<String,byte[]> removeTable(String tableName) {
return info1.remove(tableName);
}
public byte[] removeEntry(String tableName,String key) {
HashMap<String,byte[]> info= info1.get(tableName);
return info.remove(key);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package navigators.smart.tom.demo.keyvalue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import navigators.smart.tom.ServiceProxy;
import java.io.Console;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author sweta
*/
public class BFTMap implements Map<String,HashMap<String,byte[]>> {
BFTMAPUtil bft = null;
ServiceProxy KVProxy = null;
BFTMap(int id) {
bft = new BFTMAPUtil();
KVProxy = new ServiceProxy(id);
}
ByteArrayOutputStream out = null;
public HashMap<String,byte[]> get(String tableName) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.GET);
new DataOutputStream(out).writeUTF(tableName);
byte[] rep = KVProxy.invoke(out.toByteArray(),true);
ByteArrayInputStream bis = new ByteArrayInputStream(rep) ;
ObjectInputStream in = new ObjectInputStream(bis) ;
HashMap<String,byte[]> table = (HashMap<String,byte[]>) in.readObject();
in.close();
return table;
} catch (ClassNotFoundException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public byte[] getEntry(String tableName,String key) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.GET);
new DataOutputStream(out).writeUTF(tableName);
new DataOutputStream(out).writeUTF(key);
byte[] rep = KVProxy.invoke(out.toByteArray(), true);
return rep;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public HashMap<String,byte[]> put(String key, HashMap<String,byte[]> value) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.TAB_CREATE);
new DataOutputStream(out).writeUTF(key);
//ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ObjectOutputStream out1 = new ObjectOutputStream(out) ;
out1.writeObject(value);
out1.close();
byte[] rep = KVProxy.invoke(out.toByteArray(),true);
ByteArrayInputStream bis = new ByteArrayInputStream(rep) ;
ObjectInputStream in = new ObjectInputStream(bis) ;
HashMap<String,byte[]> table = (HashMap<String,byte[]>) in.readObject();
in.close();
return table;
} catch (ClassNotFoundException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public byte[] put1(String tableName, String key, byte[] value) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.PUT);
new DataOutputStream(out).writeUTF(tableName);
new DataOutputStream(out).writeUTF(key);
new DataOutputStream(out).writeUTF(new String(value));
byte[] rep = KVProxy.invoke(out.toByteArray(),true);
return rep;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public HashMap<String,byte[]> remove(Object key) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.TAB_REMOVE);
new DataOutputStream(out).writeUTF((String) key);
byte[] rep = KVProxy.invoke(out.toByteArray(),false);
ByteArrayInputStream bis = new ByteArrayInputStream(rep) ;
ObjectInputStream in = new ObjectInputStream(bis) ;
HashMap<String,byte[]> table = (HashMap<String,byte[]>) in.readObject();
in.close();
return table;
} catch (ClassNotFoundException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public byte[] removeEntry(String tableName,String key) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.REMOVE);
new DataOutputStream(out).writeUTF((String) tableName);
new DataOutputStream(out).writeUTF((String) key);
byte[] rep = KVProxy.invoke(out.toByteArray(), false);
return rep;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
public int size() {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.SIZE_TABLE);
byte[] rep = KVProxy.invoke(out.toByteArray(),false);
ByteArrayInputStream in = new ByteArrayInputStream(rep);
int size = new DataInputStream(in).readInt();
return size;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return -1;
}
}
public int size1(String tableName) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.SIZE);
new DataOutputStream(out).writeUTF(tableName);
byte[] rep = KVProxy.invoke(out.toByteArray(), false);
ByteArrayInputStream in = new ByteArrayInputStream(rep);
int size = new DataInputStream(in).readInt();
return size;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return 0;
}
}
public boolean containsKey(String key) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.TAB_CREATE_CHECK);
new DataOutputStream(out).writeUTF((String) key);
byte[] rep = KVProxy.invoke(out.toByteArray(),true);
ByteArrayInputStream in = new ByteArrayInputStream(rep);
boolean res = new DataInputStream(in).readBoolean();
return res;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean containsKey1(String tableName, String key) {
try {
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(BFTMAPUtil.CHECK);
new DataOutputStream(out).writeUTF((String) tableName);
new DataOutputStream(out).writeUTF((String) key);
byte[] rep = KVProxy.invoke(out.toByteArray(),true);
ByteArrayInputStream in = new ByteArrayInputStream(rep);
boolean res = new DataInputStream(in).readBoolean();
return res;
} catch (IOException ex) {
Logger.getLogger(BFTMap.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
public boolean isEmpty() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsValue(Object value) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void putAll(Map m) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void clear() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Set keySet() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Collection values() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Set entrySet() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsKey(Object key) {
throw new UnsupportedOperationException("Not supported yet.");
}
public HashMap<String, byte[]> get(Object key) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package navigators.smart.tom.demo.keyvalue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import navigators.smart.tom.ServiceReplica;
import navigators.smart.tom.util.DebugInfo;
/**
*
* @author sweta
*/
//This class extends ServiceReplica and overrides its functions serializeState(), deserializeState() and executeCommand().
public class BFTMapImpl extends ServiceReplica {
//The constructor passes the id of the server to the super class
public BFTMapImpl(int id) {
super(id);
}
HashMap<String,byte[]> info2 = new HashMap<String,byte[]>();
BFTMAPUtil bftReplica = new BFTMAPUtil();
@Override
protected byte[] serializeState() {
try {
//save to file (not needed for now)
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("MyObject.ser"));
out.writeObject(bftReplica);
out.close();
// serialize to byte array and return
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(bftReplica);
out.close();
return bos.toByteArray();
} catch (IOException ex) {
Logger.getLogger(BFTMapImpl.class.getName()).log(Level.SEVERE, null, ex);
return new byte[0];
}
}
@Override
protected void deserializeState(byte[] state) {
try {
//save to file (not needed for now)
ObjectInput in = new ObjectInputStream(new FileInputStream("MyObject.ser"));
bftReplica = (BFTMAPUtil) in.readObject();
in.close();
// serialize to byte array and return
ByteArrayInputStream bis = new ByteArrayInputStream(state) ;
in = new ObjectInputStream(bis) ;
bftReplica = (BFTMAPUtil) in.readObject();
in.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(BFTMapImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(BFTMapImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
@SuppressWarnings("static-access")
public byte[] executeCommand(int clientId, long timestamp, byte[] nonces, byte[] command, boolean readOnly, DebugInfo info) {
try {
ByteArrayInputStream in = new ByteArrayInputStream(command);
ByteArrayOutputStream out = null;
byte[] reply = null;
int cmd = new DataInputStream(in).readInt();
switch (cmd) {
//operations on the hashmap
case BFTMAPUtil.PUT:
String tableName = new DataInputStream(in).readUTF();
String key = new DataInputStream(in).readUTF();
String value = new DataInputStream(in).readUTF();
byte[] b = value.getBytes();
System.out.println("Key received" + key);
byte[] ret = bftReplica.addData(tableName, key, b);
if (ret == null) {
System.out.println("inside if");
ret = new byte[0];
}
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeBytes(new String(ret));
reply = out.toByteArray();
break;
case BFTMAPUtil.GET:
String tableName1 = new DataInputStream(in).readUTF();
System.out.println("tablename"+tableName1);
String id1 = new DataInputStream(in).readUTF();
System.out.println("ID received" + id1);
byte[] b1 = bftReplica.getEntry(tableName1, id1);
String value1 = new String(b1);
System.out.println("The value to be get is"+value1);
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeBytes(value1);
reply = out.toByteArray();
break;
case BFTMAPUtil.SIZE:
String tableName2 = new DataInputStream(in).readUTF();
int size = bftReplica.getSize(tableName2);
System.out.println("Size " + size);
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(size);
reply = out.toByteArray();
break;
case BFTMAPUtil.SIZE_TABLE:
int size1 = bftReplica.getSizeofTable();
System.out.println("Size " + size1);
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeInt(size1);
reply = out.toByteArray();
break;
case BFTMAPUtil.REMOVE:
String tableName4 = new DataInputStream(in).readUTF();
String id2 = new DataInputStream(in).readUTF();
System.out.println("ID received" + id2);
byte[] b2 = bftReplica.removeEntry(tableName4, id2);
String value2 = new String(b2);
System.out.println("Value removed is : " + value2);
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeBytes(value2);
reply = out.toByteArray();
break;
case BFTMAPUtil.TAB_CREATE_CHECK:
String id3 = new DataInputStream(in).readUTF();
System.out.println("Table of Table Key received" + id3);
HashMap<String, byte[]> b3 = bftReplica.getName(id3);
boolean tableExists = b3 != null;
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeBoolean(tableExists);
reply = out.toByteArray();
break;
case BFTMAPUtil.CHECK:
String tableName7 = new DataInputStream(in).readUTF();
String id4 = new DataInputStream(in).readUTF();
System.out.println("Table Key received" + id4);
byte[] b5 = bftReplica.getEntry(tableName7,id4);
boolean tableExists1 = b5 != null;
out = new ByteArrayOutputStream();
new DataOutputStream(out).writeBoolean(tableExists1);
reply = out.toByteArray();
break;
case BFTMAPUtil.TAB_CREATE:
String table4= new DataInputStream(in).readUTF();
//ByteArrayInputStream in1 = new ByteArrayInputStream(command);
ObjectInputStream in2 = new ObjectInputStream(in) ;
HashMap<String,byte[]> table=null;
try {
table = (HashMap<String, byte[]>) in2.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(BFTMapImpl.class.getName()).log(Level.SEVERE, null, ex);
}
HashMap<String,byte[]> table1=bftReplica.addTable(table4, table);
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
ObjectOutputStream out1 = new ObjectOutputStream(bos) ;
out1.writeObject(table1);
out1.close();
in.close();
reply = bos.toByteArray();
break;
case BFTMAPUtil.TAB_REMOVE:
String tableName3 = new DataInputStream(in).readUTF();
HashMap<String,byte[]> info4=bftReplica.removeTable(tableName3);
ByteArrayOutputStream bos2 = new ByteArrayOutputStream() ;
ObjectOutputStream out3 = new ObjectOutputStream(bos2) ;
out3.writeObject(info4);
out3.close();
out3.close();
reply = bos2.toByteArray();
break;
}
return reply;
} catch (IOException ex) {
Logger.getLogger(BFTMapImpl.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package navigators.smart.tom.demo.keyvalue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import navigators.smart.tom.ServiceProxy;
import java.io.Console;
import java.util.HashMap;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
*
* @author sweta
*/
public class KVClient {
public static void main(String[] args) throws IOException {
if(args.length < 1) {
System.out.println("Usage: java KVClient <process id> ");
System.exit(-1);
}
BFTMap bftMap = new BFTMap(Integer.parseInt(args[0]));
Console console = System.console();
Scanner sc = new Scanner(System.in);
boolean res = false;
ObjectOutput out=null;
while(true) {
System.out.println("select a command : 1. CREATE A NEW TABLE OF TABLES");
System.out.println("select a command : 2. REMOVE AN EXISTING TABLE OF TABLES");
System.out.println("select a command : 3. GET THE SIZE OF THE TABLE OF TABLES");
System.out.println("select a command : 4. PUT VALUES INTO A TABLE");
System.out.println("select a command : 5. GET VALUES FROM A TABLE");
System.out.println("select a command : 6. GET THE SIZE OF A TABLE");
System.out.println("select a command : 7. REMOVE AN EXISTING TABLE");
int cmd = sc.nextInt();
switch(cmd) {
//operations on the table
case BFTMAPUtil.TAB_CREATE:
boolean result4 = false;
do {
String tableKey = console.readLine("Enter the HashMap name");
result4 = bftMap.containsKey(tableKey);
if (!result4) {
//if the table name does not exist then create the table
bftMap.put(tableKey,new HashMap<String,byte[]>());
}
} while(result4);
break;
case BFTMAPUtil.SIZE_TABLE:
//obtain the size of the table of tables.
System.out.println("Computing the size of the table");
int size=bftMap.size();
System.out.println("The size of the table of tables is "+size);
break;
case BFTMAPUtil.TAB_REMOVE:
//Remove the table entry
boolean result11 = false;
HashMap<String, byte[]> info = null;
String tableKey5 = null;
System.out.println("Executing remove operation on table of tables");
do {
tableKey5 = console.readLine("Enter the valid table name you want to remove");
result11 = bftMap.containsKey(tableKey5);
} while(!result11);
info = bftMap.remove(tableKey5);
System.out.println("Table removed");
break;
//operations on the hashmap
case BFTMAPUtil.PUT:
System.out.println("Execute put function");
boolean result5 = false;
String value = null;
String tableKey = null;
String key = null;
do {
tableKey = console.readLine("Enter the valid table name in which you want to enter the data");
result4 = bftMap.containsKey(tableKey);
if (result4) {
//if the table name does not exist then create the table
key = console.readLine("Enter the key");
value = console.readLine("Enter the value");
}
} while(!result4);
byte[] b = value.getBytes();
byte[] result = bftMap.put1(tableKey,key, b);
System.out.println("Result : "+new String(result));
break;
case BFTMAPUtil.GET:
System.out.println("Execute get function");
boolean result6 = false;
boolean result7 = false;
String value2 = null;
String tableName1 = null;
String key1 = null;
do {
tableName1 = console.readLine("Enter the valid table name from which you want to get the values");
result6 = bftMap.containsKey(tableName1);
if (result6) {
do {
key1 = console.readLine("Enter the valid key: ");
result7 = bftMap.containsKey1(tableName1, key1);
if(result7) {
byte[] result1 = bftMap.getEntry(tableName1,key1);
System.out.println("The value received from GET is"+new String(result1));
}
} while(!result7);
}
} while(!result6);
break;
case BFTMAPUtil.SIZE:
System.out.println("Execute get function");
boolean result8 = false;
String value3 = null;
String tableName2 = null;
String key2 = null;
int size1 = -1;
do {
tableName2 = console.readLine("Enter the valid table whose size you want to detemine");
result8 = bftMap.containsKey(tableName2);
if (result8) {
size1 = bftMap.size1(tableName2);
System.out.println("The size is : "+size1);
}
} while(!result8);
break;
case BFTMAPUtil.REMOVE:
System.out.println("Execute get function");
boolean result9 = false;
boolean result10 = false;
String value4 = null;
String tableName3 = null;
String key3 = null;
do {
tableName3 = console.readLine("Enter the valid table name which you want to remove");
result9 = bftMap.containsKey(tableName3);
if (result9) {
do {
key3 = console.readLine("Enter the valid key: ");
result10 = bftMap.containsKey1(tableName3, key3);
if(result10) {
byte[] result2 = bftMap.removeEntry(tableName3,key3);
System.out.println("The previous value was : "+new String(result2));
}
} while(!result10);
}
} while(!result10);
break;
}
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package navigators.smart.tom.demo.keyvalue;
/**
*
* @author sweta
*/
public class KVServer {
public static void main(String[] args){
if(args.length < 1) {
System.out.println("Use: java KVServer <processId>");
System.exit(-1);
}
BFTMapImpl bftServer = new BFTMapImpl(Integer.parseInt(args[0]));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册