-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46fd352
commit 65bd62b
Showing
4 changed files
with
207 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
162
BankClient/src/es/upm/dit/cnvr/server/ConnectionDispatcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |