Preview
ATM code not working
// This is the server code.
import java.net.*;
import java.io.*;
import java.util.StringTokenizer;
import java.util.*;
public class Server {
private ServerSocket serverSocket;
public static ATMImplementation atmImplementation;
private BufferedReader bufferedReader;
List<ATMRunnable> requestQueue; //= new ArrayList<ATMRunnable>();
ATMThread[] atmthread = new ATMThread[5];
private Object lock1 = new Object();
public Server(int port) throws java.io.IOException,InterruptedException
{
serverSocket = new ServerSocket(port);
atmImplementation = new ATMImplementation();
requestQueue = Collections.synchronizedList(new ArrayList<ATMRunnable>()); }
/** serviceClient accepts a client connection and reads lines from the socket.
* Each line is handed to executeCommand for parsing and execution.
*/
public void serviceClient (ATMImplementation atmImplementation) throws java.io.IOException,InterruptedException
{
System.out.println("Accepting clients now");
Socket clientConnection = serverSocket.accept();
// Arrange to read input from the Socket
InputStream inputStream = clientConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// Arrange to write result across Socket back to client
OutputStream outputStream = clientConnection.getOutputStream();
// PrintStream printStream = new PrintStream(outputStream);
System.out.println("Client acquired on port #" + serverSocket.getLocalPort() + ", reading from socket");
String commandLine;
for(int i=0;i<5;i++)
{ while((commandLine = bufferedReader.readLine()) != null)
{try { ATMRunnable atmrunnable = new ATMRunnable(commandLine,outputStream,atmImplementation);
synchronized(lock1)
{requestQueue.add(atmrunnable);}
}catch (Exception atmex) {
System.out.println("ERROR: " + atmex);
}
atmthread[i]=new ATMThread(requestQueue); // here i create ATMThread objects which in turn start threads.
}
lock1.notifyAll(); }
}
public static void main(String argv[])
{
int port = 1099;
if(argv.length > 0) {
try {
port = Integer.parseInt(argv[0]);
} catch (Exception e) { }
}
try {
Server server = new Server(port);
server.serviceClient(atmImplementation);
System.out.println("Client serviced");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// ATMThread code :
import java.util.StringTokenizer;
import java.io.*;
import java.net.*;
import java.util.*;
class ATMThread
{ Thread t;
private Object lock1 = new Object();
public ATMThread(List<ATMRunnable> requestQueue) throws InterruptedException
{ t = new Thread(processrequestQueue(requestQueue));
t.start();
}
public ATMRunnable processrequestQueue(List<ATMRunnable> requestQueue) throws InterruptedException
{ ATMRunnable atmRunnableItem=null;
synchronized(lock1)
{ while(requestQueue.isEmpty())
{ try{ requestQueue.wait();
} catch (InterruptedException e)
{e.printStackTrace(); }
}
Iterator<ATMRunnable> iterator = requestQueue.iterator();
if(iterator.hasNext())
{ atmRunnableItem = iterator.next();
iterator.remove();
}
lock1.notifyAll();
return atmRunnableItem;
}
}
}
// ATMRunnable has run() method.
// Here is the code for ATMRunable :
import java.util.StringTokenizer;
import java.io.*;
import java.net.*;
import java.util.*;
class ATMRunnable implements Runnable{
Socket clientConnection;
//PrintStream printStream;
ATMImplementation atmImplementation;
private Object lock1 = new Object();
private String commandLine;
OutputStream outputStream;
PrintStream printStream;
public ATMRunnable(String commandLine,OutputStream outputStream,ATMImplementation atmImplementation)
{ this.commandLine = commandLine;
this.atmImplementation=atmImplementation;
this.outputStream=outputStream;
}
public String getCommandLine()
{ return commandLine;
}
public void run()
{
synchronized(lock1)
{System.out.println(Thread.currentThread() + "is running");
printStream = new PrintStream(outputStream);
while(this.commandLine!=null)
{
try{
Float result = executeCommand(this.commandLine);
if(result != null) { // Only BALANCE command returns non-null
printStream.println(result); // Write it back to the client
}
}
catch (ATMException atmex) {
System.out.println("ERROR: " + atmex);
}
}
}
lock1.notifyAll();
}
private Float executeCommand(String commandLine) throws ATMException {
// Break out the command line into String[]
StringTokenizer tokenizer = new StringTokenizer(commandLine);
String commandAndParam[] = new String[tokenizer.countTokens()];
int index = 0;
while(tokenizer.hasMoreTokens()) {
commandAndParam[index++] = tokenizer.nextToken();
}
String command = commandAndParam[0];
// Dispatch BALANCE request without further ado.
if(command.equalsIgnoreCase(Commands.BALANCE.toString())) {
return this.atmImplementation.getBalance();
}
// Must have 2nd arg for amount when processing DEPOSIT/WITHDRAW commands
if(commandAndParam.length < 2) {
throw new ATMException("Missing amount for command \"" + command + "\"");
}
try {
float amount = Float.parseFloat(commandAndParam[1]);
if(command.equalsIgnoreCase(Commands.DEPOSIT.toString())) {
this.atmImplementation.deposit(amount);
}
else if(command.equalsIgnoreCase(Commands.WITHDRAW.toString())) {
this.atmImplementation.withdraw(amount);
} else {
throw new ATMException("Unrecognized command: " + command);
}
} catch (NumberFormatException nfe) {
throw new ATMException("Unable to make float from input: " + commandAndParam[1]);
}
// BALANCE command returned result above. Other commands return null;
return null;
}
}
This is the code to be checked. rest all seems fine.