0 Replies - 2261 Views - Last Post: 04 December 2012 - 11:17 PM Rate Topic: -----

#1 J0hnnysmokes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-November 12

server-client simulation using queues

Posted 04 December 2012 - 11:17 PM

Here are the specs of my project

Write a general-purpose client-server simulation program that determines how long clients must
wait in line before being served.
3 8 10 11 18
23
2
Server
Wait Queue
front rear
at specified time
clients arrive
when server free
clients served
Current time: 23
Time required to serve client: 10
Your simulation will be time-driven, i.e. something (potentially) will happen at every clock tick.
Your system \clock" will consist of an integer counter that is incremented to indicate the passing
of a unit of time. Clients will arrive randomly (more on this later) to be placed in a queue until
the server is free. When the server is free the client (if one exists) at the front of the wait queue
is removed from the queue and \served". Each client requires a set, xed amount of time to be
served; this time will be a constant for each simulation run. Each clock cycle all clients will do one
(or more) of the following:
 wait to enter the system at its scheduled time (i.e. it's not time yet for the client to be placed
in the queue)
 arrive into the system at its scheduled time and be placed in the wait queue
 remain waiting in the wait queue
 move from the front of the queue to the server
 be serviced by the server
 nish being served by the server and leave the system
After your simulation has run for the given amount of time, your program will print a speci ed set
of statistics about the simulation.
2 Input
Input will come from a text le whose name should be given on the command-line. For each sim-
ulation (your program should be able to perform multiple simulations each time it executes) you
will input the length of the simulation, the probability that a client may arrive in a clock cycle
(more on this later), the amount of time required to service a client, and then a list of integers
1representing the arrival times of each client. This list should be terminated by the sentinel value
-1.
For example, below is a data set for a simulation that is to run for 100 clock cycles with a probability
of 10% (i.e. 0.1) that a client will arrive each clock cycle, each client takes 2 clock cycles to be
served, and clients arrive at times 14, 20, 24, 29, 36, 38, 47, 48, 51, 54, 56, 59, 75, 76, 81, and 91:
100
0.1 2
14 20 24 29 36 38 47 48 51 54
56 59 75 76 81 91 -1
Note that your data les must have this format!
Normally the randomness in simulations like this is built into the program, i.e. given a certain
inputted probability every run of your simulation would produce slightly di erent output. How-
ever, since it is dicult to grade a project where everyone's output could and probably would be
di erent, we will be generating the data set apart from the program. A Java program will be
provided (GenArrivals.java, available via a link o of the class web pages) that can be used to
automatically generate data les from inputted parameters. If you use this program to create your
data les, client arrival times are randomly generated using the given probability of arrival. It is
intended that you run this program to generate data les prior to executing your simulation.
Your program should be prepared to perform multiple simulations each time it is run (see the
example run below). Data les may contain data for several simulations; each data set will be
terminated by the sentinel value of -1.
In addition, your program must check that the data is valid:
 the length of the simulation must be  1
 the probability P that a client arrives each clock cycle must be such that 0:0  P  1:0
 the amount of time required to service every client must be  1
 the rst client cannot arrive before time 1, no two clients can arrive to be serviced at the
same time (i.e. during the same clock cycle), and each new client must have an arrival time
that is greater than the previous one
You should throw an exception (of type java.lang.Exception) with an appropriate error message
should any of these rules be violated. You should only throw an exception at the moment erroneous
data is actually needed by your program, e.g. if two jobs in the data set both arrive at time 246 but
the simulation only runs for 200 clock cycles, no exception should be thrown. Also you should run
the simulation and output statistics on all valid data sets prior to encountering any invalid data,
e.g. if an error occurs in the third data set in the le, run the simulations and report on the rst
two prior to throwing an exception upon encountering the problem in the third.
23 Output
Your program should echo print the data le name and print out the following statistics for each
simulation: the arrival probability, service time, simulation length, average wait, total clients in
system, total clients completely served, and total clients \stranded" in the wait queue or server.
You must format and present your output exactly like it is shown in the sample run below.
Stranded clients are those clients that remain in the wait queue or server as time elapses. Total
wait time is the total amount of time a client has waited before it goes to the server to be served.
Note that the average wait time is the total wait time divided by the number of clients that have
moved to the server (not the number of clients that have been totally served, though the two values
can be the same).
4 Sample Data
Below is the complete contents of data le test.dat used in the example run to follow:
100
0.2 5
6 11 12 16 18 23 28 35 37 38
43 44 45 51 54 59 60 62 64 66
67 74 81 84 85 86 92 94 97 99
-1
100
0.2 10
2 3 6 9 13 19 20 24 25 30
31 51 65 72 79 88 89 95 -1
5 Sample Run
Executed on a Windows/DOS machine:
c:\javaprogs> javac -cp queue.jar;. QSys.java
c:\javaprogs> java -cp queue.jar;. QSys test.dat
Data file: test.dat
Arrival Service Sim. Average Total Total Total
prob. time length wait clients served stranded
------------------------------------------------------------
0.20 5 100 18.50 30 15 15
0.20 10 100 32.56 18 9 9
c:\javaprogs>
6 Algorithm & Implementation Suggestions
Listed below is a rough algorithm for a single simulation given just to help you get started:
3initializations
while the clock <= time limit do
if it's time for a client to arrive then
add the client to the wait queue
end if
if server is free then
if the queue is not empty then
move client at front of queue to server
end if
else
service client in server
end if
end while
print simulation statistics
Here are a few implementation suggestions:
 get all of your data input working correctly before proceeding to coding the simulation
 de ne simple classes for separate server and client objects (remember that all Java source
code and compiled classes must be included in the jar le you submit)
 get the entire simulation working correctly before you worry about statistics
 set up diagnostic output \sca olding" to enable you to trace execution and debug your code
7 Speci cation of Class queue.Queue
You must use the class queue.Queue as provided to you in the jar le queue.jar (available o of
the class web pages). Here is the speci cation of queue.Queue:
package queue;
public class Queue <T> {
public Queue();
public boolean isEmpty();
public void makeEmpty();
public T getFront() throws Underflow;
public T dequeue() throws Underflow;
public void enqueue(T x);
}
The class queue.Underflow is also provided for you in the package queue.
4





Here is my code
import java.io.File; 
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
import queue.Queue;
import queue.Underflow;
public class Proj41773 {
        static int timeLimit; 
        static int ServiceTime;
        static double chance;
        static int[] items;
        static boolean DEBUG = false;
        public static void main(String [] args) throws Underflow, Exception {
                 
                         File file = new File(args[0]);
                         Scanner input = new Scanner(file);
                         System.out.println("Arrival   Service  Sim.    Average Total   Total   Total");
                         System.out.println("prob.     time     length  wait    clients served  stranded");
                         System.out.println("------------------------------------------------------------");
                         
                         GetInput(input);
               
                 }
         
 
         

         public static void GetInput(Scanner input) throws Exception{
                 while(input.hasNext()){
                         
                         timeLimit = input.nextInt();
                         if(DEBUG) System.out.println("Time Limit: " + timeLimit);
                         if(1 > timeLimit){
                                throw new Exception("The Lenght of the simulation is less than 1");
                         }
                         chance = input.nextDouble();
                         if(chance < 0 || chance > 1){
                                 throw new Exception("Probability is out of bounds ");
                         }
                         if(DEBUG) System.out.println("Probablility: " + chance);
                         ServiceTime = input.nextInt();
                         if(1 > ServiceTime){
                                 throw new Exception("The time to service a client is less than 1");
                         }
                         if(DEBUG) System.out.println("Time to service a client: " + ServiceTime);
                         
                         if(timeLimit >= 1){
                         items = new int[timeLimit];
                         }
                         boolean notSentinel  = true;
                         int Sentinel = 1;
                         for(int i = 0; i < items.length; i++)
                         {
                        	 if(items[i] == -1) {
                        		 Sentinel = items[i];
                        	 }
                         }
                         
                         int counter = 0;
                         while(notSentinel){
                        	     if (counter != chance && counter != ServiceTime && counter != timeLimit){
                        	    	 items[counter] = input.nextInt();
                                 }
                                 if(items[counter] == Sentinel){
                                         items[counter] = 0;
                                         notSentinel = false;
                                         runServer();
                                         counter--;
                                 }
                                 counter++;
                         }
                 
                 
                 }
         }
         
                public static void runServer() throws Underflow{
                        Queue<Integer> que = new Queue<Integer>();
                        int numberOfItemsInData = 0;
                        int numberOfItemsServed = 0;
                        Queue<Integer> serviceQue = new Queue<Integer>();
                        for(int i = 0; i < items.length; i++){
                                if(items[i] != 0){
                                que.enqueue(items[i]);
                                numberOfItemsInData++;
                                }
                        }
                        if(DEBUG)System.out.println("Initializing server.... ");
                        if(DEBUG)System.out.println(toString(que));
                        
                        boolean serverBusy = false;
                        int arrival = 1;
                        int serviced = -1;
                        int waitTime = 0;
                        int clock = 0;
                        int numberOfItemsInQ = 0;
                       
                        while(timeLimit > clock){
                                int frontOfQueue = 0;
                                if(DEBUG)System.out.println("Server Time: "+ clock + " Contents of Queue " + toString(serviceQue));
                                
                                if(DEBUG)System.out.println("Original queue: "+ toString(que));
                                try{
                                        frontOfQueue = que.getFront();
                                }catch(Exception e){
                                       
                                }
                                if(!que.isEmpty()){
                                if(clock == que.getFront()){
                                        serviceQue.enqueue(que.dequeue()); 
                                        numberOfItemsInQ++;
                                }
                                }
                                
                                if(!serverBusy && !serviceQue.isEmpty()){
                                        serviced = serviceQue.dequeue();
                                       
                                        serverBusy = true;
                                        if(DEBUG)System.out.println("Client started receving service: " + serviced);
                                }else if(serverBusy){
                                       
                                        arrival++;
                                        if(DEBUG)System.out.println(" Client being served: " + serviced + " "  + "  has been serviced for " + arrival);
                                        if(arrival == ServiceTime){
                                                serverBusy = false;
                                                numberOfItemsServed++;
                                                arrival = 0;
                                        }
                                        
                                }
                        clock++;      
                        }
                        int totalStranded = (numberOfItemsInData - numberOfItemsServed);
    
                        DecimalFormat df = new DecimalFormat("0.00");
                        String chanceFormated = df.format(chance);
                        
                        System.out.println(chanceFormated+"	   "+ServiceTime+"	    "+timeLimit+"	    "+waitTime+"	    "+numberOfItemsInData+"	    "+numberOfItemsServed+"	     "+totalStranded);
                       
                }
               

 
                    public static String toString(Queue<Integer> q) throws Underflow {
                        Queue<Integer> t = new Queue<Integer>();
                        String str = "(front)[";
                        Integer n;
                        if (q.isEmpty() == false) {
                            str += (n=q.dequeue());
                            t.enqueue(n);
                        }
                        while(q.isEmpty() == false) {
                            str += ","+(n=q.dequeue());
                            t.enqueue(n);
                        }
                        while (t.isEmpty() == false)
                            q.enqueue(t.dequeue());
                        return (str+"]");
                    }
}





Here is the code for the queue class
import queue.Queue;

public class TestQueue {


    public static void main(String [] args) throws queue.Underflow {
	Queue<Integer> q = new Queue<Integer>();

       	for (int i = 1; i <= 5; i++)
	    q.enqueue(i);
	System.out.println(toString(q));
	System.out.println(toString(q));
    }

    public static String toString(Queue<Integer> q) throws queue.Underflow {
	Queue<Integer> t = new Queue<Integer>();
	String s = "(front)[";
	Integer n; 
	if (!q.isEmpty()) {
	    s += (n=q.dequeue());
	    t.enqueue(n);
	}
	while(!q.isEmpty()) {
	    s += ","+(n=q.dequeue());
	    t.enqueue(n);
	}
	while (!t.isEmpty())
	    q.enqueue(t.dequeue());
	return (s+"]");
    }
}



Here is the input file used to test the project

300
0.34 3
8 9 11 13 17 18 25 26 27 30
36 39 40 41 43 45 50 51 55 56
60 61 62 66 67 72 74 76 78 79
80 81 82 87 92 94 95 96 98 99
101 102 103 104 107 109 113 115 117 118
119 120 124 126 128 134 135 141 142 144
149 151 152 154 162 165 168 170 171 172
174 175 184 185 187 193 195 199 212 216
219 223 225 226 227 235 236 238 242 243
244 246 248 249 250 251 256 257 258 259
262 268 269 271 273 275 278 279 281 284
287 288 296 302 303 304 305 306 309 310
311 316 320 326 327 328 331 334 342 345
346 347 349 353 356 360 -1
5
0.8 3
1 2 3 4 5 -1
425
0.6 2
2 4 5 6 7 9 12 14 15 16
17 18 20 21 22 23 26 27 28 31
32 33 35 36 37 38 40 41 42 43
44 46 47 49 53 54 55 58 60 61
63 67 68 69 70 71 72 75 76 77
78 79 80 82 83 84 85 87 89 91
93 94 97 98 99 102 104 106 108 110
113 115 116 118 120 121 122 123 125 127
128 132 133 134 138 139 140 141 143 144
145 147 148 149 150 151 154 158 161 164
167 170 171 172 173 176 177 179 181 182
183 184 186 193 194 196 197 199 200 202
203 204 206 207 208 210 211 212 214 215
216 217 218 219 221 223 225 227 228 230
232 233 238 239 240 241 243 244 245 250
251 254 255 256 257 259 260 262 264 266
267 270 271 272 273 274 275 276 279 281
282 283 284 285 286 289 290 291 292 293
294 295 297 301 302 303 304 307 308 309
310 311 312 313 314 315 317 318 319 320
321 322 324 329 330 331 332 334 335 337
338 339 340 342 344 345 346 348 349 350
351 352 354 355 359 361 362 363 365 366
368 370 374 375 377 378 379 380 382 383
384 386 387 389 390 394 396 398 399 400
401 402 403 406 408 410 411 412 416 418
422 424 425 428 430 433 434 437 439 440
441 442 443 446 447 449 450 451 454 455
456 458 460 463 464 465 466 467 470 473
476 480 481 482 483 485 488 490 491 494
495 497 498 500 501 504 505 507 -1


here is my output
Arrival   Service  Sim.    Average Total   Total   Total
prob.     time     length  wait    clients served  stranded
------------------------------------------------------------
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at Proj41773.GetInput(Proj41773.java:72)
	at Proj41773.main(Proj41773.java:31)




Here is the issue, when my code runs the sample data it works fine (minus the average wait time) However, the data used to test the program had values that was greater than simulation time that doesnt work with my code's logic. What I am trying to do is read the file without it being affected by the fact the values are higher than simulation time.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1