KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Connector > EEG > EEM > Elements > DataStreamLocalReceiverGen


1 /*
2  * DataStreamLocalReceiverGen.java
3  *
4  * Created on 13. duben 2002, 21:08
5  */

6
7 package SOFA.Connector.EEG.EEM.Elements;
8
9 import SOFA.Connector.EEG.CodeWriter.JWriterDirectorException;
10 import SOFA.Connector.EEG.EEM.Actions.JImplWriterDirectorBase;
11 import SOFA.SOFAnode.Made.TIR.OperationDef;
12
13 /**
14  *
15  * @author ghort
16  * @version
17  */

18 public class DataStreamLocalReceiverGen extends JImplWriterDirectorBase {
19
20     protected String JavaDoc is, os, bh, isPackage, osPackage;
21
22     protected static final int pullMode=0;
23     protected static final int pushMode=1;
24     
25     protected int sMode, rMode;
26
27     /** Creates new DataStreamLocalReceiverGen */
28     public DataStreamLocalReceiverGen(String JavaDoc provIfaceCDL, SOFA.Connector.Property[] params) throws JWriterDirectorException {
29         super(provIfaceCDL,params);
30         isPackage=SOFA.Connector.Property.findFirst(params,"isPackage");
31         osPackage=SOFA.Connector.Property.findFirst(params,"osPackage");
32         is=isPackage+".CNInterface";
33         os=osPackage+".CNInterface";
34         bh="SOFA.Connector.EEG.Types.DataStream.bytesHolder";
35
36         String JavaDoc mode=SOFA.Connector.Property.findFirst(params,"senderMode");
37         if (mode!=null) {
38             if (mode.equals("push")) {
39                 sMode=pushMode;
40             } else if (mode.equals("pull")) {
41                 sMode=pullMode;
42             } else {
43                 throw new JWriterDirectorException("Parameter senderMode is incorrect '"+mode+"'.");
44             }
45         } else {
46             throw new JWriterDirectorException("Parameter senderMode is not specified");
47         }
48
49         mode=SOFA.Connector.Property.findFirst(params,"receiverMode");
50         if (mode!=null) {
51             if (mode.equals("push")) {
52                 rMode=pushMode;
53             } else if (mode.equals("pull")) {
54                 rMode=pullMode;
55             } else {
56                 throw new JWriterDirectorException("Parameter receiverMode is incorrect '"+mode+"'.");
57             }
58         } else {
59             throw new JWriterDirectorException("Parameter receiverMode is not specified");
60         }
61
62         if (sMode==pullMode && rMode==pushMode) {
63             throw new JWriterDirectorException("Mode sender-push/receiver-pull is not supported.");
64         }
65     }
66
67     public String JavaDoc[] getImplements() {
68         if (rMode==pullMode) {
69       if (sMode==pullMode) {
70             return new String JavaDoc[] {"SOFA.Connector.EEG.Types.Receiver","SOFA.Connector.EEG.EEM.Elements.DataStreamLocalNotifiable",is};
71       } else {
72             return new String JavaDoc[] {"SOFA.Connector.EEG.Types.Receiver",is};
73       }
74         } else {
75             return new String JavaDoc[] {"SOFA.Connector.EEG.Types.Receiver","SOFA.Connector.Linkable",os};
76         }
77     }
78     
79     public String JavaDoc getClassBody() throws JWriterDirectorException {
80         String JavaDoc methods;
81         if (sMode==pushMode && rMode==pushMode) {
82             methods=
83                 "\tpublic void write(byte[] buffer, int len, String thr_id) {\n"+
84                 "\t\tclient.write(buffer,len,thr_id);\n"+
85                 "\t}\n\n";
86         } else if (sMode==pullMode && rMode==pullMode) {
87             methods=
88                 "\tprotected java.lang.reflect.Method senderRead;\n"+
89                 "\tprotected Object senderReadParams[]=new Object[3];\n\n"+
90         "\tpublic void linkNotification(Object sender) throws SOFA.Connector.LinkException {\n"+
91         "\t\tthis.sender=sender;\n"+
92         "\t\ttry {\n"+
93         "\t\t\tsenderRead=sender.getClass().getMethod(\"read\",new Class[] {"+bh+".class, Integer.TYPE, String.class});\n"+
94         "\t\t} catch (NoSuchMethodException e) {\n"+
95         "\t\t\tthrow new SOFA.Connector.LinkException(\"Cannot create dynamic local binding\",e);\n"+
96         "\t\t}\n"+
97         "\t}\n\n"+
98                 "\tpublic int read("+bh+" bufferHolder, int len, String thr_id) {\n"+
99         "\t\tsenderReadParams[0]=bufferHolder;\n"+
100         "\t\tsenderReadParams[1]=new Integer(len);\n"+
101         "\t\tsenderReadParams[2]=thr_id;\n"+
102         "\t\ttry {\n"+
103                 "\t\t\treturn senderRead.invoke(sender,senderReadParams).intValue();\n"+
104         "\t\t} catch (IllegalAccessException _e) {\n"+
105         "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"IllegalAccessException\", _e);\n"+
106         "\t\t} catch (java.lang.reflect.InvocationTargetException _e) {\n"+
107         "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"InvocationTargetException\", _e);\n"+
108         "\t\t}\n"+
109                 "\t}\n\n";
110         } else if (sMode==pushMode && rMode==pullMode) {
111             String JavaDoc outClass=SOFA.Connector.Property.findFirst(params,"outClass");
112             final int queueSize=16384;
113             methods=
114                 "\tprotected java.nio.ByteBuffer queue;\n"+
115                 "\tprotected int queueStart, queueEnd;\n"+
116                 "\t"+outClass+"() {\n"+
117                 "\t\tqueue=java.nio.ByteBuffer.allocateDirect("+queueSize+");\n"+
118                 "\t\tqueueStart=queueEnd=0;\n"+
119                 "\t}\n\n"+
120                 "\tpublic synchronized void write(byte[] buffer, int len, String thr_id) {\n"+
121                 "\t\tint rem=queueStart-queueEnd-1;\n"+
122                 "\t\tif (rem<0) {\n"+
123                 "\t\t\trem+="+queueSize+";\n"+
124                 "\t\t}\n"+
125                 "\t\twhile (rem<len) {\n"+
126                 "\t\t\ttry {\n"+
127                 "\t\t\t\twait();\n"+
128                 "\t\t\t} catch (InterruptedException e) {\n"+
129                 "\t\t\t}\n"+
130                 "\t\t\trem=queueStart-queueEnd-1;\n"+
131                 "\t\t\tif (rem<0) {\n"+
132                 "\t\t\t\trem+="+queueSize+";\n"+
133                 "\t\t\t}\n"+
134                 "\t\t}\n"+
135                 "\t\ttry {\n"+
136                 "\t\t\tqueue.position(queueEnd);\n"+
137                 "\t\t\tif (queueEnd<queueStart) {\n"+
138                 "\t\t\t\tqueue.put(buffer,0,len);\n"+
139                 "\t\t\t\tqueueEnd+=len\n"+
140                 "\t\t\t} else {\n"+
141                 "\t\t\t\tint partLen="+queueSize+"-queueEnd;\n"+
142                 "\t\t\t\tqueue.put(buffer,0,partLen);\n"+
143                 "\t\t\t\tqueue.position(0);\n"+
144                 "\t\t\t\tqueue.put(buffer,partLen,len-partLen);\n"+
145                 "\t\t\t\tqueueEnd=len-partLen\n"+
146                 "\t\t\t}\n"+
147                 "\t\t\tnotifyAll();\n"+
148                 "\t\t} catch (BufferOverflowException e) {\n"+
149                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
150                 "\t\t} catch (IndexOutOfBoundsException e) {\n"+
151                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
152                 "\t\t} catch (ReadOnlyBufferException e) {\n"+
153                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
154                 "\t\t}\n"+
155                 "\t}\n\n"+
156                 "\tpublic synchronized int read("+bh+" bufferHolder, int len, String thr_id) {\n"+
157                 "\t\tbyte[] buffer=bufferHolder.value;\n"+
158                 "\t\tint rem=queueEnd-queueStart;\n"+
159                 "\t\tif (rem<0) {\n"+
160                 "\t\t\trem+="+queueSize+";\n"+
161                 "\t\t}\n"+
162                 "\t\twhile (rem==0) {\n"+
163                 "\t\t\ttry {\n"+
164                 "\t\t\t\twait();\n"+
165                 "\t\t\t} catch (InterruptedException e) {\n"+
166                 "\t\t\t}\n"+
167                 "\t\t\trem=queueEnd-queueStart;\n"+
168                 "\t\t\tif (rem<0) {\n"+
169                 "\t\t\t\trem+="+queueSize+";\n"+
170                 "\t\t\t}\n"+
171                 "\t\t}\n"+
172                 "\t\tif (rem<len) {\n"+
173                 "\t\t\trem=len;\n"+
174                 "\t\t}\n"+
175                 "\t\ttry {\n"+
176                 "\t\t\tqueue.position(queueStart);\n"+
177                 "\t\t\tif (queueStart+rem<="+queueSize+") {\n"+
178                 "\t\t\t\tqueue.get(buffer,0,rem);\n"+
179                 "\t\t\t\tqueueStart+=rem\n"+
180                 "\t\t\t} else {\n"+
181                 "\t\t\t\tint partLen="+queueSize+"-queueStart;\n"+
182                 "\t\t\t\tqueue.get(buffer,0,partLen);\n"+
183                 "\t\t\t\tqueue.position(0);\n"+
184                 "\t\t\t\tqueue.put(buffer,partLen,rem-partLen);\n"+
185                 "\t\t\t\tqueueStart=rem-partLen\n"+
186                 "\t\t\t}\n"+
187                 "\t\t\tnotifyAll();\n"+
188                 "\t\t\treturn rem;\n"+
189                 "\t\t} catch (BufferOverflowException e) {\n"+
190                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
191                 "\t\t} catch (IndexOutOfBoundsException e) {\n"+
192                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
193                 "\t\t} catch (ReadOnlyBufferException e) {\n"+
194                 "\t\t\tthrow new SOFA.Connector.ConnectorTransportException(\"Unexpected exception caught.\",e);\n"+
195                 "\t\t}\n"+
196                 "\t}\n\n";
197         } else {
198             throw new JWriterDirectorException("Mode sender-push/receiver-pull is not supported.");
199         }
200
201         return managerTieImpl() + "\n" +
202             (sMode==pullMode && rMode==pullMode ? "\tprotected Object sender;\n" : "")+
203             "\tprotected "+os+" client;\n\n"+
204             "\tpublic void link(Object target) {\n"+
205             "\t\tclient=("+os+")target;\n"+
206             "\t}\n\n"+
207             "\tpublic SOFA.Connector.TaggedProfile[] getProfiles() {\n"+
208             "\t\treturn new SOFA.Connector.TaggedProfile[] { new SOFA.Connector.ECG.Profiles.LocalProfile(this) };\n"+
209             "\t}\n\n"+methods+"\n\n"+
210                         "\tpublic org.objectweb.fractal.api.Type getFcItfType() { return null; }\n"+
211                         "\tpublic boolean isFcInternalItf() { return false; }\n"+
212                         "\tpublic String getFcItfName() { throw new SOFA.Connector.ConnectorTransportException(\"Interconection via EventPassing connector - getFcItfName is not supported.\"); }\n"+
213                         "\tpublic org.objectweb.fractal.api.Component getFcItfOwner() { throw new SOFA.Connector.ConnectorTransportException(\"Interconection via EventPassing connector - getFcItfOwner is not supported.\"); }\n\n";
214     }
215     
216     public String JavaDoc getMethodDef(OperationDef mt) throws JWriterDirectorException {
217         return null;
218     }
219
220     public boolean defaultImplements() {
221         return false;
222     }
223 }
224
Popular Tags