Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 Kevin 1
// Memory client that interfaces with MemManager
2
public class MemClient {
3
	private Handle[] recordArray;		// Client's array of handles
4
	private MemManager memoryManager;	// Instance of a memory manager
5
 
6
	public MemClient(int input_numRecs, int input_poolSize) {
7
		// Initialize the record array and create a new memory manager
8
		recordArray = new Handle[input_numRecs];
9
		memoryManager = new MemManager(input_poolSize);
10
	}
11
 
12
	public void processLine(String line) {
13
		// Split the passed line into tokens
14
		String delim = "[ ]+";
15
		String[] tokens = line.split(delim);
16
 
17
		// Parse tokens to determine what action to take
18
		if (tokens[0].compareToIgnoreCase("insert") == 0) {
19
			processInsertCommand(tokens, recordArray, memoryManager);
20
		} else if (tokens[0].compareToIgnoreCase("remove") == 0) {
21
			processRemoveCommand(tokens, recordArray, memoryManager);
22
		} else if (tokens[0].compareToIgnoreCase("print") == 0) {
23
			processPrintCommand(tokens, recordArray, memoryManager);
24
		} else {
25
			System.out.printf("\"%s\" command is not supported\n", tokens[0]);
26
		}
27
	}
28
 
29
	public void processInsertCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) {
30
		// Process insert command
31
		if (tokens.length != 5) {
32
			System.out.printf("Arguments must be in format \"insert recnum x y name\"\n");
33
		} else {
34
			try {
35
				// Check for array bounds for record array
36
				int recordID = Integer.parseInt(tokens[1]);
37
				if (recordID < 0 || recordID > recordArray.length - 1) {
38
					System.out.printf("Arguments out of bound\n");
39
				} else {
40
					// Delete record in record array if it exists
41
					if (recordArray[recordID] != null) {
42
						memoryManager.remove(recordArray[recordID]);
43
						recordArray[recordID] = null;
44
					}
45
					// Serialize to byte array and send to memory manager
46
					int xpos = Integer.parseInt(tokens[2]);
47
					int ypos = Integer.parseInt(tokens[3]);
48
					byte[] buffer = ByteStringConverter.convertToByteArray(xpos, ypos, tokens[4]);
49
					Handle retHandle = memoryManager.insert(buffer, buffer.length);
50
					if (retHandle == null) {
51
						System.out.printf("No free space is avaliable to store the record\n");
52
					} else {
53
						// Save returned handle to the record array
54
						recordArray[recordID] = retHandle;
55
					}
56
				}
57
			} catch (NumberFormatException e) {
58
				System.out.printf("Error attempting to parse int values\n");
59
			}
60
		}
61
	}
62
 
63
	public void processRemoveCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) {
64
		// Process remove command
65
		if (tokens.length != 2) {
66
			System.out.printf("A record ID must be supplied\n");
67
		} else {
68
			try {
69
				int recordID = Integer.parseInt(tokens[1]);
70
				if (recordID < 0 || recordID > recordArray.length - 1) {
71
					System.out.printf("Arguments out of bound\n");
72
				} else {
73
					// Remove record from record array if it exists
74
					if (recordArray[recordID] != null) {
75
						memoryManager.remove(recordArray[recordID]);
76
						recordArray[recordID] = null;
77
					} else {
78
						System.out.printf("No record found for record ID %d\n", recordID);
79
					}
80
				}
81
			} catch (NumberFormatException e) {
82
				System.out.printf("Error attempting to parse int values\n");
83
			}
84
		}
85
	}
86
 
87
	public void processPrintCommand(String[] tokens, Handle[] recordArray, MemManager memoryManager) {
88
		byte[] fromPool = new byte[255];
89
		// Process print command
90
		if (tokens.length == 1) {
91
			// Print out every entry in recordArray
92
			for (int i = 0; i < recordArray.length; i++) {
93
				if (recordArray[i] != null) {
94
					fromPool = new byte[255];
95
					System.out.printf("Record #%d at address %d: ", i, recordArray[i].getAddress());
96
					memoryManager.get(fromPool, recordArray[i], 255);
97
					String[] strArray = ByteStringConverter.convertToStringArray(fromPool);
98
					System.out.printf("%s (%s,%s)\n", strArray[2].trim(), strArray[0], strArray[1]);
99
				}
100
			}
101
			// Print out free block list
102
			memoryManager.dump();
103
		} else if (tokens.length == 2) {
104
			// Print out specified record
105
			try {
106
				int recordID = Integer.parseInt(tokens[1]);
107
				if (recordID < 0 || recordID > recordArray.length - 1) {
108
					System.out.printf("Arguments out of bound\n");
109
				} else {
110
					if (recordArray[recordID] != null) {
111
						// Get and print out record
112
						memoryManager.get(fromPool, recordArray[recordID], 255);
113
						String[] strArray = ByteStringConverter.convertToStringArray(fromPool);
114
						System.out.printf("Record #%d: %s (%s,%s)\n", recordID, strArray[2].trim(), strArray[0], strArray[1]);
115
					} else {
116
						System.out.printf("No record found for record ID %d\n", recordID);
117
					}
118
				}
119
			} catch (NumberFormatException e) {
120
				System.out.printf("Error attempting to parse int values\n");
121
			}
122
		} else {
123
			System.out.printf("Invalud number of arguments\n");
124
		}
125
	}
126
}