001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.events;
025:
026: import org.myoodb.objects.*;
027:
028: public class Client implements Runnable {
029: public static int PORT = 54321;
030: public static String USERNAME = "admin";
031: public static String PASSWORD = "admin";
032: public static Thread LISTEN_THREAD = null;
033:
034: public static org.myoodb.MyOodbDatabase THE_DB = null;
035:
036: public static void main(String args[]) throws Exception {
037: org.myoodb.MyOodbDatabase db = THE_DB = org.myoodb.MyOodbDatabase
038: .open("tcp://localhost:" + PORT, USERNAME, PASSWORD);
039:
040: org.myoodb.collectable.LinkedList persons = (org.myoodb.collectable.LinkedList) db
041: .getRoot("Persons");
042: if (persons == null) {
043: persons = (org.myoodb.collectable.LinkedList) db
044: .createRoot("Persons",
045: "org.myoodb.collectable.LinkedListDbImpl");
046: //persons = (org.myoodb.collectable.LinkedList) db.createRoot("Persons", org.myoodb.collectable.LinkedListDbImpl.class);
047: }
048:
049: Person person = (Person) db
050: .createObject("org.myoodb.objects.PersonDbImpl");
051: //Person person = (Person) db.createObject(PersonDbImpl.class);
052:
053: person.setName("John Smith");
054: persons.add(person);
055:
056: org.myoodb.event.EventLog eventLog = (org.myoodb.event.EventLog) db
057: .getRoot("EventLog");
058: if (eventLog == null) {
059: eventLog = (org.myoodb.event.EventLog) db.createRoot(
060: "EventLog", "org.myoodb.event.EventLogDbImpl");
061: //eventLog = (org.myoodb.event.EventLog) db.createRoot("EventLog", org.myoodb.event.EventLogDbImpl.class);
062: }
063:
064: LISTEN_THREAD = new Thread(new Client());
065: LISTEN_THREAD.setDaemon(true);
066: LISTEN_THREAD.start();
067:
068: for (int i = 0; i < 100; i++) {
069: MessageEvent messageEvent = (MessageEvent) db
070: .createObject("org.myoodb.objects.MessageEventDbImpl");
071: //MessageEvent messageEvent = (MessageEvent) db.createObject(MessageEventDbImpl.class);
072:
073: messageEvent.setMessage("Did you clean the basement?");
074: messageEvent.setLogStore(eventLog);
075: messageEvent.notify(person);
076:
077: Thread.sleep(1000);
078: }
079: }
080:
081: public void run() {
082: try {
083: org.myoodb.event.EventLog eventLog = (org.myoodb.event.EventLog) THE_DB
084: .getRoot("EventLog");
085:
086: long startTime = eventLog.getLocalTime();
087: while (LISTEN_THREAD != null) {
088: java.util.ArrayList events = eventLog.listenForEvents(
089: startTime, MessageEvent.class);
090: //java.util.ArrayList events = eventLog.listenForEvents(org.myoodb.event.Event.class); // listen for all events
091:
092: for (int i = 0; i < events.size(); i++) {
093: MessageEvent messageEvent = (MessageEvent) events
094: .get(i);
095:
096: Person person = (Person) THE_DB
097: .getObject(messageEvent
098: .getCollectableHandle());
099:
100: System.out.println("Message: " + messageEvent
101: + " To: " + person);
102:
103: startTime = messageEvent.getTime() + 1;
104: }
105: }
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110: }
|