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.arch.emm;
28 import des.util.logsys;
29 
30 import des.flow.event;
31 import des.flow.signal;
32 
33 /++
34 Inner interpret of thread
35 
36 action must be in:
37   * preparation               -> ctor
38   * last actions before start -> process special input event
39   * actions on pause          -> process special input event
40   * processing                -> process
41   * terminate all             -> selfDestroy( external memory manager )
42 +/
43 abstract class WorkElement : EventBus, SignalBus, ExternalMemoryManager
44 {
45     mixin EMM;
46     mixin ClassLogger;
47 
48 private:
49 
50     ///
51     SignalProcessor signal_processor;
52 
53     ///
54     EventProcessor event_listener;
55 
56 public:
57 
58     /// main work function
59     abstract void process();
60 
61     /// 
62     EventProcessor[] getEventProcessors() { return []; }
63 
64     ///
65     final void setEventListener( EventProcessor ep )
66     {
67         event_listener = ep;
68         logger.Debug( "set event listener [%s]", ep );
69     }
70 
71     /// push event to event listener if it exists
72     final void pushEvent( in Event ev )
73     {
74         logger.trace( "push event with code [%d] timestamp [%d] to listener [%s]", ev.code, ev.timestamp, event_listener );
75         if( event_listener !is null )
76             event_listener.processEvent( ev );
77     }
78 
79     ///
80     final void setSignalProcessor( SignalProcessor sp )
81     in { assert( sp !is null ); } body
82     {
83         signal_processor = sp;
84         logger.Debug( "set signal processor [%s]", sp );
85     }
86 
87     /// send signal to signal processor if it exists
88     final void sendSignal( in Signal sg )
89     {
90         logger.trace( "send signal [%s] to processor [%s]", sg, signal_processor );
91         if( signal_processor !is null )
92             signal_processor.processSignal( sg );
93     }
94 }
95 
96 ///
97 unittest
98 {
99     struct TestStruct { double x, y; string info; immutable(int)[] data; }
100     auto ts = TestStruct( 3.14, 2.7, "hello", [ 2, 3, 4 ] );
101 
102     class TestElement: WorkElement
103     {
104         override void process(){}
105     }
106 
107     class TestSignalProcessor : SignalProcessor 
108     { void processSignal( in Signal s ) { assert( s.code == 0 ); } }
109 
110     auto elem = new TestElement;
111     elem.setSignalProcessor( new TestSignalProcessor );
112 
113     elem.sendSignal( Signal(0) );
114 
115     size_t cnt = 0;
116     elem.setEventListener( new class EventProcessor {
117         void processEvent( in Event ev )
118         { cnt++; assert( ev.as!TestStruct == ts ); }
119         });
120 
121     auto ev = Event( 8, ts );
122     elem.pushEvent( ev );
123     elem.pushEvent( const Event( ev ) );
124     elem.pushEvent( shared Event( ev ) );
125     elem.pushEvent( immutable Event( ev ) );
126     elem.pushEvent( const shared Event( ev ) );
127     elem.pushEvent( shared const Event( ev ) );
128     elem.pushEvent( immutable shared Event( ev ) );
129     elem.pushEvent( shared immutable Event( ev ) );
130     assert( cnt == 8 );
131 }