1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.flow.element;
26 
27 import des.util.emm;
28 import des.util.logger;
29 
30 import des.flow.event;
31 import des.flow.signal;
32 
33 /+
34 action must be in:
35     preparation -> ctor
36     last actions before start processing -> process special input event
37     processing -> process
38     terminate all -> selfDestroy( external memory manager )
39 +/
40 
41 abstract class WorkElement : ExternalMemoryManager
42 {
43     mixin DirectEMM;
44     mixin AnywayLogger;
45 
46 private:
47     SignalProcessor signal_processor;
48     EventProcessor event_listener;
49 
50 public:
51 
52     abstract void process();
53 
54     EventProcessor[] getEventProcessors() { return []; }
55 
56     final void setEventListener( EventProcessor ep )
57     {
58         event_listener = ep;
59         logger.Debug( "set event listener [%s]", ep );
60     }
61 
62     final void pushEvent( in Event ev )
63     {
64         logger.trace( "push event with code [%d] timestamp [%d] to listener [%s]", ev.code, ev.timestamp, event_listener );
65         if( event_listener !is null )
66             event_listener.processEvent( ev );
67     }
68 
69     final void setSignalProcessor( SignalProcessor sp )
70     in { assert( sp !is null ); } body
71     {
72         signal_processor = sp;
73         logger.Debug( "set signal processor [%s]", sp );
74     }
75 
76     final void sendSignal( in Signal sg )
77     {
78         logger.trace( "send signal [%s] to processor [%s]", sg, signal_processor );
79         if( signal_processor !is null )
80             signal_processor.processSignal( sg );
81     }
82 }
83 
84 unittest
85 {
86     struct TestStruct { double x, y; string info; immutable(int)[] data; }
87     auto ts = TestStruct( 3.14, 2.7, "hello", [ 2, 3, 4 ] );
88 
89     class TestElement: WorkElement
90     {
91         override void process(){}
92         protected void selfDestroy(){}
93     }
94 
95     class TestSignalProcessor : SignalProcessor 
96     { void processSignal( in Signal s ) { assert( s.code == 0 ); } }
97 
98     auto elem = new TestElement;
99     elem.setSignalProcessor( new TestSignalProcessor );
100 
101     elem.sendSignal( Signal(0) );
102 
103     size_t cnt = 0;
104     elem.setEventListener( new FunctionEventProcessor (
105         ( in Event ev ) { cnt++; assert( ev.as!TestStruct == ts ); }
106     ));
107 
108     auto ev = Event( 8, ts );
109     elem.pushEvent( ev );
110     elem.pushEvent( const Event( ev ) );
111     elem.pushEvent( shared Event( ev ) );
112     elem.pushEvent( immutable Event( ev ) );
113     elem.pushEvent( const shared Event( ev ) );
114     elem.pushEvent( shared const Event( ev ) );
115     elem.pushEvent( immutable shared Event( ev ) );
116     elem.pushEvent( shared immutable Event( ev ) );
117     assert( cnt == 8 );
118 }