| 93 |
Kevin |
1 |
/*
|
|
|
2 |
* CS 3114 Project 1
|
|
|
3 |
* Author: Kevin Lee
|
|
|
4 |
* Compiler: Eclipse 3.7.0
|
|
|
5 |
* Operating System: Windows 7 x64
|
|
|
6 |
* Date Completed: 9/1/2011
|
|
|
7 |
*
|
|
|
8 |
* The following program is designed to provide an interface to
|
|
|
9 |
* client side code that allows the client to store and retrieve
|
|
|
10 |
* byte arrays of data. The memory manager has no information on
|
|
|
11 |
* the actual data to be stored, other than the size of the byte
|
|
|
12 |
* array. The memory manager allocates a memory pool when the
|
|
|
13 |
* object is created, and executes all operations on the memory
|
|
|
14 |
* pool. The memory manager also keeps a doubly linked list of
|
|
|
15 |
* empty blocks from which it finds the best fit block to store
|
|
|
16 |
* the passed data in. The client is responsible for parsing the
|
|
|
17 |
* command file as well as creating the byte array holding the
|
|
|
18 |
* data to be stored. The client also has a record array which
|
|
|
19 |
* stores handles passed back from the memory manager from which
|
|
|
20 |
* it uses to retrieve stored data from the memory manager.
|
|
|
21 |
*
|
|
|
22 |
* The stored data is saved in the following format:
|
|
|
23 |
* byte[0] = size of data record
|
|
|
24 |
* byte[1-4] = int (x-coordinate)
|
|
|
25 |
* byte[5-8] = int (y-coordinate)
|
|
|
26 |
* byte[9-?] = string (name of city name)
|
|
|
27 |
* The first byte is only used by the memory manager and is not
|
|
|
28 |
* returned to the client when the data record is retrieved.
|
|
|
29 |
*
|
|
|
30 |
* On my honor:
|
|
|
31 |
*
|
|
|
32 |
* - I have not used source code obtained from another student,
|
|
|
33 |
* or any other unauthorized source, either modified or
|
|
|
34 |
* unmodified.
|
|
|
35 |
*
|
|
|
36 |
* - All source code and documentation used in my program is
|
|
|
37 |
* either my original work, or was derived by me from the
|
|
|
38 |
* source code published in the textbook for this course.
|
|
|
39 |
*
|
|
|
40 |
* - I have not discussed coding details about this project with
|
|
|
41 |
* anyone other than my partner (in the case of a joint
|
|
|
42 |
* submission), instructor, ACM/UPE tutors or the TAs assigned
|
|
|
43 |
* to this course. I understand that I may discuss the concepts
|
|
|
44 |
* of this program with other students, and that another student
|
|
|
45 |
* may help me debug my program so long as neither of us writes
|
|
|
46 |
* anything during the discussion or modifies any computer file
|
|
|
47 |
* during the discussion. I have violated neither the spirit nor
|
|
|
48 |
* letter of this restriction.
|
|
|
49 |
*/
|
|
|
50 |
import java.io.*;
|
|
|
51 |
|
|
|
52 |
public class memman {
|
|
|
53 |
public static void main(String[] args) {
|
|
|
54 |
|
|
|
55 |
int input_poolSize; // Size of memory pool to allocate
|
|
|
56 |
int input_numRecs; // Total number of records to hold
|
|
|
57 |
String input_commandFile; // Location of file to parse
|
|
|
58 |
|
|
|
59 |
// 3 arguments must be supplied to the program
|
|
|
60 |
if (args.length != 3) {
|
|
|
61 |
System.out.println("3 Arguments must be supplied!");
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// Parses passed arguments into member variables
|
|
|
66 |
try {
|
|
|
67 |
input_poolSize = Integer.parseInt(args[0]);
|
|
|
68 |
input_numRecs = Integer.parseInt(args[1]);
|
|
|
69 |
input_commandFile = args[2];
|
|
|
70 |
} catch (NumberFormatException e) {
|
|
|
71 |
System.out.printf("The first two arguments must be an integer");
|
|
|
72 |
return;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Handles error if the passed arguments are negative numbers
|
|
|
76 |
if (input_poolSize < 0 || input_numRecs < 0) {
|
|
|
77 |
System.out.printf("Int arguments cannot be negative");
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
// Check to make sure the command file exists
|
|
|
82 |
File cmdFile = new File(input_commandFile);
|
|
|
83 |
if (!cmdFile.exists()) {
|
|
|
84 |
System.out.printf("Command file not found");
|
|
|
85 |
return;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
MemClient memoryClient = new MemClient(input_numRecs, input_poolSize);
|
|
|
89 |
|
|
|
90 |
// Parse each command with the memory client
|
|
|
91 |
try {
|
|
|
92 |
BufferedReader reader = new BufferedReader(new FileReader(cmdFile));
|
|
|
93 |
String cin;
|
|
|
94 |
while ((cin = reader.readLine()) != null) {
|
|
|
95 |
cin = cin.trim();
|
|
|
96 |
if (cin.length() != 0) {
|
|
|
97 |
memoryClient.processLine(cin);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
} catch (FileNotFoundException e) {
|
|
|
101 |
System.out.printf("Command file not found");
|
|
|
102 |
return;
|
|
|
103 |
} catch (IOException e) {
|
|
|
104 |
System.out.printf("Unable to read input file");
|
|
|
105 |
return;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|