Monday, June 14, 2010

b. Implementation of queue

-------------------------------------------------------------------------------------------------------------
SOURCE CODE
----------------------------------------------------------------------------------------------


Server Program :

import java.io.*;
import java.net.*;

//class implementing Queue operations
class Queue{

private int queue[];
private int toq;
private int eoq;

// allocate and initialize queue
Queue(int size){
queue = new int[size];
toq = 0;
eoq = -1;
}
//check whether queue is full
boolean isFull(){
if(eoq == queue.length-1)
return true;
else
return false;
}
//check whether queue is empty
boolean isEmpty(){
if(eoq < 0)
return true;
else
return false;
}

// Push an item into queue
void push(int item){
for(int i=eoq;i>-1;i--){
queue[i+1]=queue[i];
}
++eoq;
queue[0] = item;
}

// Pop an item from the queue
int pop(){
return queue[eoq--];
}
//get all elements of queue
int[] getQueue(){
return queue;
}
//get current size of queue
int getCurrentSize(){
return (eoq+1);
}
}

//class implementing Queue Server operations
class QueueServer implements Runnable{

static ServerSocket serverSocket;
Socket socket;
BufferedReader read,from;
PrintWriter to;
Thread thread;
Queue queue;
int clientID;
static int clientCounter=0;

//constructor
QueueServer(Socket socket)throws Exception{

this.socket=socket;
clientID=++clientCounter;
System.out.println("New Client connection accepted. ClientID: "+clientID);
from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
to=new PrintWriter(socket.getOutputStream(),true);
thread=new Thread(this);
thread.start();
}

//run() method for each client request thread
public void run(){

try{
to.println("Enter size of the queue: ");
int size=Integer.parseInt((from.readLine()).trim());
queue=new Queue(size);
int item=0;
String action=" ";
to.println("You can \"push\" \"pop\" \"display\" elements or \"exit\" What next??");

do{

action=(from.readLine()).toLowerCase();

if(action.equals("push")){
if(queue.isFull())
to.println("Queue Full. What next??");
else{
to.println("Enter element: ");
item=Integer.parseInt((from.readLine()).trim());
queue.push(item);
to.println("Pushed item. What next??");
}
}
else if(action.equals("pop")){
if(queue.isEmpty())
to.println("Stack Empty. What next??");
else
to.println("Popped item: "+queue.pop()+" What next??");
}
else if(action.equals("display")){
if(queue.isEmpty())
to.println("Queue Empty. What next??");
else{
int[] q=queue.getQueue();
String queueStr="";
for(int i=0;i queueStr=queueStr+" "+q[i];
to.println("Queue contains: "+queueStr+" What next??");
}
}
else if(action.equals("exit")){
socket.close();
break;
}
else
to.println("Invalid option. What next??");

}while(!(action=="exit"));

System.out.println("Client "+clientID+" exited.");

}catch(Exception e){System.out.println(e);}
}

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

serverSocket=new ServerSocket(2222);

System.out.println("Server Started. Press Ctrl+C to close server.");
while(true){
Socket s=serverSocket.accept();
QueueServer server=new QueueServer(s);
}
}
}


Client Program :

import java.io.*;
import java.net.*;

//class implementing client operations
class QueueClient{

//main() method
public static void main(String args[]){

try{
String in="";
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter server address: ");
Socket socket=new Socket(read.readLine(),2222);
BufferedReader from=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter to=new PrintWriter(socket.getOutputStream(),true);

//read write loop of client
while(true){
System.out.println(from.readLine());
in=read.readLine();
to.println(in);
if(in.equalsIgnoreCase("exit")){
socket.close();
break;
}
}
System.out.println("Client closing.");
}catch(Exception e){e.printStackTrace(System.out);}
}
}

No comments:

Post a Comment