OW2 Consortium
Search OW2 Mail Archive: 

Advanced Search - Powered by Google


Mail Archive Home | easybeans-commits List | June 2008 Index

<--  Date Index     <--  Thread Index    

[easybeans-commits] [3571] trunk/util/modules/event: Event service


Title: [3571] trunk/util/modules/event: Event service
Revision
3571
Author
missonng
Date
2008-06-30 18:29:28 +0200 (Mon, 30 Jun 2008)

Log Message

Event service

Modified Paths

Added Paths

Removed Paths

Diff

Modified: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/EventPriority.java (3570 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/EventPriority.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/EventPriority.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -25,7 +25,7 @@
 package org.ow2.util.event.api;
 
 /**
- * Define handler's priorities.<br>
+ * Define listener's priorities.<br>
  * Priorities natural order is define by the ordinal in this enumeration.<br>
  * The special value NONE_PRIORITY is defined to delimit synchronous priorities from asynchronous priorities.
  * It shouldn't be use, but will be treated as the highest asynchronous priority if you do so.

Modified: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEvent.java (3570 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEvent.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEvent.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -25,19 +25,10 @@
 package org.ow2.util.event.api;
 
 /**
- * Define the base interface for an event.
+ * Define the base interface for an event.<br>
+ * As listeners are executed in concurrency, behavior is not specify if writable event attributes are not thread safe.
  * @author missonng
  */
 public interface IEvent {
-    /**
-     * Get the event number.
-     * @return The event number.
-     */
-    public long getNumber();
 
-    /**
-     * Get the event time.
-     * @return The event time.
-     */
-    public long getTime();
 }

Modified: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventDispatcher.java (3570 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventDispatcher.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventDispatcher.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -25,33 +25,25 @@
 package org.ow2.util.event.api;
 
 /**
- * Define methods handlers need to be switch and schedule.
+ * Define methods needed to register listeners and dispatch events.<br>
+ * Before dereferencing the dispatcher, you have to set the size to 0, for the garbage collector to do his job.
+ * You have to be sure that no one can call the dispatcher after you set its size to 0, otherwise :
+ * <ul>
+ *   <li>Providers can block infinitely when dispatching events with synchronous, until you define a size greater than 0.</li>
+ *   <li>The asynchronous tasks queue may grow infinitely, until you define a size greater than 0.</li>
+ * </ul>
  * @author missonng
  */
 public interface IEventDispatcher {
     /**
-     * Start the dispatcher.
-     */
-    void start();
-
-    /**
-     * Stop the dispatcher.
-     */
-    void stop();
-
-    /**
-     * Tell if the dispatcher is available.
-     * @return True if the dispatcher is available, false otherwise.
-     */
-    boolean isAvailable();
-
-    /**
      * Get the event dispatcher number of workers.
+     * @return The event dispatcher number of workers.
      */
     int getSize();
 
     /**
      * Set the event dispatcher number of workers.
+     * @param size The event dispatcher number of workers.
      */
     void setSize(int size);
 
@@ -59,17 +51,17 @@
      * Add an event listener.
      * @param listener The event listener to add.
      */
-    void addEventListener(IEventListener listener);
+    void addListener(IEventListener listener);
 
     /**
      * Remove an event listener.
      * @param listener The event listener to remove.
      */
-    void removeEventListener(IEventListener listener);
+    void removeListener(IEventListener listener);
 
     /**
      * Dispatch this event.
      * @param event The event to dispatch.
      */
-    void dispatchEvent(final IEvent event);
+    void dispatch(final IEvent event);
 }

Modified: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventListener.java (3570 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventListener.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventListener.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -33,15 +33,20 @@
 public interface IEventListener {
     /**
      * Handle the event.
+     * @param event The event to handle.
      */
-    void handleEvent(IEvent event);
+    void handle(IEvent event);
 
+    /**
+     * Check whether the listener wants to handle this event.
+     * @param event The event to check.
+     * @return True if the listener wants to handle this event, false otherwise.
+     */
+    boolean accept(IEvent event);
 
-    boolean acceptEvent(IEvent event);
-
     /**
-     * Get the handler priority.
-     * @return The handler priority.
+     * Get the listener priority.
+     * @return The listener priority.
      */
     EventPriority getPriority();
 }

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventProvider.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventProvider.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventProvider.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,61 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api;
+
+/**
+ * Define the base interface for an event provider.
+ * @author missonng
+ */
+public interface IEventProvider {
+    /**
+     * Get the event provider id.
+     * @return The event provider id.
+     */
+    String getId();
+
+    /**
+     * Get the event dispatcher used by this event provider.
+     * @return The event dispatcher use by this event provider.
+     */
+    IEventDispatcher getDispatcher();
+
+    /**
+     * Set the event dispatcher used by this event provider.
+     * @param dispatcher The event dispatcher use by this event provider.
+     */
+    void setDispatcher(IEventDispatcher dispatcher);
+
+    /**
+     * Add an event listener.
+     * @param listener The event listener to add.
+     */
+    void addListener(IEventListener listener);
+
+    /**
+     * Remove an event listener.
+     * @param listener The event listener to remove.
+     */
+    void removeListener(IEventListener listener);
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventProvider.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventService.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventService.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventService.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,69 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api;
+
+import org.ow2.util.event.api.exception.EventFilterMalformed;
+import org.ow2.util.event.api.exception.EventIdAlreadyExists;
+import org.ow2.util.event.api.exception.EventIdMalformed;
+import org.ow2.util.event.api.exception.EventIdNotExists;
+
+/**
+ * Define a root event dispatcher, and manage a provider tree using a hierarchical naming convention.
+ * By default, created provider will use their parent event provider, but they can redefine their own one.
+ * Listener are registered in the service using a filter on provider ids.<br>
+ * The service use this filter to register the listener on all matching providers.
+ * @author missonng
+ */
+public interface IEventService {
+    /**
+     * Create and return a new event provider with the specified id.
+     * @param id The event provider id.
+     * @return The event provider.
+     * @throws EventIdMalformed If the id does not match the regexp : (/\w+)+
+     * @throws EventIdAlreadyExists If a provider with this id already exists.
+     */
+    IEventProvider createProvider(String id) throws EventIdMalformed, EventIdAlreadyExists;
+
+    /**
+     * Destroy an new event provider with the specified id.
+     * @param id The event provider id.
+     * @throws EventIdNotExists If no provider with this id exists.
+     */
+    void destroyProvider(String id) throws EventIdNotExists;
+
+    /**
+     * Register a listener on all provider matching the filter.
+     * @param listener The listener to register.
+     * @param filter The filter to apply.
+     * @throws EventFilterMalformed If the filter does not match the regexp : (/[\w\*]+)+
+     */
+    void registerListener(IEventListener listener, String filter) throws EventFilterMalformed;
+
+    /**
+     * Unregister an event listener.
+     * @param listener The event listener to unregister.
+     */
+    void unregisterListener(IEventListener listener);
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/IEventService.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventFilterMalformed.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventFilterMalformed.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventFilterMalformed.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,68 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api.exception;
+
+/**
+ * Exception thrown when a listener filter is malformed.
+ * @author missonng
+ */
+public class EventFilterMalformed extends Exception {
+    /**
+     * The EventFilterMalformed class serialVersionUID.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The EventFilterMalformed constructor.
+     */
+    public EventFilterMalformed() {
+        super();
+    }
+
+    /**
+     * The EventFilterMalformed constructor.
+     * @param message The exception message.
+     */
+    public EventFilterMalformed(final String message) {
+        super(message);
+    }
+
+    /**
+     * The EventFilterMalformed constructor.
+     * @param cause The exception cause.
+     */
+    public EventFilterMalformed(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * The EventFilterMalformed constructor.
+     * @param message The exception message.
+     * @param cause The exception cause.
+     */
+    public EventFilterMalformed(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventFilterMalformed.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdAlreadyExists.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdAlreadyExists.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdAlreadyExists.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,68 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api.exception;
+
+/**
+ * Exception thrown when trying to create a provider with an already existing id.
+ * @author missonng
+ */
+public class EventIdAlreadyExists extends Exception {
+    /**
+     * The EventIdAlreadyExists class serialVersionUID.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The EventIdAlreadyExists constructor.
+     */
+    public EventIdAlreadyExists() {
+        super();
+    }
+
+    /**
+     * The EventIdAlreadyExists constructor.
+     * @param message The exception message.
+     */
+    public EventIdAlreadyExists(final String message) {
+        super(message);
+    }
+
+    /**
+     * The EventIdAlreadyExists constructor.
+     * @param cause The exception cause.
+     */
+    public EventIdAlreadyExists(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * The EventIdAlreadyExists constructor.
+     * @param message The exception message.
+     * @param cause The exception cause.
+     */
+    public EventIdAlreadyExists(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdAlreadyExists.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdMalformed.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdMalformed.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdMalformed.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,68 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api.exception;
+
+/**
+ * Exception thrown when a provider id is malformed.
+ * @author missonng
+ */
+public class EventIdMalformed extends Exception {
+    /**
+     * The EventIdMalformed class serialVersionUID.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The EventIdMalformed constructor.
+     */
+    public EventIdMalformed() {
+        super();
+    }
+
+    /**
+     * The EventIdMalformed constructor.
+     * @param message The exception message.
+     */
+    public EventIdMalformed(final String message) {
+        super(message);
+    }
+
+    /**
+     * The EventIdMalformed constructor.
+     * @param cause The exception cause.
+     */
+    public EventIdMalformed(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * The EventIdMalformed constructor.
+     * @param message The exception message.
+     * @param cause The exception cause.
+     */
+    public EventIdMalformed(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdMalformed.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdNotExists.java (0 => 3571)


--- trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdNotExists.java	                        (rev 0)
+++ trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdNotExists.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,68 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.api.exception;
+
+/**
+ * Exception thrown when trying to destroy a provider with none existing id.
+ * @author missonng
+ */
+public class EventIdNotExists extends Exception {
+    /**
+     * The EventIdNotExists class serialVersionUID.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The EventIdNotExists constructor.
+     */
+    public EventIdNotExists() {
+        super();
+    }
+
+    /**
+     * The EventIdNotExists constructor.
+     * @param message The exception message.
+     */
+    public EventIdNotExists(final String message) {
+        super(message);
+    }
+
+    /**
+     * The EventIdNotExists constructor.
+     * @param cause The exception cause.
+     */
+    public EventIdNotExists(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * The EventIdNotExists constructor.
+     * @param message The exception message.
+     * @param cause The exception cause.
+     */
+    public EventIdNotExists(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+}
Property changes on: trunk/util/modules/event/api/src/main/java/org/ow2/util/event/api/exception/EventIdNotExists.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Deleted: trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/AbstractEvent.java (3570 => 3571)


--- trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/AbstractEvent.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/AbstractEvent.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -1,73 +0,0 @@
-/**
- * OW2 Util
- * Copyright (C) 2008 Bull S.A.S.
- * Contact: easybeans@xxxxxxx
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
- *
- * --------------------------------------------------------------------------
- * $Id$
- * --------------------------------------------------------------------------
- */
-
-package org.ow2.util.event.impl;
-
-import org.ow2.util.event.api.IEvent;
-
-/**
- * A generic abstract class for events.
- * @author missonng
- */
-public abstract class AbstractEvent implements IEvent {
-    /**
-     * A static field to number events.
-     */
-    private static long nextNumber = 0;
-
-    /**
-     * The event's number.
-     */
-    private long number;
-
-    /**
-     * The event's time.
-     */
-    private long time;
-
-    /**
-     * The event's Constructor.
-     */
-    public AbstractEvent() {
-        this.number = AbstractEvent.nextNumber++;
-        this.time = System.currentTimeMillis();
-    }
-
-    /**
-     * Get the event's number.
-     * @return The event's number.
-     */
-    public long getNumber() {
-        return this.number;
-    }
-
-    /**
-     * Get the event's time.
-     * @return The event's time.
-     */
-    public long getTime() {
-        return this.time;
-    }
-
-}

Modified: trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventDispatcher.java (3570 => 3571)


--- trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventDispatcher.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventDispatcher.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -24,6 +24,7 @@
 
 package org.ow2.util.event.impl;
 
+import java.util.HashSet;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.Semaphore;
@@ -35,7 +36,13 @@
 import org.ow2.util.event.api.IEventListener;
 
 /**
- * A generic service to dispatch events.
+ * Define methods needed to register listeners and dispatch events.<br>
+ * Before dereferencing the dispatcher, you have to set the size to 0, for the garbage collector to do his job.
+ * You have to be sure that no one can call the dispatcher after you set its size to 0, otherwise :
+ * <ul>
+ *   <li>Providers can block infinitely when dispatching events with synchronous, until you define a size greater than 0.</li>
+ *   <li>The asynchronous tasks queue may grow infinitely, until you define a size greater than 0.</li>
+ * </ul>
  * @author missonng
  */
 public class EventDispatcher implements IEventDispatcher {
@@ -60,7 +67,6 @@
                     try {
                         task.run();
                     } catch (Exception ex) {
-                        //nothing to do.
                         continue;
                     }
                 } else {
@@ -118,7 +124,6 @@
                     }
                     return synchronousQueue.take();
                 } catch (InterruptedException ex) {
-                    //nothing to do.
                     continue;
                 }
             }
@@ -126,11 +131,6 @@
     }
 
     /**
-     * The default number of workers.
-     */
-    private static final int DEFAULT_SIZE = 1;
-
-    /**
      * The ordinal of the NONE_PRIORITY value.
      */
     private static final int NONE_INDEX = EventPriority.NONE_PRIORITY.ordinal();
@@ -146,7 +146,7 @@
 
     /**
      * The listener set.<br>
-     * Use a copy-on-write set allow iteration on the set with neither synchronization, nor ConcurrentModificationException.
+     * Using a copy-on-write set allow iteration on the set with neither synchronization, nor ConcurrentModificationException.
      */
     private final CopyOnWriteArrayList<IEventListener> listeners = new CopyOnWriteArrayList<IEventListener>();
 
@@ -157,44 +157,11 @@
     private final EventQueue queue = new EventQueue();
 
     /**
-     * Tell if the dispatcher is available.
-     */
-    private boolean available = false;
-
-    /**
      * The number of workers.
      */
-    private int size = DEFAULT_SIZE;
+    private int size = 0;
 
     /**
-     * Start the dispatcher.
-     */
-    public synchronized void start() {
-        if (!this.available) {
-            this.available = true;
-            createWorkers(this.size);
-        }
-    }
-
-    /**
-     * Stop the dispatcher.
-     */
-    public synchronized void stop() {
-        if (this.available) {
-            this.available = false;
-            destroyWorkers(this.size);
-        }
-    }
-
-    /**
-     * Tell if the dispatcher is available.
-     * @return True if the dispatcher is available, false otherwise.
-     */
-    public synchronized boolean isAvailable() {
-        return this.available;
-    }
-
-    /**
      * Get the number of workers.
      * @return The number of workers.
      */
@@ -207,44 +174,20 @@
      * @param size The number of workers.
      */
     public synchronized void setSize(final int size) {
-        if (this.available) {
-            final int diff = this.size - size;
-            if (diff > 0) {
-                destroyWorkers(diff);
-            } else {
-                createWorkers(-diff);
-            }
+        final int diff = this.size - size;
+        if (diff > 0) {
+            destroyWorkers(diff);
+        } else {
+            createWorkers(-diff);
         }
         this.size = size;
     }
 
     /**
-     * A helper method to create workers.
-     * @param nb The number of workers to create.
-     */
-    private void createWorkers(final int nb) {
-        for (int i = 0; i < nb; i++) {
-            new EventWorker().start();
-        }
-    }
-
-    /**
-     * A helper method to destroy workers.
-     * @param nb The number of workers to destroy.
-     */
-    private void destroyWorkers(final int nb) {
-        final Runnable kill = EventDispatcher.KILL;
-        final EventQueue queue = this.queue;
-        for (int i = 0; i < nb; i++) {
-            queue.put(kill);
-        }
-    }
-
-    /**
      * Add a listener.
      * @param listener The listener to add.
      */
-    public void addEventListener(final IEventListener listener) {
+    public void addListener(final IEventListener listener) {
         this.listeners.add(listener);
     }
 
@@ -252,20 +195,16 @@
      * Remove a listener.
      * @param listener The listener to remove.
      */
-    public void removeEventListener(final IEventListener listener) {
+    public void removeListener(final IEventListener listener) {
         this.listeners.remove(listener);
     }
 
     /**
-     * Dispatch this event.
+     * Dispatch an event.
      * @param event The event to dispatch.
      */
     @SuppressWarnings("unchecked")
-    public void dispatchEvent(final IEvent event) {
-        if (!this.available) {
-            throw new NullPointerException("The dispatcher is not available");
-        }
-
+    public void dispatch(final IEvent event) {
         final Semaphore semaphore = new Semaphore(0, true);
         final ConcurrentLinkedQueue<Runnable>[] runnableMap =
             new ConcurrentLinkedQueue[EventPriority.values().length];
@@ -274,14 +213,17 @@
             runnableMap[i] = new ConcurrentLinkedQueue<Runnable>();
         }
 
-        for (final IEventListener listener : this.listeners) {
-            if (listener.acceptEvent(event)) {
+        HashSet<IEventListener> listeners = new HashSet<IEventListener>();
+        listeners.addAll(this.listeners);
+
+        for (final IEventListener listener : listeners) {
+            if (listener.accept(event)) {
                 int index = listener.getPriority().ordinal();
                 if (index < NONE_INDEX) {
                     runnableMap[index].add(new Runnable() {
                         public void run() {
                             try {
-                                listener.handleEvent(event);
+                                listener.handle(event);
                             } finally {
                                 semaphore.release();
                             }
@@ -290,7 +232,7 @@
                 } else {
                     runnableMap[index].add(new Runnable() {
                         public void run() {
-                            listener.handleEvent(event);
+                            listener.handle(event);
                         }
                     });
                 }
@@ -354,4 +296,26 @@
             }
         }
     }
+
+    /**
+     * A helper method to create workers.
+     * @param nb The number of workers to create.
+     */
+    private void createWorkers(final int nb) {
+        for (int i = 0; i < nb; i++) {
+            new EventWorker().start();
+        }
+    }
+
+    /**
+     * A helper method to destroy workers.
+     * @param nb The number of workers to destroy.
+     */
+    private void destroyWorkers(final int nb) {
+        final Runnable kill = EventDispatcher.KILL;
+        final EventQueue queue = this.queue;
+        for (int i = 0; i < nb; i++) {
+            queue.put(kill);
+        }
+    }
 }

Added: trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventService.java (0 => 3571)


--- trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventService.java	                        (rev 0)
+++ trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventService.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,330 @@
+/**
+ * OW2 Util
+ * Copyright (C) 2008 Bull S.A.S.
+ * Contact: easybeans@xxxxxxx
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ *
+ * --------------------------------------------------------------------------
+ * $Id$
+ * --------------------------------------------------------------------------
+ */
+
+package org.ow2.util.event.impl;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.ow2.util.event.api.IEventDispatcher;
+import org.ow2.util.event.api.IEventListener;
+import org.ow2.util.event.api.IEventProvider;
+import org.ow2.util.event.api.IEventService;
+import org.ow2.util.event.api.exception.EventFilterMalformed;
+import org.ow2.util.event.api.exception.EventIdAlreadyExists;
+import org.ow2.util.event.api.exception.EventIdMalformed;
+import org.ow2.util.event.api.exception.EventIdNotExists;
+
+/**
+ * Define a root event dispatcher, and manage a provider tree using a hierarchical naming convention.
+ * By default, created provider will use their parent event provider, but they can redefine their own one.
+ * Listener are registered in the service using a filter on provider ids.<br>
+ * The service use this filter to register the listener on all matching providers.
+ * @author missonng
+ */
+public class EventService implements IEventService {
+    /**
+     * An inner implementation of IEventProvider.
+     * @author guillaume
+     */
+    private final class EventProvider implements IEventProvider {
+        /**
+         * The event provider id.
+         */
+        private String id;
+
+        /**
+         * The event provider dispatcher.
+         */
+        private IEventDispatcher dispatcher;
+
+        /**
+         * The event provider listeners.
+         */
+        private List<IEventListener> listeners;
+
+        /**
+         * The event provider parent.
+         */
+        private EventProvider parent;
+
+        /**
+         * The event provider childs.
+         */
+        private Map<String, EventProvider> childs;
+
+        /**
+         * The root event provider constructor.
+         */
+        private EventProvider() {
+            synchronized (EventService.this) {
+                this.id = "/";
+                this.dispatcher = new EventDispatcher();
+                this.listeners = new LinkedList<IEventListener>();
+                this.parent = null;
+                this.childs = new HashMap<String, EventProvider>();
+                this.dispatcher.setSize(1);
+            }
+        }
+
+        /**
+         * The event provider constructor.
+         * @param id The event provider id.
+         * @throws EventIdMalformed If the id does not match the regexp : (/\w+)+
+         * @throws EventIdAlreadyExists If a provider with this id already exists.
+         */
+        private EventProvider(final String id) throws EventIdMalformed, EventIdAlreadyExists {
+            synchronized (EventService.this) {
+                if (!id.matches("(/\\w+)+")) {
+                    throw new EventIdMalformed();
+                }
+
+                String parentId = id.substring(0, id.lastIndexOf("/"));
+                EventProvider parent = (parentId.equals("") ? rootProvider : (EventProvider) providers.get(parentId));
+
+                this.id = id;
+                this.parent = parent == null ? new EventProvider(parentId) : parent;
+                this.childs = new HashMap<String, EventProvider>();
+                this.dispatcher = this.parent.dispatcher;
+                this.listeners = new LinkedList<IEventListener>();
+                this.parent.childs.put(id, this);
+
+                for (Entry<IEventListener, String> entry : EventService.this.filters.entrySet()) {
+                    if (this.id.matches(entry.getValue())) {
+                        addListener(entry.getKey());
+                    }
+                }
+            }
+        }
+
+        /**
+         * Destroy the event provider.
+         */
+        private void destroy() {
+            synchronized (EventService.this) {
+                for (IEventListener listener : this.listeners) {
+                    this.dispatcher.removeListener(listener);
+                }
+
+                for (EventProvider child : this.childs.values()) {
+                    child.destroy();
+                }
+
+                this.parent.childs.remove(id);
+                this.parent = null;
+            }
+        }
+
+        /**
+         * Get the event provider id.
+         * @return The event provider id.
+         */
+        public String getId() {
+            synchronized (EventService.this) {
+                return id;
+            }
+        }
+
+        /**
+         * Add an event listener.
+         * @param listener The event listener to add.
+         */
+        public void addListener(final IEventListener listener) {
+            synchronized (EventService.this) {
+                this.listeners.add(listener);
+                this.dispatcher.addListener(listener);
+            }
+        }
+
+        /**
+         * Remove an event listener.
+         * @param listener The event listener to remove.
+         */
+        public void removeListener(final IEventListener listener) {
+            synchronized (EventService.this) {
+                this.listeners.remove(listener);
+                this.dispatcher.removeListener(listener);
+            }
+        }
+
+        /**
+         * Get the event dispatcher used by this event provider.
+         * @return The event dispatcher use by this event provider.
+         */
+        public IEventDispatcher getDispatcher() {
+            synchronized (EventService.this) {
+                return this.dispatcher;
+            }
+        }
+
+        /**
+         * Set the event dispatcher used by this event provider.
+         * @param dispatcher The event dispatcher use by this event provider.
+         */
+        public void setDispatcher(final IEventDispatcher dispatcher) {
+            synchronized (EventService.this) {
+                IEventDispatcher oldDispatcher = this.dispatcher;
+                this.dispatcher = null;
+                IEventDispatcher newDispatcher = dispatcher == null ? this.parent.dispatcher : dispatcher;
+                this.dispatcher = newDispatcher;
+
+                for (IEventListener listener : listeners) {
+                    newDispatcher.addListener(listener);
+                    oldDispatcher.removeListener(listener);
+                }
+
+                for (EventProvider child : this.childs.values()) {
+                    if (child.dispatcher == oldDispatcher) {
+                        child.setDispatcher(newDispatcher);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * The root event provider.
+     */
+    private EventProvider rootProvider;
+
+    /**
+     * The root event dispatcher.
+     */
+    private IEventDispatcher rootDispatcher;
+
+    /**
+     * The service managed event providers.
+     */
+    private HashMap<String, EventProvider> providers;
+
+    /**
+     * The service managed event listeners.
+     */
+    private HashMap<IEventListener, String> filters;
+
+    /**
+     * Create and return a new event provider with the specified id.
+     * @param id The event provider id.
+     * @return The event provider.
+     * @throws EventIdMalformed If the id does not match the regexp : (/\w+)+
+     * @throws EventIdAlreadyExists If a provider with this id already exists.
+     */
+    public synchronized IEventProvider createProvider(final String id) throws EventIdMalformed, EventIdAlreadyExists {
+        if (this.providers.containsKey(id)) {
+            throw new EventIdAlreadyExists();
+        } else {
+            EventProvider provider = new EventProvider(id);
+            this.providers.put(id, provider);
+            return provider;
+        }
+    }
+
+    /**
+     * Destroy an new event provider with the specified id.
+     * @param id The event provider id.
+     * @throws EventIdNotExists If no provider with this id exists.
+     */
+    public synchronized void destroyProvider(final String id) throws EventIdNotExists {
+        EventProvider provider = this.providers.remove(id);
+        if (provider != null) {
+            while (!this.providers.containsKey(provider.parent.id)) {
+                provider = provider.parent;
+            }
+
+            provider.destroy();
+        } else {
+            throw new EventIdNotExists();
+        }
+    }
+
+    /**
+     * Register a listener on all provider matching the filter.
+     * @param listener The listener to register.
+     * @param filter The filter to apply.
+     * @throws EventFilterMalformed If the filter does not match the regexp : (/[\w\*]+)+
+     */
+    public synchronized void registerListener(final IEventListener listener, final String filter) throws EventFilterMalformed {
+        if (!filter.matches("(/[\\w\\*]+)+")) {
+            throw new EventFilterMalformed();
+        }
+
+        String newregexp = filter.replaceAll("\\*\\*", "[/\\\\w]#").replaceAll("\\*", "\\\\w#").replaceAll("#", "*");
+        String oldregexp = filters.put(listener, newregexp);
+
+        for (Entry<String, EventProvider> entry : this.providers.entrySet()) {
+            if (oldregexp != null && entry.getKey().matches(oldregexp)) {
+                entry.getValue().removeListener(listener);
+            }
+
+            if (entry.getKey().matches(newregexp)) {
+                entry.getValue().addListener(listener);
+            }
+        }
+    }
+
+    /**
+     * Unregister an event listener.
+     * @param listener The event listener to unregister.
+     */
+    public synchronized void unregisterListener(final IEventListener listener) {
+        String oldregexp = filters.remove(listener);
+        if (oldregexp != null) {
+            for (Entry<String, EventProvider> entry : this.providers.entrySet()) {
+                if (entry.getKey().matches(oldregexp)) {
+                    entry.getValue().removeListener(listener);
+                }
+            }
+        }
+    }
+
+    /**
+     * Start the service.
+     */
+    public synchronized void start() {
+        rootDispatcher = new EventDispatcher();
+        rootDispatcher.setSize(1);
+        rootProvider = new EventProvider();
+        rootProvider.setDispatcher(rootDispatcher);
+        providers = new HashMap<String, EventProvider>();
+        providers.put("/", rootProvider);
+        filters = new HashMap<IEventListener, String>();
+    }
+
+    /**
+     * Stop the service.
+     */
+    public synchronized void stop() {
+        filters.clear();
+        filters = null;
+        providers.clear();
+        providers = null;
+        rootProvider.destroy();
+        rootProvider = null;
+        rootDispatcher.setSize(0);
+        rootDispatcher = null;
+    }
+}
\ No newline at end of file
Property changes on: trunk/util/modules/event/impl/src/main/java/org/ow2/util/event/impl/EventService.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Added: trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventDispatcherTest.java (0 => 3571)


--- trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventDispatcherTest.java	                        (rev 0)
+++ trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventDispatcherTest.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -0,0 +1,194 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.ow2.util.event.impl.test;
+
+import java.util.ArrayList;
+import java.util.concurrent.Semaphore;
+
+import org.ow2.util.event.api.EventPriority;
+import org.ow2.util.event.api.IEvent;
+import org.ow2.util.event.api.IEventListener;
+import org.ow2.util.event.impl.EventDispatcher;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author guillaume
+ */
+public class EventDispatcherTest {
+    private static long next_event_number = 1;
+
+    private class Event implements IEvent {
+        private Thread thr;
+        private long tbgn;
+        private long tend;
+        private long tt;
+
+        public Event(final long tt) {;
+            this.tt = tt;
+            this.thr = new Thread() {
+                @Override
+                public void run() {
+                    Event.this.tbgn = System.nanoTime();
+                    EventDispatcherTest.this.dispatcher.dispatch(Event.this);
+                    Event.this.tend = System.nanoTime();
+                    EventDispatcherTest.this.semaphore.release();
+                }
+            };
+        }
+
+        public Thread getThread() {
+            return this.thr;
+        }
+
+        public long getTimeBegin() {
+            return this.tbgn;
+        }
+
+        public long getTimeEnd() {
+            return this.tend;
+        }
+
+        public long getTimeProcess() {
+            return this.tt;
+        }
+
+        public String getProviderId() {
+            return "";
+        }
+
+        public long getNumber() {
+            return 0;
+        }
+
+        public long getTime() {
+            return 0;
+        }
+    }
+
+    private class EventListener implements IEventListener {
+        private EventPriority priority;
+
+        public EventListener(final EventPriority priority) {
+            this.priority = priority;
+        }
+
+        public EventPriority getPriority() {
+            return this.priority;
+        }
+
+        public boolean accept(final IEvent event) {
+            return true;
+        }
+
+        public void handle(final IEvent event) {
+            final long t = System.nanoTime();
+            final long tt = ((Event)event).getTimeProcess();
+            while (System.nanoTime() - t < tt) {
+
+            };
+        }
+    }
+
+    private EventDispatcher dispatcher;
+    private ArrayList<Event> events;
+    private Semaphore semaphore;
+
+    @Test(timeOut=10000)
+    public void bench() {
+        //nb event the test will dispatch
+        final long[] nbes = {1};//{10000};
+
+        //nb listener
+        final long[] nbls = {10};//{10, 20, 40};
+
+        //events period in nanosecond
+        final long[] dts = {1000000};//{4000000, 2000000, 1000000};
+
+        //synchronous listener rate (% * nb listener)
+        final double[] ratesls = {.4};//{.1, .2, .4};
+
+        //listeners processing time (% * events period)
+        final double[] ratetts = {.01};//{.01, .05, .10, .50};
+
+        //weird event listeners processing time (% * events period)
+        final double[] ratethtts = {100.0};//{10.0, 50.0, 100.0, 500.0};
+
+        //weird event rate (% * nb event)
+        final double[] ratenbhtts = {.001};//{.001, .005, .01, .05};
+
+        for (final long nbe : nbes) {
+            for (final long nbl : nbls) {
+                for (final long dt : dts) {
+                    for (final double ratesl : ratesls) {
+                        for (final double ratett : ratetts) {
+                            for (final double ratethtt : ratethtts) {
+                                for (final double ratenbhtt : ratenbhtts) {
+                                    final long tt = (long)(ratett * dt);
+                                    final long htt = (long)(ratethtt * dt);
+                                    this.dispatcher = new EventDispatcher();
+                                    this.semaphore = new Semaphore(0, true);
+                                    this.events = new ArrayList<Event>();
+
+                                    this.dispatcher.setSize(100);
+
+                                    for (long i=0;i<nbl;i++) {
+                                        this.dispatcher.addListener(new EventListener(((double)i / nbl < ratesl) ? EventPriority.SYNC_NORM_PRIORITY : EventPriority.ASYNC_NORM_PRIORITY));
+                                    }
+
+                                    for (long i=0;i<nbe;i++) {
+                                        this.events.add(new Event((i%(1 / ratenbhtt) == 0) ? htt : tt));
+                                    }
+
+                                    for (final Event event : this.events) {
+                                        final long t = System.nanoTime();
+                                        event.getThread().start();
+                                        while (System.nanoTime() - t < dt) {
+
+                                        }
+                                    }
+                                    this.semaphore.acquireUninterruptibly((int)nbe);
+                                    this.dispatcher.setSize(0);
+
+                                    //long meantt = 0;
+                                    //long meanhtt = 0;
+                                    //long nbtt = 0;
+                                    //long nbhtt = 0;
+                                    //for (final Event event : this.events) {
+                                    //    if (event.getTimeProcess() == tt) {
+                                    //        nbtt++;
+                                    //        meantt += event.getTimeEnd() - event.getTimeBegin();
+                                    //    } else {
+                                    //        nbhtt++;
+                                    //        meanhtt += event.getTimeEnd() - event.getTimeBegin();
+                                    //    }
+                                    //}
+
+                                    //String str = nbe + ";" +
+                                    //            nbl + ";" +
+                                    //            dt + ";" +
+                                    //            ratesl + ";" +
+                                    //            ratett + ";" +
+                                    //            ratethtt + ";" +
+                                    //            ratenbhtt + ";" +
+                                    //            (meantt/nbtt) + ";" +
+                                    //            (meanhtt/nbhtt) + ";" +
+                                    //            ((meantt + meanhtt) / (nbtt + nbhtt)) + "\n";
+                                    //System.out.println(str);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        //System.out.println("Finished successfully!!!!");
+    }
+
+    public static void main(final String[] args) {
+        new EventDispatcherTest().bench();
+    }
+}
Property changes on: trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventDispatcherTest.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:mime-type
   + text/plain

Deleted: trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventTest.java (3570 => 3571)


--- trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventTest.java	2008-06-30 15:24:54 UTC (rev 3570)
+++ trunk/util/modules/event/impl/src/test/java/org/ow2/util/event/impl/test/EventTest.java	2008-06-30 16:29:28 UTC (rev 3571)
@@ -1,182 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-
-package org.ow2.util.event.impl.test;
-
-import java.util.ArrayList;
-import java.util.concurrent.Semaphore;
-
-import org.ow2.util.event.api.EventPriority;
-import org.ow2.util.event.api.IEvent;
-import org.ow2.util.event.api.IEventListener;
-import org.ow2.util.event.impl.AbstractEvent;
-import org.ow2.util.event.impl.EventDispatcher;
-import org.testng.annotations.Test;
-
-/**
- *
- * @author guillaume
- */
-public class EventTest {
-    private class Event extends AbstractEvent {
-        private Thread thr;
-        private long tbgn;
-        private long tend;
-        private long tt;
-
-        public Event(final long tt) {
-            this.tt = tt;
-            this.thr = new Thread() {
-                @Override
-                public void run() {
-                    Event.this.tbgn = System.nanoTime();
-                    EventTest.this.dispatcher.dispatchEvent(Event.this);
-                    Event.this.tend = System.nanoTime();
-                    EventTest.this.semaphore.release();
-                }
-            };
-        }
-
-        public Thread getThread() {
-            return this.thr;
-        }
-
-        public long getTimeBegin() {
-            return this.tbgn;
-        }
-
-        public long getTimeEnd() {
-            return this.tend;
-        }
-
-        public long getTimeProcess() {
-            return this.tt;
-        }
-    }
-
-    private class EventListener implements IEventListener {
-        private EventPriority priority;
-
-        public EventListener(final EventPriority priority) {
-            this.priority = priority;
-        }
-
-        public EventPriority getPriority() {
-            return this.priority;
-        }
-
-        public boolean acceptEvent(final IEvent event) {
-            return true;
-        }
-
-        public void handleEvent(final IEvent event) {
-            final long t = System.nanoTime();
-            final long tt = ((Event)event).getTimeProcess();
-            while (System.nanoTime() - t < tt) {
-
-            };
-        }
-    }
-
-    private EventDispatcher dispatcher;
-    private ArrayList<Event> events;
-    private Semaphore semaphore;
-
-    @Test(timeOut=10000)
-    public void test() {
-        //nb event the test will dispatch
-        final long[] nbes = {1};//{10000};
-
-        //nb listener
-        final long[] nbls = {10};//{10, 20, 40};
-
-        //events period in nanosecond
-        final long[] dts = {1000000};//{4000000, 2000000, 1000000};
-
-        //synchronous listener rate (% * nb listener)
-        final double[] ratesls = {.4};//{.1, .2, .4};
-
-        //listeners processing time (% * events period)
-        final double[] ratetts = {.01};//{.01, .05, .10, .50};
-
-        //weird event listeners processing time (% * events period)
-        final double[] ratethtts = {100.0};//{10.0, 50.0, 100.0, 500.0};
-
-        //weird event rate (% * nb event)
-        final double[] ratenbhtts = {.001};//{.001, .005, .01, .05};
-
-        for (final long nbe : nbes) {
-            for (final long nbl : nbls) {
-                for (final long dt : dts) {
-                    for (final double ratesl : ratesls) {
-                        for (final double ratett : ratetts) {
-                            for (final double ratethtt : ratethtts) {
-                                for (final double ratenbhtt : ratenbhtts) {
-                                    final long tt = (long)(ratett * dt);
-                                    final long htt = (long)(ratethtt * dt);
-                                    this.dispatcher = new EventDispatcher();
-                                    this.semaphore = new Semaphore(0, true);
-                                    this.events = new ArrayList<Event>();
-
-                                    this.dispatcher.start();
-                                    this.dispatcher.setSize(100);
-
-                                    for (long i=0;i<nbl;i++) {
-                                        this.dispatcher.addEventListener(new EventListener(((double)i / nbl < ratesl) ? EventPriority.SYNC_NORM_PRIORITY : EventPriority.ASYNC_NORM_PRIORITY));
-                                    }
-
-                                    for (long i=0;i<nbe;i++) {
-                                        this.events.add(new Event((i%(1 / ratenbhtt) == 0) ? htt : tt));
-                                    }
-
-                                    for (final Event event : this.events) {
-                                        final long t = System.nanoTime();
-                                        event.getThread().start();
-                                        while (System.nanoTime() - t < dt) {
-
-                                        }
-                                    }
-                                    this.semaphore.acquireUninterruptibly((int)nbe);
-                                    this.dispatcher.stop();
-
-                                    //long meantt = 0;
-                                    //long meanhtt = 0;
-                                    //long nbtt = 0;
-                                    //long nbhtt = 0;
-                                    //for (final Event event : this.events) {
-                                    //    if (event.getTimeProcess() == tt) {
-                                    //        nbtt++;
-                                    //        meantt += event.getTimeEnd() - event.getTimeBegin();
-                                    //    } else {
-                                    //        nbhtt++;
-                                    //        meanhtt += event.getTimeEnd() - event.getTimeBegin();
-                                    //    }
-                                    //}
-
-                                    //String str = nbe + ";" +
-                                    //            nbl + ";" +
-                                    //            dt + ";" +
-                                    //            ratesl + ";" +
-                                    //            ratett + ";" +
-                                    //            ratethtt + ";" +
-                                    //            ratenbhtt + ";" +
-                                    //            (meantt/nbtt) + ";" +
-                                    //            (meanhtt/nbhtt) + ";" +
-                                    //            ((meantt + meanhtt) / (nbtt + nbhtt)) + "\n";
-                                    //System.out.println(str);
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        //System.out.println("Finished successfully!!!!");
-    }
-
-    public static void main(final String[] args) {
-        new EventTest().test();
-    }
-}


<--  Date Index     <--  Thread Index    

Reply via email to:

Powered by MHonArc.

Copyright © 2006-2007, OW2 Consortium | contact | webmaster.