Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Offbeatlop committed Dec 12, 2017
1 parent 46fd352 commit 65bd62b
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 3 deletions.
3 changes: 1 addition & 2 deletions BankClient/serverips.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
138.4.31.74:6788
138.4.31.75:6788
138.4.31.80:6788
138.4.31.75:6788
162 changes: 162 additions & 0 deletions BankClient/src/es/upm/dit/cnvr/server/ConnectionDispatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package es.upm.dit.cnvr.server;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;

import org.apache.zookeeper.ZooKeeper;

import es.upm.dit.cnvr.model.BankClient;
import es.upm.dit.cnvr.model.ClientDB;
import es.upm.dit.cnvr.model.OperationEnum;
import es.upm.dit.cnvr.model.ServiceStatus;
import es.upm.dit.cnvr.model.Transaction;
import es.upm.dit.cnvr.model.ServiceStatus;

public class ConnectionDispatcherTest extends Thread {

private Transaction transaction;
private Socket connection;
private ObjectInputStream inFromClient;
private DataOutputStream outToClient;
private ClientDB db;
private int id;
private ZookeeperObject zkobject;
private Operate operate;
private ZooKeeper zk = ZookeeperObject.getZk();
private static String rootBarrierOperation = "/boperation";
private Transaction transactiontest;

/**
* Constructor
* @param id The identifier of the connection
* @param connection The connection handle for sending a message to the client
* @param db The database of clients
* @param zkobject The object that administer zookeeper
* @param operate The object that create the znodes of the operations
*/
public ConnectionDispatcherTest(int id, ClientDB db, ZookeeperObject zkobject, Operate operate, Transaction transactiontest) {
this.id = id;
this.db = db;
this.zkobject = zkobject;
this.operate = operate;
this.transactiontest = transactiontest;
}

public void run () {

Handler handler;
int sequence = 0;
ServiceStatus status = null;

try {
System.out.println("ConnHandler " + id + ": socket waiting messages.");

//while (true) {
if (zk.getChildren(rootBarrierOperation, false).size() > 0){

synchronized(ZookeeperObject.getMutexOperate()){
System.out.println("He hecho wait con el mutexOperate (en ConnectionDispatcher.java): " + System.identityHashCode(ZookeeperObject.getMutexOperate()));
ZookeeperObject.getMutexOperate().wait();

}
}

transaction = transactiontest;
BankClient bc = transaction.getBankClient();
//TODO: Deberiamos meter comprobaciones para que si la operacion no la realiza bien que no cree los nodos de operaciones
if (transaction.getOperation().equals(OperationEnum.CREATE_CLIENT)){
bc.setAccount(generateId(5));
status = db.createClient(bc);
transaction.setBankClient(bc);
transaction.setStatus(status);
if (status.equals(ServiceStatus.OK)){
operate.operation(transaction);
operate.setPersonalCounter(operate.getPersonalCounter()+1);
}
}

if (transaction.getOperation().equals(OperationEnum.DELETE_CLIENT)){
status = db.deleteClient(bc.getAccount(),bc.getClientName());
transaction.setStatus(status);
if (status.equals(ServiceStatus.OK)){
operate.operation(transaction);
operate.setPersonalCounter(operate.getPersonalCounter()+1);
}
}

//TODO: Quitar en ProcessOperation, no es necesario
if (transaction.getOperation().equals(OperationEnum.READ_CLIENT)){
if (bc.getAccount() != "0") {
bc = db.readAccount(bc.getAccount());
transaction.setBankClient(bc);
transaction.setStatus(ServiceStatus.OK);
}
status = ServiceStatus.OK;
}

if (transaction.getOperation().equals(OperationEnum.UPDATE_CLIENT)){
status = db.update(bc.getAccount(), bc.getBalance());
transaction.setStatus(status);
if (status.equals(ServiceStatus.OK)){
operate.operation(transaction);
operate.setPersonalCounter(operate.getPersonalCounter()+1);
}
}

if (transaction.getOperation().equals(OperationEnum.SHOW_ALL)){

transaction.setClientdb(db);;
transaction.setStatus(ServiceStatus.OK);

}

if(es.upm.dit.cnvr.client.ClientApp.debug){
System.out.println("Debug operation");
System.out.println("Connection Dispatcher");
System.out.println("Operation: " + transaction.getOperation());
System.out.println("Account ID: " + transaction.getBankClient().getAccount());
System.out.println("Client Name: " + transaction.getBankClient().getClientName());
System.out.println("Balance: " + transaction.getBankClient().getBalance());

}

sequence ++;
//}
}
catch (NullPointerException e) {
System.out.println("Exception. Connection " + id + " Connection closed");
}
catch (Exception e) {
System.out.println(e);
}

try {
//id ++;
System.out.println("Connection " + id + ": connection closed");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("FIN CONNECTION DISPATCHER");
}
//TODO Change in the method put of HashMap -> <String, BankClient>
private static String generateId(int bloqDigits){
Random random = new Random();
StringBuilder accountId = new StringBuilder();
accountId.append("EB32-");
for(int i=0;i<bloqDigits-1;i++){
for(int j=0; j<4;j++){
accountId.append(Integer.toHexString(random.nextInt(16)));
}
if(i!= bloqDigits -2)
accountId.append("-");
}
return accountId.toString().toUpperCase();
}

}
2 changes: 1 addition & 1 deletion BankClient/src/es/upm/dit/cnvr/server/TCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception {

ServerSocket welcomeSocket = new ServerSocket(6788);


while (true) {
try {
connectionSocket = welcomeSocket.accept();
Expand Down
43 changes: 43 additions & 0 deletions BankClient/src/es/upm/dit/cnvr/server/TCPServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package es.upm.dit.cnvr.server;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import es.upm.dit.cnvr.model.BankClient;
import es.upm.dit.cnvr.model.ClientDB;
import es.upm.dit.cnvr.model.OperationEnum;
import es.upm.dit.cnvr.model.Transaction;
import es.upm.dit.cnvr.server.ZookeeperObject;


public class TCPServerTest {

public static void main(String[] args) throws Exception {

Socket connectionSocket;
int id = 0;
ClientDB db = ClientDB.getInstance();
ZookeeperObject zkobject = new ZookeeperObject();
zkobject.configure();
Operate operate = zkobject.getOperate();
int loop = 100;
Transaction transaction;

for (int i=0; i<=loop; i++) {
try {
Thread.sleep(750);
transaction = new Transaction(OperationEnum.CREATE_CLIENT, new BankClient("0", "User de prueba", i));
ConnectionDispatcherTest conHandler = new ConnectionDispatcherTest(id, db, zkobject, operate, transaction);
System.out.println("Get a socket connection");
conHandler.start();
System.out.println("Reach after start");
id++;
} catch (Exception e) {
System.out.println("Closed the socket");
}
}
//welcomeSocket.close();
}

}

0 comments on commit 65bd62b

Please sign in to comment.