1 package de.desy.video.sw;
2
3 import com.cosylab.gui.components.util.RunnerHelper;
4
5 import de.desy.acop.transport.AccessMode;
6 import de.desy.acop.transport.ConnectionFailed;
7 import de.desy.tine.client.TLink;
8 import de.desy.tine.client.TLinkCallback;
9 import de.desy.tine.dataUtils.TDataType;
10 import de.desy.tine.definitions.TAccess;
11 import de.desy.tine.definitions.TMode;
12 import de.desy.tine.types.IMAGE;
13 import de.desy.tine.queryUtils.TPropertyQuery;
14 import de.desy.tine.queryUtils.TQuery;
15 import de.desy.tine.startup.TInitializer;
16 import de.desy.tine.startup.DefaultTInitializerFactory;
17 import de.desy.tine.startup.TInitializerFactory;
18
19
20
21
22
23
24
25
26
27
28 public final class TineHandler implements TLinkCallback {
29
30
31 private ImageDisplayer displayer;
32
33
34
35 private IMAGE m_srcImgHdr;
36
37
38 private byte[] m_srcBuf=null;
39
40
41 private byte[] m_srcBufV2=null;
42
43
44 private TLink m_vidLink;
45
46 private boolean m_bTransferRunning = false;
47
48 private boolean m_bResetDisplayer = false;
49
50 private boolean m_bV3Transfer = true;
51
52 private int m_iTransportLengthV2 = 768*574*1+CVideoHeader2.HDRSIZE;
53
54
55 private final int m_iTransportLengthV3 = CVideoHeader3.TRANSPORT_LENGTH_V3;
56
57
58 public TineHandler(ImageDisplayer displayer) {
59 this.displayer = displayer;
60 }
61
62
63
64
65
66
67
68 public void callback(TLink link) {
69
70
71 if (!m_bTransferRunning) return;
72
73
74 if (m_bV3Transfer == true)
75 {
76
77
78 if (link.getLinkStatus() == 516)
79 return;
80
81
82
83 if (link.getLinkStatus() != 0) {
84 System.out.println("Link Error : code=" + link.getLinkStatus() + " msg=" + link.getLastError());
85 return;
86 }
87 }
88 else
89 {
90
91
92 if (link.getLinkStatus() == 607)
93 return;
94
95
96
97 if (link.getLinkStatus() != 0) {
98 System.out.println("Link Error : code=" + link.getLinkStatus() + " msg=" + link.getLastError());
99 return;
100 }
101
102
103
104
105
106
107 CVideoHeader2 vidH2 = new CVideoHeader2();
108
109
110
111 String vidFrom = "'V2:/"+link.getContext()+"/"+link.getDeviceServer()+"'";
112
113
114 boolean ret = vidH2.packageV2BlobIntoIMAGE(vidFrom, m_srcBufV2, m_srcImgHdr);
115 if (ret == false)
116 {
117
118 System.out.println("TINE Callback: Videotype V2 conversion unsuccessful");
119 return;
120 }
121
122 }
123
124
125
126 if (m_bResetDisplayer == true) {
127 displayer.resetForReceiving();
128 m_bResetDisplayer = false;
129 }
130
131
132 displayer.updateValue(new CVideoHeader3(m_srcImgHdr));
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public void openLink(String aAddress, AccessMode mode, int accessRate) throws Exception
149 {
150 String possiblyIncompleteTineDeviceName = aAddress;
151 String tineAddress;
152
153
154 m_bV3Transfer = probeTineV3andV2(possiblyIncompleteTineDeviceName);
155
156
157
158
159 if (m_srcBuf == null)
160 m_srcBuf = new byte[m_iTransportLengthV3];
161
162 m_srcImgHdr = new IMAGE();
163 m_srcImgHdr.setImageFrameBuffer(m_srcBuf);
164
165
166 m_bResetDisplayer = true;
167
168
169 if (m_bV3Transfer == true)
170 {
171
172 tineAddress = getFullQualifiedV3Address( possiblyIncompleteTineDeviceName );
173 TDataType img_dout = new TDataType(m_srcImgHdr);
174 m_vidLink = new TLink(tineAddress,img_dout,null,TAccess.CA_READ);
175 }
176 else
177 {
178
179 tineAddress = getFullQualifiedV2Address( possiblyIncompleteTineDeviceName );
180
181
182
183
184
185 int v2Size = m_iTransportLengthV2+1;
186
187
188 if (m_srcBufV2 == null || m_srcBufV2.length != v2Size)
189 {
190 m_srcBufV2 = new byte[v2Size];
191 }
192
193 TDataType img_dout = new TDataType(m_srcBufV2);
194 m_vidLink = new TLink(tineAddress,img_dout,null,TAccess.CA_READ);
195 }
196
197
198 short tMode;
199 if (mode == AccessMode.POLL) {
200 tMode = TMode.CM_POLL;
201 } else if (mode == AccessMode.POLL_NETWORK) {
202 tMode = TMode.CM_POLL | TMode.CM_MCAST;
203 } else if (mode == AccessMode.READ) {
204 tMode = TMode.CM_SINGLE;
205 } else {
206 throw new ConnectionFailed("Cannot establish a connection. AccessMode should be POLL or READ.", null);
207 }
208
209 if (m_vidLink.attach(tMode, this, accessRate) < 0)
210 {
211 m_vidLink.cancel();
212 m_vidLink = null;
213
214 throw new ConnectionFailed("No permanent connection to server (\""+m_vidLink.getLastError()+"\".", null);
215 }
216
217 m_bTransferRunning = true;
218 }
219
220
221
222
223
224
225 public void closeLink() {
226 if (m_vidLink != null) {
227 m_vidLink.cancel();
228 m_vidLink = null;
229 }
230 m_bTransferRunning = false;
231 }
232
233
234
235
236
237
238
239
240
241
242 private String getFullQualifiedV2Address(String aPartialAddress)
243 {
244 String retval;
245
246 retval = aPartialAddress;
247 if (retval.endsWith("/") == true) retval +="device_0";
248 retval += "/FRAME.GET";
249
250 return retval;
251 }
252
253
254
255
256
257
258
259
260 private String getFullQualifiedV3Address(String aPartialAddress)
261 {
262 String retval;
263
264 retval = aPartialAddress;
265 if (retval.endsWith("/") == true) retval +="Output";
266 retval += "/Frame.Sched";
267 return retval;
268 }
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 private boolean probeTineV3andV2(String aPartialAddress) throws Exception {
288
289
290 byte[] framebits3 = new byte[CVideoHeader3.HDRSIZE];
291 String addressVSV2 = getFullQualifiedV2Address(aPartialAddress);
292 TDataType dout3 = new TDataType(framebits3);
293
294 String devProp="Header";
295 if (aPartialAddress.endsWith("/")) aPartialAddress += "Output";
296
297
298
299 TLink ref = new TLink(aPartialAddress, devProp, dout3, null, TAccess.CA_READ);
300 int cc = ref.execute();
301 if (cc != 0)
302 {
303
304
305
306
307
308
309
310
311 String context = "DEFAULT";
312 String server = "";
313 String device = "device_0";
314 String property = "FRAME.GET";
315
316 String[] list = aPartialAddress.split("/");
317 int l = list.length;
318
319 if (l>0)
320 {
321 if (list[0].compareTo("") == 0)
322 {
323 if ((list.length>1) && (list[1].compareTo("") != 0)) context = list[1];
324 if ((list.length>2) && (list[2].compareTo("") != 0)) server = list[2];
325 }
326 else
327 {
328 server = list[0];
329 }
330 }
331
332
333
334
335 TPropertyQuery[] propList = TQuery.getPropertyInformation(context,server,device,property);
336
337
338 if (propList == null)
339 {
340 framebits3 = null;
341 throw new ConnectionFailed("Probing Address did not succeed. Transfer was cancelled.", null);
342 }
343
344
345 if (propList.length<1)
346 {
347 framebits3 = null;
348 throw new ConnectionFailed("Probing Address did not succeed. Transfer was cancelled.", null);
349 }
350
351
352
353
354 m_iTransportLengthV2 = propList[0].prpSize;
355 byte [] framebits2 = new byte[m_iTransportLengthV2+1];
356 TDataType dout2 = new TDataType(framebits2);
357
358
359 TLink ref1 = new TLink(addressVSV2, dout2, null, TAccess.CA_READ);
360 int cc1 = ref1.execute();
361
362 if ((cc1 == 0) || (cc1 == 607))
363 {
364
365
366
367
368
369
370 ref1.cancel();
371 framebits2 = null;
372 framebits3 = null;
373
374
375 return false;
376 }
377
378 ref1.cancel();
379 framebits2 = null;
380 framebits3 = null;
381
382
383
384
385
386
387 throw new ConnectionFailed("Probing Address did not succeed. Transfer was cancelled.", null);
388 } else {
389
390
391
392
393 ref.cancel();
394 if (dout3.dCompletionLength != CVideoHeader3.HDRSIZE) {
395 throw new ImageException("CF_IMAGE header is not "+CVideoHeader3.HDRSIZE+" bytes in size. Transfer was cancelled.");
396 }
397 }
398
399
400
401 framebits3 = null;
402 return true;
403 }
404
405
406 public static void main(String[] args) throws Exception
407 {
408
409
410 ImageDisplayer d = new ImageDisplayer();
411 TineHandler handler = new TineHandler(d);
412 handler.openLink("/TEST/SGP_IMM1", AccessMode.POLL, 1000);
413 RunnerHelper.runComponent(d, 800,600);
414 }
415 }