1 package de.desy.acop.video.displayer;
2
3 import java.util.logging.Handler;
4 import java.util.logging.Level;
5 import java.util.logging.LogRecord;
6 import java.util.logging.Logger;
7 import java.util.logging.StreamHandler;
8
9 import de.desy.acop.transport.ConnectionFailed;
10 import de.desy.acop.transport.ConnectionParameters;
11 import de.desy.tine.client.TLink;
12 import de.desy.tine.client.TLinkCallback;
13 import de.desy.tine.dataUtils.TDataType;
14 import de.desy.tine.definitions.TAccess;
15 import de.desy.tine.definitions.TFormat;
16 import de.desy.tine.definitions.TMode;
17 import de.desy.tine.queryUtils.TPropertyQuery;
18 import de.desy.tine.queryUtils.TQuery;
19 import de.desy.tine.types.IMAGE;
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public final class TineHandler implements TLinkCallback {
34
35 private static final String LOGGER_NAME = "TineVideo";
36
37
38
39
40
41 private static final int V2CODE_NO_DATA = 607;
42
43
44
45
46
47 private static final int V3CODE_NO_DATA = 516;
48
49
50 private TineImageReceiver receiver;
51
52
53
54
55
56 private IMAGE srcImage;
57
58
59 private byte[] srcBufV2;
60
61
62 private TLink tlink;
63
64
65 private boolean isTransfering;
66
67
68 private boolean isResetReceiver;
69
70
71 private boolean isTransferV3 = true;
72
73
74 private boolean isAImage = false;
75
76 public static final Logger logger;
77
78 static {
79 logger = Logger.getLogger(LOGGER_NAME);
80 LogFormatter formatter = new LogFormatter();
81 Handler handler = new StreamHandler(System.out, formatter) {
82 @Override
83 public synchronized void publish(LogRecord record) {
84 super.publish(record);
85 flush();
86 }
87 };
88 handler.setLevel(Level.INFO);
89 logger.setLevel(Level.INFO);
90 Handler[] handlers = logger.getHandlers();
91 for (Handler h : handlers) {
92 logger.removeHandler(h);
93 }
94 logger.addHandler(handler);
95 logger.setUseParentHandlers(false);
96 }
97
98
99 public TineHandler(TineImageReceiver receiver) {
100 this.receiver = receiver;
101 }
102
103
104
105
106
107
108
109 public void callback(TLink tlink) {
110 logger.log(Level.FINE, "Tine Handler: update received.");
111 if (!isTransfering) {
112 logger.log(Level.INFO, "Tine Handler: Invalid update.");
113 return;
114 }
115
116 int cc = tlink.getLinkStatus();
117 if (cc != 0) {
118 logger.log(isNoData(cc) ? Level.INFO : Level.WARNING,
119 "Tine Handler: addr = \"/" + tlink.getFullDeviceNameAndProperty()
120 + "\", code = " + tlink.getLinkStatus()
121 + ", msg = \"" + tlink.getLastError() + "\"");
122 return;
123 }
124
125 if (isTransferV3 == false) {
126
127
128
129 VideoHeaderV2 vidH2 = new VideoHeaderV2();
130
131
132
133 String vidFrom = "'V2:/" + tlink.getContext() + "/" + tlink.getDeviceServer() + "'";
134
135
136 if (!vidH2.packageV2BlobIntoIMAGE(vidFrom, srcBufV2, srcImage)) {
137 logger.log(Level.WARNING, "Tine Handler: Videotype V2 conversion failed, addr = " + vidFrom);
138 System.err.println("Videotype V2 conversion failed, addr = " + vidFrom);
139 return;
140 }
141 }
142
143
144
145 if (isResetReceiver) {
146 receiver.resetForReceiving();
147 isResetReceiver = false;
148 }
149
150
151 receiver.updateValue(srcImage);
152 }
153
154
155
156
157
158
159
160
161
162
163 public void openLink(ConnectionParameters connParams) throws ConnectionFailed {
164 if (connParams == null)
165 throw new NullPointerException("connParams == null!");
166 logger.log(Level.INFO, "Tine Handler: Connecting to " + connParams.getRemoteName() + ".");
167 TPropertyQuery[] infos = null;
168 try {
169 infos = TQuery.getPropertyInformation(connParams.getDeviceContext(),
170 connParams.getDeviceGroup(),
171 connParams.getDeviceName(),
172 connParams.getDeviceProperty());
173
174 } catch (Exception e) {
175 throw new ConnectionFailed("Probing Address did not succeed.", null);
176 }
177
178 if (infos == null || infos.length == 0)
179 throw new ConnectionFailed("Probing Address did not succeed.", null);
180
181 short type = infos[0].prpFormat;
182 int size = infos[0].prpSize;
183
184 switch (type) {
185 case TFormat.CF_BYTE:
186 if (size <= 1)
187 throw new ConnectionFailed("Illegal property data size: " + size, null);
188 size -= VideoHeaderV2.HDRSIZE;
189 isTransferV3 = false;
190 break;
191
192 case TFormat.CF_AIMAGE:
193 isAImage = true;
194
195 case TFormat.CF_IMAGE:
196 isTransferV3 = true;
197 break;
198
199 default:
200 throw new ConnectionFailed("Unsupported data type: " + TFormat.toString(type), null);
201 }
202
203
204
205
206
207
208
209 srcImage = new IMAGE(size);
210
211
212 isResetReceiver = true;
213
214
215 String address = "/" + connParams.getDeviceContext() + "/" + connParams.getDeviceGroup()
216 + "/" + connParams.getDeviceName() + "/" + connParams.getDeviceProperty();
217
218 if (!isTransferV3) {
219 int buffSize = size + VideoHeaderV2.HDRSIZE;
220 if (srcBufV2 == null || srcBufV2.length != buffSize)
221 srcBufV2 = new byte[buffSize];
222 tlink = new TLink(address, new TDataType(srcBufV2), null, TAccess.CA_READ);
223
224 } else
225
226 tlink = new TLink(address,
227 isAImage ? new TDataType(new IMAGE[] { srcImage }) : new TDataType(srcImage),
228 null,
229 TAccess.CA_READ);
230
231
232 short tMode;
233 switch (connParams.getAccessMode()) {
234 case POLL:
235 tMode = TMode.CM_POLL;
236 break;
237 case POLL_NETWORK:
238 tMode = TMode.CM_POLL | TMode.CM_MCAST;
239 break;
240 case POLL_CONNECT:
241 tMode = TMode.CM_POLL | TMode.CM_CONNECT;
242 break;
243 case READ:
244 tMode = TMode.CM_SINGLE;
245 break;
246 default:
247 throw new IllegalArgumentException("Illegal AccessMode: " + connParams.getAccessMode());
248 }
249
250 if (tlink.attach(tMode, this, connParams.getAccessRate()) < 0) {
251 tlink.cancel();
252 tlink = null;
253 throw new ConnectionFailed("No permanent connection to server (\"" + tlink.getLastError() + "\".", null);
254 }
255 logger.log(Level.INFO, "Tine Handler: Link established.");
256 isTransfering = true;
257 }
258
259
260
261
262
263
264
265
266 private boolean isNoData(int cc) {
267 return isTransferV3 ? (cc == V3CODE_NO_DATA) : (cc == V2CODE_NO_DATA);
268 }
269
270
271
272
273
274
275 public void closeLink() {
276 logger.log(Level.INFO, "Tine Handler: Disconnecting.");
277 if (tlink != null) {
278 tlink.close();
279 tlink = null;
280 }
281 isTransfering = false;
282 }
283
284
285
286
287
288
289
290
291
292
293 }