1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.proton.engine.BaseHandler;
13 
14 import hunt.collection.LinkedHashSet;
15 import hunt.proton.engine.CoreHandler;
16 import hunt.proton.engine.Handler;
17 import hunt.proton.engine.Record;
18 import hunt.proton.engine.Extendable;
19 import hunt.proton.engine.Event;
20 /**
21  * BaseHandler
22  *
23  */
24 
25 class BaseHandler : CoreHandler
26 {
27 
28     public static Handler getHandler(Record r ,string key) {
29         return  cast(Handler)(r.get(key));
30     }
31 
32     public static void setHandler(Record r, Handler handler,string key) {
33         r.set(key,  cast(Object)handler);
34     }
35 
36     public static Handler getHandler(Extendable ext , string key) {
37         return  cast(Handler)(ext.attachments().get(key));
38     }
39 
40     public static void setHandler(Extendable ext, Handler handler,string key) {
41         ext.attachments().set(key, cast(Object) handler);
42     }
43 
44     this()
45     {
46         _children = new LinkedHashSet!Handler();
47     }
48 
49     private LinkedHashSet!Handler _children ;//= new LinkedHashSet!Handler();
50 
51      public void onConnectionInit(Event e) { onUnhandled(e); }
52      public void onConnectionLocalOpen(Event e) { onUnhandled(e); }
53      public void onConnectionRemoteOpen(Event e) { onUnhandled(e); }
54      public void onConnectionLocalClose(Event e) { onUnhandled(e); }
55      public void onConnectionRemoteClose(Event e) { onUnhandled(e); }
56      public void onConnectionBound(Event e) { onUnhandled(e); }
57      public void onConnectionUnbound(Event e) { onUnhandled(e); }
58      public void onConnectionFinal(Event e) { onUnhandled(e); }
59 
60      public void onSessionInit(Event e) { onUnhandled(e); }
61      public void onSessionLocalOpen(Event e) { onUnhandled(e); }
62      public void onSessionRemoteOpen(Event e) { onUnhandled(e); }
63      public void onSessionLocalClose(Event e) { onUnhandled(e); }
64      public void onSessionRemoteClose(Event e) { onUnhandled(e); }
65      public void onSessionFinal(Event e) { onUnhandled(e); }
66 
67      public void onLinkInit(Event e) { onUnhandled(e); }
68      public void onLinkLocalOpen(Event e) { onUnhandled(e); }
69      public void onLinkRemoteOpen(Event e) { onUnhandled(e); }
70      public void onLinkLocalDetach(Event e) { onUnhandled(e); }
71      public void onLinkRemoteDetach(Event e) { onUnhandled(e); }
72      public void onLinkLocalClose(Event e) { onUnhandled(e); }
73      public void onLinkRemoteClose(Event e) { onUnhandled(e); }
74      public void onLinkFlow(Event e) { onUnhandled(e); }
75      public void onLinkFinal(Event e) { onUnhandled(e); }
76 
77      public void onDelivery(Event e) { onUnhandled(e); }
78      public void onTransport(Event e) { onUnhandled(e); }
79      public void onTransportError(Event e) { onUnhandled(e); }
80      public void onTransportHeadClosed(Event e) { onUnhandled(e); }
81      public void onTransportTailClosed(Event e) { onUnhandled(e); }
82      public void onTransportClosed(Event e) { onUnhandled(e); }
83 
84      public void onReactorInit(Event e) { onUnhandled(e); }
85      public void onReactorQuiesced(Event e) { onUnhandled(e); }
86      public void onReactorFinal(Event e) { onUnhandled(e); }
87 
88      public void onTimerTask(Event e) { onUnhandled(e); }
89 
90      public void onSelectableInit(Event e) { onUnhandled(e); }
91      public void onSelectableUpdated(Event e) { onUnhandled(e); }
92      public void onSelectableReadable(Event e) { onUnhandled(e); }
93      public void onSelectableWritable(Event e) { onUnhandled(e); }
94      public void onSelectableExpired(Event e) { onUnhandled(e); }
95      public void onSelectableError(Event e) { onUnhandled(e); }
96      public void onSelectableFinal(Event e) { onUnhandled(e); }
97 
98      public void onUnhandled(Event event) {}
99 
100     
101     public void add(Handler child) {
102         _children.add(child);
103     }
104 
105     
106     public LinkedHashSet!Handler children() {
107         return _children;
108     }
109 
110 	
111 	public void handle(Event e) {
112         int type = e.getType().ordinal;
113 
114         enum CONNECTION_INIT =   AmqpEventType.CONNECTION_INIT.ordinal;
115         enum CONNECTION_LOCAL_OPEN =AmqpEventType.CONNECTION_LOCAL_OPEN.ordinal;
116         enum CONNECTION_REMOTE_OPEN = AmqpEventType.CONNECTION_REMOTE_OPEN.ordinal;
117         enum CONNECTION_LOCAL_CLOSE = AmqpEventType.CONNECTION_LOCAL_CLOSE.ordinal;
118         enum CONNECTION_REMOTE_CLOSE =  AmqpEventType.CONNECTION_REMOTE_CLOSE.ordinal;
119         enum CONNECTION_BOUND = AmqpEventType.CONNECTION_BOUND.ordinal;
120         enum CONNECTION_UNBOUND = AmqpEventType.CONNECTION_UNBOUND.ordinal;
121         enum CONNECTION_FINAL = AmqpEventType.CONNECTION_FINAL.ordinal;
122 
123 
124         enum SESSION_INIT = AmqpEventType.SESSION_INIT.ordinal;
125         enum SESSION_LOCAL_OPEN = AmqpEventType.SESSION_LOCAL_OPEN.ordinal;
126         enum SESSION_REMOTE_OPEN = AmqpEventType.SESSION_REMOTE_OPEN.ordinal;
127         enum SESSION_LOCAL_CLOSE = AmqpEventType.SESSION_LOCAL_CLOSE.ordinal;
128         enum SESSION_REMOTE_CLOSE = AmqpEventType.SESSION_REMOTE_CLOSE.ordinal;
129         enum SESSION_FINAL =  AmqpEventType.SESSION_FINAL.ordinal;
130 
131 
132         enum LINK_INIT =  AmqpEventType.LINK_INIT.ordinal;
133         enum LINK_LOCAL_OPEN = AmqpEventType.LINK_LOCAL_OPEN.ordinal;
134         enum LINK_REMOTE_OPEN = AmqpEventType.LINK_REMOTE_OPEN.ordinal;
135         enum LINK_LOCAL_DETACH = AmqpEventType.LINK_LOCAL_DETACH.ordinal;
136         enum LINK_REMOTE_DETACH = AmqpEventType.LINK_REMOTE_DETACH.ordinal;
137         enum LINK_LOCAL_CLOSE = AmqpEventType.LINK_LOCAL_CLOSE.ordinal;
138         enum LINK_REMOTE_CLOSE = AmqpEventType.LINK_REMOTE_CLOSE.ordinal;
139         enum LINK_FLOW = AmqpEventType.LINK_FLOW.ordinal;
140         enum LINK_FINAL =  AmqpEventType.LINK_FINAL.ordinal;
141         enum DELIVERY = AmqpEventType.DELIVERY.ordinal;
142         enum TRANSPORT =  AmqpEventType.TRANSPORT.ordinal;
143         enum TRANSPORT_ERROR = AmqpEventType.TRANSPORT_ERROR.ordinal;
144         enum TRANSPORT_HEAD_CLOSED = AmqpEventType.TRANSPORT_HEAD_CLOSED.ordinal;
145         enum TRANSPORT_TAIL_CLOSED = AmqpEventType.TRANSPORT_TAIL_CLOSED.ordinal;
146         enum TRANSPORT_CLOSED = AmqpEventType.TRANSPORT_CLOSED.ordinal;
147         enum REACTOR_FINAL = AmqpEventType.REACTOR_FINAL.ordinal;
148         enum REACTOR_QUIESCED = AmqpEventType.REACTOR_QUIESCED.ordinal;
149         enum REACTOR_INIT = AmqpEventType.REACTOR_INIT.ordinal;
150         enum SELECTABLE_ERROR = AmqpEventType.SELECTABLE_ERROR.ordinal;
151         enum SELECTABLE_EXPIRED = AmqpEventType.SELECTABLE_EXPIRED.ordinal;
152         enum SELECTABLE_FINAL = AmqpEventType.SELECTABLE_FINAL.ordinal;
153         enum SELECTABLE_INIT = AmqpEventType.SELECTABLE_INIT.ordinal;
154         enum SELECTABLE_READABLE = AmqpEventType.SELECTABLE_READABLE.ordinal;
155         enum SELECTABLE_UPDATED = AmqpEventType.SELECTABLE_UPDATED.ordinal;
156         enum SELECTABLE_WRITABLE = AmqpEventType.SELECTABLE_WRITABLE.ordinal;
157         enum TIMER_TASK = AmqpEventType.TIMER_TASK.ordinal;
158         enum NON_CORE_EVENT = AmqpEventType.NON_CORE_EVENT.ordinal;
159         
160         switch (type) {
161         case CONNECTION_INIT:
162             onConnectionInit(e);
163             break;
164         case CONNECTION_LOCAL_OPEN:
165             onConnectionLocalOpen(e);
166             break;
167         case CONNECTION_REMOTE_OPEN:
168             onConnectionRemoteOpen(e);
169             break;
170         case CONNECTION_LOCAL_CLOSE:
171             onConnectionLocalClose(e);
172             break;
173         case CONNECTION_REMOTE_CLOSE:
174             onConnectionRemoteClose(e);
175             break;
176         case CONNECTION_BOUND:
177             onConnectionBound(e);
178             break;
179         case CONNECTION_UNBOUND:
180             onConnectionUnbound(e);
181             break;
182         case CONNECTION_FINAL:
183             onConnectionFinal(e);
184             break;
185         case SESSION_INIT:
186             onSessionInit(e);
187             break;
188         case SESSION_LOCAL_OPEN:
189             onSessionLocalOpen(e);
190             break;
191         case SESSION_REMOTE_OPEN:
192             onSessionRemoteOpen(e);
193             break;
194         case SESSION_LOCAL_CLOSE:
195             onSessionLocalClose(e);
196             break;
197         case SESSION_REMOTE_CLOSE:
198             onSessionRemoteClose(e);
199             break;
200         case SESSION_FINAL:
201             onSessionFinal(e);
202             break;
203         case LINK_INIT:
204             onLinkInit(e);
205             break;
206         case LINK_LOCAL_OPEN:
207             onLinkLocalOpen(e);
208             break;
209         case LINK_REMOTE_OPEN:
210             onLinkRemoteOpen(e);
211             break;
212         case LINK_LOCAL_DETACH:
213             onLinkLocalDetach(e);
214             break;
215         case LINK_REMOTE_DETACH:
216             onLinkRemoteDetach(e);
217             break;
218         case LINK_LOCAL_CLOSE:
219             onLinkLocalClose(e);
220             break;
221         case LINK_REMOTE_CLOSE:
222             onLinkRemoteClose(e);
223             break;
224         case LINK_FLOW:
225             onLinkFlow(e);
226             break;
227         case LINK_FINAL:
228             onLinkFinal(e);
229             break;
230         case DELIVERY:
231             onDelivery(e);
232             break;
233         case TRANSPORT:
234             onTransport(e);
235             break;
236         case TRANSPORT_ERROR:
237             onTransportError(e);
238             break;
239         case TRANSPORT_HEAD_CLOSED:
240             onTransportHeadClosed(e);
241             break;
242         case TRANSPORT_TAIL_CLOSED:
243             onTransportTailClosed(e);
244             break;
245         case TRANSPORT_CLOSED:
246             onTransportClosed(e);
247             break;
248         case REACTOR_FINAL:
249             onReactorFinal(e);
250             break;
251         case REACTOR_QUIESCED:
252             onReactorQuiesced(e);
253             break;
254         case REACTOR_INIT:
255             onReactorInit(e);
256             break;
257         case SELECTABLE_ERROR:
258             onSelectableError(e);
259             break;
260         case SELECTABLE_EXPIRED:
261             onSelectableExpired(e);
262             break;
263         case SELECTABLE_FINAL:
264             onSelectableFinal(e);
265             break;
266         case SELECTABLE_INIT:
267             onSelectableInit(e);
268             break;
269         case SELECTABLE_READABLE:
270             onSelectableReadable(e);
271             break;
272         case SELECTABLE_UPDATED:
273             onSelectableWritable(e);
274             break;
275         case SELECTABLE_WRITABLE:
276             onSelectableWritable(e);
277             break;
278         case TIMER_TASK:
279             onTimerTask(e);
280             break;
281         case NON_CORE_EVENT:
282             onUnhandled(e);
283             break;
284         default:
285         break;
286         }
287 
288 	}
289 
290     override
291      int opCmp(Object o)
292      {
293          return _children.size - ((cast(BaseHandler)o)._children).size;
294      }
295 
296 
297 
298     int opCmp(Handler o)
299     {
300       return  _children.size - ((cast(BaseHandler)o)._children).size;
301     }
302 }