Performacne improvements via adding a netty-based openflowj and openflow plugin;...
[controller.git] / third-party / openflowj_netty / src / main / java / org / openflow / util / ProducerConsumer.java
1 package org.openflow.util;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Map;
6 import java.util.Set;
7
8 /**
9  * The following implement a producer/consumer design pattern in which both
10  * producers and consumers explicitly employ a centralized registration
11  * mechanism, and java Interfaces are used as contracts.<br>
12  */
13 public class ProducerConsumer {
14
15     /*
16      * Class variables
17      */
18     protected static ProducerConsumer singleton;
19
20     /*
21      * Default constructor
22      */
23     protected ProducerConsumer() {
24         producerMap = new Hashtable<Class<?>, Set<IProducer>>();
25     }
26
27     /*
28      * Instance variables
29      */
30
31     // Interface/IProducer map
32     protected Map<Class<?>, Set<IProducer>> producerMap;
33
34     /*
35      * Protected methods
36      */
37
38     protected void _registerConsumer(Object consumer, Class<?>[] interfaces,
39                                      Set<Class<?>> iSet,
40                                      Set<Class<?>> iUniqueSet) {
41         // *...Process all interfaces...*/
42         for (Class<?> iface : interfaces) {
43
44             // *...Protect against repeated interfaces...*/
45             if (!iUniqueSet.contains(iface)) {
46                 iUniqueSet.add(iface);
47
48                 Set<IProducer> producers = producerMap.get(iface);
49
50                 if (producers != null) {
51                     for (IProducer producer : producers)
52                         producer.registerConsumer(iface, consumer);
53                     iSet.add(iface);
54                 }
55
56                 // *...Recurse...*/
57                 _registerConsumer(consumer, iface.getInterfaces(), iSet,
58                                   iUniqueSet);
59             }
60         }
61     }
62
63     protected void _registerConsumer(Object consumer, Class<?> clazz,
64                                      Set<Class<?>> iSet,
65                                      Set<Class<?>> iUniqueSet) {
66         if (clazz != null) {
67             // *...Process all interfaces...*/
68             _registerConsumer(consumer, clazz.getInterfaces(), iSet,
69                               iUniqueSet);
70
71             // *...Recurse the class hierarchy...*/
72             _registerConsumer(consumer, clazz.getSuperclass(), iSet,
73                               iUniqueSet);
74         }
75     }
76
77     protected int _deregisterConsumer(Object consumer,
78                                       Class<?>[] interfaces,
79                                       Set<Class<?>> iUniqueSet) {
80         int count = 0;
81
82         // *...Process all interfaces...*/
83         for (Class<?> iface : interfaces) {
84
85             // *...Protect against repeated interfaces...*/
86             if (!iUniqueSet.contains(iface)) {
87                 iUniqueSet.add(iface);
88
89                 Set<IProducer> producers = producerMap.get(iface);
90
91                 if (producers != null) {
92                     for (IProducer producer : producers)
93                         producer.deregisterConsumer(iface, consumer);
94
95                     count++;
96                 }
97
98                 // *...Recurse...*/
99                 count += _deregisterConsumer(consumer,
100                                              iface.getInterfaces(),
101                                              iUniqueSet);
102             }
103         }
104
105         return count;
106     }
107
108     protected int _deregisterConsumer(Object consumer, Class<?> clazz,
109                                       Set<Class<?>> iUniqueSet) {
110         int count = 0;
111
112         if (clazz != null) {
113             // *...Process all interfaces...*/
114             count += _deregisterConsumer(consumer, clazz.getInterfaces(),
115                                          iUniqueSet);
116
117             // *...Recurse the class hierarchy...*/
118             count += _deregisterConsumer(consumer, clazz.getSuperclass(),
119                                          iUniqueSet);
120         }
121
122         return count;
123     }
124
125     /*
126      * Singleton API
127      */
128
129     /**
130      * @return singleton ProducerConsumer
131      */
132     public static synchronized ProducerConsumer getSingleton() {
133         if (singleton == null) singleton = new ProducerConsumer();
134
135         return singleton;
136     }
137
138     /*
139      * Producer APIs
140      */
141
142     /**
143      * Producer registration
144      * 
145      * @param producer
146      *            object that implements IProducer
147      * @param iface
148      *            interface supported by the producer
149      * @return whether there was a previously registered producer, or true if
150      *         one or more the arguments were invalid
151      */
152     public boolean registerProducer(IProducer producer, Class<?> iface) {
153         if (producer != null && iface != null && iface.isInterface()) {
154             Set<IProducer> producers = producerMap.get(iface);
155
156             if (producers == null) {
157                 producers = new HashSet<IProducer>();
158                 producerMap.put(iface, producers);
159             }
160
161             return producers.add(producer);
162         } else
163             return true;
164     }
165
166     /**
167      * Producer deregistration
168      * 
169      * @param producer
170      *            object that implements IProducer
171      * @param iface
172      *            interface supported by the producer
173      * @return whether the interface/producer pair was removed, or false if one
174      *         or more the arguments were invalid
175      */
176     public boolean deregisterProducer(IProducer producer, Class<?> iface) {
177         if (producer != null && iface != null && iface.isInterface()) {
178             Set<IProducer> producers = producerMap.get(iface);
179
180             if (producers != null) return producers.remove(producer);
181         }
182
183         return false;
184     }
185
186     /*
187      * Consumer APIs
188      */
189
190     /**
191      * Consumer registration
192      * 
193      * @param consumer
194      *            object that implements producer-specific interfaces
195      * @return set of supported interfaces
196      */
197     public Set<Class<?>> registerConsumer(Object consumer) {
198         Set<Class<?>> iSet = new HashSet<Class<?>>();
199
200         if (consumer != null)
201                              _registerConsumer(consumer,
202                                                consumer.getClass(), iSet,
203                                                new HashSet<Class<?>>());
204
205         return iSet;
206     }
207
208     /**
209      * Consumer deregistration
210      * 
211      * @param consumer
212      *            object to deregister
213      * @return number of unregistered interfaces
214      */
215     public int deregisterConsumer(Object consumer) {
216         if (consumer != null)
217             return _deregisterConsumer(consumer, consumer.getClass(),
218                                        new HashSet<Class<?>>());
219         else
220             return 0;
221     }
222
223 }