1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.ext;
25
26 import java.util.*;
27 import java.io.*;
28
29 import zeus.util.*;
30 import zeus.concepts.Performative;
31 import zeus.concepts.ZeusParser;
32 import zeus.agents.PersistentStore;
33 import zeus.concepts.ZeusParser;
34
35
36 public class FlatFile extends PersistentStore {
37
38 protected Hashtable ht = new Hashtable();
39 String fsep = System.getProperty("file.separator");
40 String ROOT = System.getProperty("user.dir") + fsep + "dbs" + fsep;
41 String NEWLINE = System.getProperty("line.separator");
42 String NEWRECORD = NEWLINE + "_:RECORD";
43 String RECORD_TAG = "_:RECORD";
44 String PREFIX = "ps_";
45
46
47
48 public void createSession(String replyKey, String agent,
49 String sessionType, String sessionId, String accessKey){
50
51 Performative msg;
52 String type;
53 try{
54 if (isAccessible())
55 type = ROOT + sessionType;
56 else
57 type = ROOT + PREFIX+agent + fsep + sessionType;
58
59 File folder = new File(type);
60 if(!folder.exists()) {
61 if (!folder.mkdirs()) throw new SecurityException("Couldn't create sessionType");
62 }
63
64 msg = new Performative("inform");
65 msg.setReceiver(agent);
66 msg.setInReplyTo(replyKey);
67
68 File file = new File(type + fsep + sessionId);
69 if (file.exists())
70 msg.setContent(sessionType + " created by "+ agent + " already exists... appending.");
71 else
72 msg.setContent("done");
73
74 RandomAccessFile fp = new RandomAccessFile(file,"rw");
75 ht.put(accessKey, fp);
76
77 }
78 catch(IOException ioe) {
79 ioe.printStackTrace();
80 msg = new Performative("failure");
81 msg.setReceiver(agent);
82 msg.setInReplyTo(replyKey);
83 msg.setContent(agent + " couldn't create " + sessionId);
84 }
85 catch(SecurityException se) {
86 se.printStackTrace();
87 msg = new Performative("failure");
88 msg.setReceiver(agent);
89 msg.setInReplyTo(replyKey);
90 msg.setContent("Access to create denied by security manager");
91 }
92
93 proxy.sendMsg(msg);
94 }
95
96 public void deleteSession(String replyKey, String agent,
97 String sessionType, String sessionId) {
98
99 Performative msg=null;
100 String fname;
101
102 if (isAccessible())
103 fname = ROOT + sessionType + fsep +
104 sessionId;
105 else
106 fname = ROOT + PREFIX+agent + fsep + sessionType + fsep +
107 sessionId;
108
109 File file = new File(fname);
110 try {
111 if (file.exists()) {
112
113 if (!file.delete())
114 throw new IOException("Delete failed. Close session" + sessionId);
115 msg = new Performative("inform");
116 msg.setReceiver(agent);
117 msg.setInReplyTo(replyKey);
118 msg.setContent("done");
119 }
120 else {
121 msg = new Performative("failure");
122 msg.setReceiver(agent);
123 msg.setInReplyTo(replyKey);
124 msg.setContent(sessionId + " doesn't exist, delete failed");
125 }
126 }
127 catch (IOException ioe){
128 ioe.printStackTrace();
129 }
130
131 proxy.sendMsg(msg);
132 }
133
134 public void countRecords(String replyKey,String agent,String accessKey){
135 int recordCount=0;
136 long currentPos = 0;
137 Performative msg = null;
138
139
140 try {
141 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
142 currentPos = fp.getFilePointer();
143
144 if (fp == null)
145 throw new IOException("Error counting records with access " + accessKey);
146
147 recordCount = getCountUpTo(fp, fp.length());
148 msg = new Performative("inform");
149 msg.setReceiver(agent);
150 msg.setContent(Integer.toString(recordCount));
151 msg.setInReplyTo(replyKey);
152 fp.seek(currentPos);
153 }
154 catch (IOException e){
155 e.printStackTrace();
156 msg = new Performative("failure");
157 msg.setReceiver(agent);
158 msg.setContent("-1");
159 msg.setInReplyTo(replyKey);
160 }
161 proxy.sendMsg(msg);
162 }
163
164 private int getCountUpTo(RandomAccessFile fp, long length){
165
166 int count = 0;
167 String buf ;
168
169 try {
170 fp.seek(0);
171 while (fp.getFilePointer() < length) {
172 buf = fp.readLine();
173
174 if (buf.equals(RECORD_TAG))
175 count += 1;
176 }
177 fp.seek(length);
178 }
179 catch (IOException e){
180 e.printStackTrace();
181 }
182 return count;
183 }
184
185 public void getAllSessions(String replyKey, String agent,
186 String sessionType){
187
188 Performative msg;
189 String list = "";
190 String fname;
191
192 if (isAccessible())
193 fname = ROOT + sessionType;
194 else
195 fname = ROOT + PREFIX+agent + fsep + sessionType;
196
197 File folder = new File(fname);
198
199 if(!folder.exists()) {
200 System.out.println("Session type doesn't exist");
201 msg = new Performative("failure");
202 msg.setReceiver(agent);
203 msg.setContent("Session type doesn't exist");
204 msg.setInReplyTo(replyKey);
205 proxy.sendMsg(msg);
206 return;
207 }
208
209 String[] dir = folder.list();
210 msg = new Performative("inform");
211 msg.setReceiver(agent);
212 msg.setContent(Misc.concat(dir));
213 msg.setInReplyTo(replyKey);
214
215 proxy.sendMsg(msg);
216 }
217
218 public void deleteSessionType(String replyKey, String agent,
219 String sessionType){
220
221 Performative msg;
222 File f;
223 String fname;
224
225 if (isAccessible())
226 fname = ROOT + sessionType;
227 else
228 fname = ROOT + PREFIX+agent + fsep + sessionType;
229
230 File folder= new File(fname);
231
232 if(!folder.exists()) {
233 System.out.println("Session type doesn't exist");
234 msg = new Performative("failure");
235 msg.setReceiver(agent);
236 msg.setContent("Session type doesn't exist: delete failed.");
237 msg.setInReplyTo(replyKey);
238 proxy.sendMsg(msg);
239 return;
240 }
241
242 try {
243 String[] dir = folder.list();
244 for(int i=0; i<dir.length;i++) {
245 f = new File(fname +fsep+ dir[i]);
246 if (f.exists()) {
247 if (!f.delete())
248 throw new IOException("Delete Failed close all sessions");
249 }
250 }
251
252 if (!folder.delete() )
253 throw new IOException("Delete Failed close all sessions");
254 msg = new Performative("inform");
255 msg.setReceiver(agent);
256 msg.setContent("done");
257 msg.setInReplyTo(replyKey);
258 }
259 catch(IOException ioe ){
260 ioe.printStackTrace();
261 msg = new Performative("failure");
262 msg.setReceiver(agent);
263 msg.setContent("Error purging " + sessionType);
264 msg.setInReplyTo(replyKey);
265 }
266 proxy.sendMsg(msg);
267 }
268
269 public void openSession(String replyKey, String agent,
270 String sessionType, String sessionId, String accessKey){
271
272 Performative msg;
273 String fname;
274 if (ht.contains(accessKey)){
275 msg = new Performative("failure");
276 msg.setReceiver(agent);
277 msg.setInReplyTo(replyKey);
278 msg.setContent(sessionId + " already opened");
279 proxy.sendMsg(msg);
280 return;
281 }
282
283
284 try {
285 msg = new Performative("inform");
286 msg.setReceiver(agent);
287 msg.setInReplyTo(replyKey);
288
289 if (isAccessible())
290 fname =ROOT+sessionType+fsep+sessionId;
291 else
292 fname = ROOT+PREFIX+agent+fsep+sessionType+fsep+sessionId;
293
294 File file = new File(fname);
295
296 if (!file.exists())
297 throw new IOException(sessionId + " doesn't exist");
298 else {
299 RandomAccessFile fp = new RandomAccessFile(file,"rw");
300 ht.put(accessKey, fp);
301 msg.setContent("done");
302 }
303 }
304 catch(IOException ioe) {
305 ioe.printStackTrace();
306 msg = new Performative("failure");
307 msg.setReceiver(agent);
308 msg.setInReplyTo(replyKey);
309 msg.setContent(agent + " couldn't open " + sessionId);
310 }
311 proxy.sendMsg(msg);
312 }
313
314 public void saveRecord(String replyKey, String agent,
315 String accessKey, String record){
316
317 Performative msg;
318
319 try {
320 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
321 if (fp == null) throw new IOException("Error file pointer doesn't exist.");
322 fp.seek(fp.length());
323 if (fp.getFilePointer() != 0)
324 fp.writeBytes(NEWLINE);
325 fp.writeBytes(RECORD_TAG);
326 fp.writeBytes(NEWLINE);
327 fp.writeBytes(record);
328 msg = new Performative("inform");
329 msg.setReceiver(agent);
330 msg.setInReplyTo(replyKey);
331 msg.setContent("done");
332 }
333 catch(IOException e){
334 e.printStackTrace();
335 msg = new Performative("failure");
336 msg.setReceiver(agent);
337 msg.setInReplyTo(replyKey);
338 msg.setContent(agent + " couldn't save " + record);
339 }
340 proxy.sendMsg(msg);
341 }
342
343 public void closeSession(String replyKey,String agent,String accessKey){
344
345 Performative msg;
346
347 try {
348 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
349 if (fp == null)
350 throw new IOException("Error file pointer doesn't exist.");
351 fp.close();
352 ht.remove(accessKey);
353 msg = new Performative("inform");
354 msg.setReceiver(agent);
355 msg.setInReplyTo(replyKey);
356 msg.setContent("done");
357 }
358 catch(IOException e){
359 e.printStackTrace();
360 msg = new Performative("failure");
361 msg.setReceiver(agent);
362 msg.setInReplyTo(replyKey);
363 msg.setContent(agent + " couldn't close with accesskey:" + accessKey);
364 }
365 proxy.sendMsg(msg);
366
367 }
368
369 public void nextRecord(String replyKey,String agent,String accessKey){
370
371 Performative msg;
372 try {
373 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
374 if (fp == null)
375 throw new IOException("Error file pointer doesn't exist.");
376
377 String record = readRecord(fp);
378 msg = new Performative("inform");
379 msg.setReceiver(agent);
380 msg.setInReplyTo(replyKey);
381 msg.setContent(record);
382 }
383 catch(IOException e){
384 e.printStackTrace();
385 msg = new Performative("failure");
386 msg.setReceiver(agent);
387 msg.setInReplyTo(replyKey);
388 msg.setContent(agent + " couldn't read next record with accesskey:" + accessKey);
389 }
390 proxy.sendMsg(msg);
391 }
392
393 public void priorRecord(String replyKey,String agent,String accessKey){
394 int cnt;
395 String record = "";
396 Performative msg;
397 try {
398 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
399 if (fp == null)
400 throw new IOException("Error file pointer doesn't exist.");
401 cnt = getCountUpTo(fp,fp.getFilePointer());
402 fp.seek(0);
403 for (int i=0; i < cnt; i++)
404 record = readRecord(fp);
405 msg = new Performative("inform");
406 msg.setReceiver(agent);
407 msg.setInReplyTo(replyKey);
408 msg.setContent(record);
409
410
411 fp.seek(0);
412 for (int i=0; i < cnt-1; i++)
413 readRecord(fp);
414 }
415 catch(IOException e){
416 e.printStackTrace();
417 msg = new Performative("failure");
418 msg.setReceiver(agent);
419 msg.setInReplyTo(replyKey);
420 msg.setContent(agent + " couldn't locate prior record with accesskey:" + accessKey);
421 }
422 proxy.sendMsg(msg);
423 }
424
425 public void beginSession(String replyKey,String agent,String accessKey){
426
427 Performative msg;
428 try {
429 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
430 if (fp == null)
431 throw new IOException("Error file pointer doesn't exist.");
432
433 fp.seek(0);
434 msg = new Performative("inform");
435 msg.setReceiver(agent);
436 msg.setInReplyTo(replyKey);
437 msg.setContent("done");
438 }
439 catch(IOException e){
440 e.printStackTrace();
441 msg = new Performative("failure");
442 msg.setReceiver(agent);
443 msg.setInReplyTo(replyKey);
444 msg.setContent(agent + " can't move to first record with accesskey:" + accessKey);
445 }
446 proxy.sendMsg(msg);
447 }
448
449 public void endSession(String replyKey,String agent,String accessKey){
450 Performative msg;
451 try {
452 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
453 if (fp == null)
454 throw new IOException("Error file pointer doesn't exist.");
455 fp.seek(fp.length());
456 msg = new Performative("inform");
457 msg.setReceiver(agent);
458 msg.setInReplyTo(replyKey);
459 msg.setContent("done");
460 }
461 catch(IOException e){
462 e.printStackTrace();
463 msg = new Performative("failure");
464 msg.setReceiver(agent);
465 msg.setInReplyTo(replyKey);
466 msg.setContent(agent + " can't move to last record with accesskey:" + accessKey);
467 }
468 proxy.sendMsg(msg);
469 }
470
471 public void getAgents(String replyKey,String agent,String accessKey){
472 int cnt;
473 String record;
474 HSet resultSet = new HSet();
475 Performative perf;
476 Performative msg;
477 long currentPos;
478
479 try {
480 RandomAccessFile fp = (RandomAccessFile) ht.get(accessKey);
481 if (fp == null)
482 throw new IOException("Error file pointer doesn't exist.");
483 currentPos = fp.getFilePointer();
484 cnt = getCountUpTo(fp,fp.length());
485 fp.seek(0);
486 for(int i=0; i< cnt; i++) {
487 record = readRecord(fp);
488 perf = ZeusParser.performative(record);
489 resultSet.add(perf.getSender());
490 resultSet.add(perf.getReceiver());
491 }
492
493 msg = new Performative("inform");
494 msg.setReceiver(agent);
495 msg.setInReplyTo(replyKey);
496 msg.setContent(Misc.concat(resultSet));
497 fp.seek(currentPos);
498 }
499 catch(IOException e){
500 e.printStackTrace();
501 msg = new Performative("failure");
502 msg.setReceiver(agent);
503 msg.setInReplyTo(replyKey);
504 msg.setContent(agent + ": error lisitng agents with accesskey:" + accessKey);
505 }
506
507 proxy.sendMsg(msg);
508 }
509
510
511 private String readRecord(RandomAccessFile fp){
512 String input = "";
513 boolean begin=false;
514 try {
515 while (fp.getFilePointer() < fp.length()) {
516 String buf = fp.readLine();
517 if (buf.equals(RECORD_TAG)) {
518 begin = !begin;
519 if (!begin)
520 break;
521 else
522 continue;
523 }
524 if (begin)
525 input += buf;
526 }
527 fp.seek(fp.getFilePointer()-NEWRECORD.length());
528 }
529 catch(IOException e){
530 e.printStackTrace();
531 }
532 return input;
533 }
534
535
536
537 }