forked from epics-base/pvDatabaseJava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPVRecord.java
553 lines (534 loc) · 20.2 KB
/
PVRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* EPICS pvData is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
package org.epics.pvdatabase;
import java.util.concurrent.locks.ReentrantLock;
import org.epics.pvaccess.server.rpc.Service;
import org.epics.pvdata.copy.PVCopy;
import org.epics.pvdata.copy.PVCopyTraverseMasterCallback;
import org.epics.pvdata.factory.ConvertFactory;
import org.epics.pvdata.misc.LinkedList;
import org.epics.pvdata.misc.LinkedListCreate;
import org.epics.pvdata.misc.LinkedListNode;
import org.epics.pvdata.property.PVTimeStamp;
import org.epics.pvdata.property.PVTimeStampFactory;
import org.epics.pvdata.property.TimeStamp;
import org.epics.pvdata.property.TimeStampFactory;
import org.epics.pvdata.pv.Convert;
import org.epics.pvdata.pv.PVField;
import org.epics.pvdata.pv.PVStructure;
import org.epics.pvdata.pv.PostHandler;
import org.epics.pvdata.pv.Type;
/**
* Base class for a PVRecord.
* Derived classes only need to implement a constructor and optionally process and destroy.
* @author mrk
* @since 2015.01.20
*/
public class PVRecord implements PVCopyTraverseMasterCallback {
private static final Convert convert = ConvertFactory.getConvert();
private static LinkedListCreate<PVListener> listenerListCreate = new LinkedListCreate<PVListener>();
private static LinkedListCreate<PVRecordClient> clientListCreate = new LinkedListCreate<PVRecordClient>();
private String recordName;
private PVStructure pvStructure;
private BasePVRecordStructure pvRecordStructure = null;
private LinkedList<PVListener> pvAllListenerList = listenerListCreate.create();
private LinkedList<PVRecordClient> clientList = clientListCreate.create();
private ReentrantLock lock = new ReentrantLock();
private int depthGroupPut = 0;
private int traceLevel = 0;
private static volatile int numberRecords = 0;
private int id = numberRecords++;
private PVTimeStamp pvTimeStamp = PVTimeStampFactory.create();
private TimeStamp timeStamp = TimeStampFactory.create();
private boolean isDestroyed = false;
// following only valid while addListener or removeListener is active.
private boolean isAddListener = false;
private PVListener pvListener = null;
/**
* Create a PVRecord that has pvStructure as it's top level structure.
* A derived class must call super(recordName, pvStructure).
* @param recordName The record name.
* @param pvStructure The top level structure.
*/
public PVRecord(String recordName,PVStructure pvStructure) {
if(pvStructure.getParent()!=null) {
throw new IllegalStateException(recordName + " pvStructure not a top level structure");
}
this.recordName = recordName;
this.pvStructure = pvStructure;
pvRecordStructure = new BasePVRecordStructure(pvStructure,null,this);
PVField pvField = pvStructure.getSubField("timeStamp");
if(pvField!=null) pvTimeStamp.attach(pvField);
}
/**
* Destroy the PVRecord. Release any resources used and
* get rid of listeners and requesters.
* If derived class overrides this then it must call super.destroy()
* after it has destroyed resources it uses.
*/
public void destroy()
{
if(traceLevel>0) {
System.out.println("PVRecord::destroy() " + recordName);
}
lock.lock();
try {
if(isDestroyed) return;
isDestroyed = true;
} finally {
lock.unlock();
}
pvTimeStamp.detach();
PVDatabase pvDatabase = PVDatabaseFactory.getMaster();
if(pvDatabase!=null) pvDatabase.removeRecord(this);
while(true) {
LinkedListNode<PVListener> listNode = pvAllListenerList.removeHead();
if(listNode==null) break;
PVListener listener = listNode.getObject();
listener.unlisten(this);
}
while(true) {
LinkedListNode<PVRecordClient> listNode = clientList.removeHead();
if(listNode==null) break;
PVRecordClient pvRecordClient = listNode.getObject();
pvRecordClient.detach(this);
}
}
/**
* A derived method is expected to override this method and can also call this method.
* It is the method that makes a record smart.
* If a timeStamp field is present this method sets it equal to the current time.
* If a derived class encounters errors it should raise alarms
*/
public void process()
{
if(traceLevel>2) {
System.out.println("PVRecord::process() " + recordName);
}
if(pvTimeStamp.isAttached()) {
pvTimeStamp.get(timeStamp);
timeStamp.getCurrentTime();
pvTimeStamp.set(timeStamp);
}
}
/**
* Get the record instance name.
* @return The name.
*/
public final String getRecordName() {
return recordName;
}
/**
* Get the top level PVRecordStructure.
* @return The PVRecordStructure interface.
*/
public final PVRecordStructure getPVRecordStructure() {
return pvRecordStructure;
}
/**
* Convenience method for derived classes.
* @return The top level PVStructure.
*/
public final PVStructure getPVStructure()
{
return pvStructure;
}
/**
* Find the PVRecordField for the pvField.
* @param pvField The pvField interface.
* @return The PVRecordField interface or null is not in record.
*/
public final PVRecordField findPVRecordField(PVField pvField) {
return findPVRecordField(pvRecordStructure,pvField);
}
private PVRecordField findPVRecordField(PVRecordStructure pvrs,PVField pvField) {
int desiredOffset = pvField.getFieldOffset();
PVField pvf = pvrs.getPVField();
int offset = pvf.getFieldOffset();
if(offset==desiredOffset) return pvrs;
PVRecordField[] pvrss = pvrs.getPVRecordFields();
for(int i=0; i<pvrss.length; i++) {
PVRecordField pvrf = pvrss[i];
pvf = pvrf.getPVField();
offset = pvf.getFieldOffset();
if(offset==desiredOffset) return pvrf;
int nextOffset = pvf.getNextFieldOffset();
if(nextOffset<=desiredOffset) continue;
return findPVRecordField((PVRecordStructure)pvrf,pvField);
}
throw new IllegalStateException(recordName + " pvField " + pvField.getFieldName() + " not in PVRecord");
}
/**
* Lock the record instance.
* This must be called before accessing the record.
*/
public final void lock() {
if(traceLevel>2) {
System.out.println("PVRecord::lock() " + recordName);
}
lock.lock();
}
/**
* Unlock the record.
*/
public final void unlock() {
if(traceLevel>2) {
System.out.println("PVRecord::unlock() " + recordName);
}
lock.unlock();
}
/**
* Try to lock the record instance.
* This can be called before accessing the record instead of lock.
* @return If true then it is just like lock. If false the record must not be accessed.
*/
public final boolean tryLock() {
if(traceLevel>2) {
System.out.println("PVRecord::tryLock() " + recordName);
}
return lock.tryLock();
}
/**
* While holding lock on this record lock another record.
* If the other record is already locked than this record may be unlocked.
* The caller must call the unlock method of the other record when done with it.
* @param otherRecord the other record.
*/
public final void lockOtherRecord(PVRecord otherRecord) {
if(traceLevel>2) {
System.out.println("PVRecord::lockOtherRecord() " + recordName);
}
PVRecord impl = (PVRecord)otherRecord;
int otherId = impl.id;
if(id<=otherId) {
otherRecord.lock();
return;
}
int count = lock.getHoldCount();
for(int i=0; i<count; i++) lock.unlock();
otherRecord.lock();
for(int i=0; i<count; i++) lock.lock();
}
/**
* Register a client of the record.
* This must be called by any code that connects to the record.
* @param pvRecordClient The record client.
* @return (false,true) means (failure,success)
*/
public final boolean addPVRecordClient(PVRecordClient pvRecordClient) {
if(traceLevel>1) {
System.out.println("PVRecord::addPVRecordClient() " + recordName);
}
lock.lock();
try {
if(isDestroyed) return false;
if(clientList.contains(pvRecordClient)) return false;
LinkedListNode<PVRecordClient> listNode = clientListCreate.createNode(pvRecordClient);
if(traceLevel>1) {
System.out.println("PVRecord::addPVRecordClient() calling clientList.push_back(pvRecordClient)");
}
clientList.addTail(listNode);
return true;
} finally {
lock.unlock();
}
}
/**
* remove a client of the record.
* This must be called by any code that disconnects from the record.
* @param pvRecordClient The record client.
* @return (false,true) means (failure,success)
*/
public final boolean removePVRecordClient(PVRecordClient pvRecordClient) {
if(traceLevel>1) {
System.out.println("PVRecord::removePVRecordClient() " + recordName);
}
lock.lock();
try {
if(isDestroyed) return false;
if(!clientList.contains(pvRecordClient)) return false;
clientList.remove(pvRecordClient);
return true;
} finally {
lock.unlock();
}
}
/**
* Add a PVListener. This must be called before pvField.addListener.
* @param listener The listener.
* @param pvCopy The pvStructure that has the client fields.
* @return (false,true) means (failure,success)
*/
public final boolean addListener(PVListener listener,PVCopy pvCopy) {
if(traceLevel>1) {
System.out.println("PVRecord::addListener() " + recordName);
}
lock.lock();
try {
if(isDestroyed) return false;
if(pvAllListenerList.contains(listener)) return false;
LinkedListNode<PVListener> listNode = listenerListCreate.createNode(listener);
pvAllListenerList.addTail(listNode);
this.pvListener = listener;
isAddListener = true;
pvCopy.traverseMaster(this);
this.pvListener = null;
return true;
} finally {
lock.unlock();
}
}
/* (non-Javadoc)
* @see org.epics.pvdata.copy.PVCopyTraverseMasterCallback#nextMasterPVField(org.epics.pvdata.pv.PVField)
*/
public void nextMasterPVField(PVField pvField) {
BasePVRecordField pvRecordField = (BasePVRecordField)findPVRecordField(pvField);
if(isAddListener) {
pvRecordField.addListener(this.pvListener);
} else {
pvRecordField.removeListener(this.pvListener);
}
}
/**
* Remove a PVListener.
* @param listener The listener.
* @param pvCopy The pvStructure that has the client fields.
* @return (false,true) means (failure,success)
*/
public final boolean removeListener(PVListener listener,PVCopy pvCopy) {
if(traceLevel>1) {
System.out.println("PVRecord::removeListener() " + recordName);
}
lock.lock();
try {
if(isDestroyed) return false;
if(!pvAllListenerList.contains(listener)) return false;
pvAllListenerList.remove(listener);
this.pvListener = listener;
isAddListener = false;
pvCopy.traverseMaster(this);
this.pvListener = null;
return true;
} finally {
lock.unlock();
}
}
/**
* Return a service corresponding to the specified request PVStructure.
* @param pvRequest The request PVStructure
* @return The corresponding service
*/
public Service getService(PVStructure pvRequest)
{
return null;
}
/**
* Begin a group of related puts.
*/
public final void beginGroupPut() {
if(++depthGroupPut>1) return;
if(traceLevel>2) {
System.out.println("PVRecord::beginGroupPut() " + recordName);
}
// no need to synchronize because record must be locked when this is called.
LinkedListNode<PVListener> listNode = pvAllListenerList.getHead();
while(listNode!=null) {
PVListener pvListener = listNode.getObject();
pvListener.beginGroupPut(this);
listNode = pvAllListenerList.getNext(listNode);
}
}
/**
* End of a group of related puts.
*/
public final void endGroupPut() {
if(--depthGroupPut>0) return;
if(traceLevel>2) {
System.out.println("PVRecord::endGroupPut() " + recordName);
}
// no need to synchronize because record must be locked when this is called.
LinkedListNode<PVListener> listNode = pvAllListenerList.getHead();
while(listNode!=null) {
PVListener pvListener = listNode.getObject();
pvListener.endGroupPut(this);
listNode = pvAllListenerList.getNext(listNode);
}
}
/**
* Get trace level: (0,1,2) means (nothing,lifetime,process)
* @return the level
*/
public final int getTraceLevel(){ return traceLevel;}
/**
* Set trace level: (0,1,2) means (nothing,lifetime,process)
* @param level The level
*/
public final void setTraceLevel(int level) { traceLevel = level;}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() { return toString(0);}
/**
* Implement standard toString()
* @param indentLevel indentation level.
* @return The record as a string.
*/
public String toString(int indentLevel) {
StringBuilder builder = new StringBuilder();
convert.newLine(builder,indentLevel);
builder.append("record ");
builder.append(recordName);
builder.append(" ");
pvStructure.toString(builder, indentLevel);
return builder.toString();
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return id;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
return (obj instanceof PVRecord && ((PVRecord)obj).id == id);
}
private static class BasePVRecordField implements PVRecordField, PostHandler{
private LinkedListCreate<PVListener> linkedListCreate = new LinkedListCreate<PVListener>();
private PVField pvField = null;
private PVRecord pvRecord = null;
private PVRecordStructure parent = null;
private boolean isStructure = false;
private LinkedList<PVListener> pvListenerList = linkedListCreate.create();
private String fullName = null;
private String fullFieldName = null;
BasePVRecordField(PVField pvField,PVRecordStructure parent,PVRecord pvRecord) {
this.pvField = pvField;
this.parent = parent;
this.pvRecord = pvRecord;
if(pvField.getField().getType()==Type.structure) isStructure = true;
pvField.setPostHandler(this);
}
public PVRecordStructure getParent() {
return parent;
}
public PVField getPVField() {
return pvField;
}
public String getFullFieldName() {
if(fullFieldName==null) createNames();
return fullFieldName;
}
public String getFullName() {
if(fullName==null) createNames();
return fullName;
}
public PVRecord getPVRecord() {
return pvRecord;
}
public void postPut() {
if(parent!=null) {
BasePVRecordField pvf = (BasePVRecordField)parent;
pvf.postParent(this);
}
postSubField();
}
private void postParent(PVRecordField subField) {
LinkedListNode<PVListener> listNode = pvListenerList.getHead();
while(listNode!=null) {
PVListener pvListener = listNode.getObject();
pvListener.dataPut((PVRecordStructure)this,subField);
listNode = pvListenerList.getNext(listNode);
}
if(parent!=null) {
BasePVRecordField pv = (BasePVRecordField)parent;
pv.postParent(subField);
}
}
private void postSubField() {
callListener();
if(isStructure) {
PVRecordStructure recordStructure = (PVRecordStructure)this;
PVRecordField[] pvRecordFields = recordStructure.getPVRecordFields();
for(int i=0; i<pvRecordFields.length; i++) {
BasePVRecordField pv = (BasePVRecordField)pvRecordFields[i];
pv.postSubField();
}
}
}
private void callListener() {
LinkedListNode<PVListener> listNode = pvListenerList.getHead();
while(listNode!=null) {
PVListener pvListener = listNode.getObject();
pvListener.dataPut(this);
listNode = pvListenerList.getNext(listNode);
}
}
private void createNames(){
StringBuilder builder = new StringBuilder();
PVField pvField = getPVField();
boolean isLeaf = true;
while(pvField!=null) {
String fieldName = pvField.getFieldName();
PVStructure pvParent = pvField.getParent();
if(!isLeaf && pvParent!=null) {
builder.insert(0, '.');
}
isLeaf = false;
builder.insert(0, fieldName);
pvField = pvParent;
}
fullFieldName = builder.toString();
String xxx = pvRecord.getRecordName();
if(fullFieldName.length()>0) xxx += ".";
builder.insert(0, xxx);
fullName = builder.toString();
}
private boolean addListener(PVListener pvListener) {
if(pvRecord.getTraceLevel()>1) {
System.out.println("PVRecordField::addListener() " + getFullName() );
}
if(pvListenerList.contains(pvListener)) return false;
LinkedListNode<PVListener> listNode = linkedListCreate.createNode(pvListener);
pvListenerList.addTail(listNode);
return true;
}
// This is only called by PVRecord, which has the record locked.
private void removeListener(PVListener pvListener) {
if(pvRecord.getTraceLevel()>1) {
System.out.println("PVRecordField::removeListener() " + getFullName() );
}
pvListenerList.remove(pvListener);
}
}
private static class BasePVRecordStructure extends BasePVRecordField implements PVRecordStructure {
private BasePVRecordField[] pvRecordFields;
BasePVRecordStructure(PVStructure pvStructure,PVRecordStructure parent,PVRecord pvRecord) {
super(pvStructure,parent,pvRecord);
PVField[] pvFields = pvStructure.getPVFields();
pvRecordFields = new BasePVRecordField[pvFields.length];
for(int i=0; i<pvFields.length; i++) {
PVField pvField = pvFields[i];
if(pvField.getField().getType()==Type.structure) {
pvRecordFields[i] = new BasePVRecordStructure((PVStructure)pvField,this,pvRecord);
} else {
pvRecordFields[i] = new BasePVRecordField(pvField,this,pvRecord);
}
}
}
public PVRecordField[] getPVRecordFields() {
return pvRecordFields;
}
public PVStructure getPVStructure() {
return (PVStructure)super.getPVField();
}
}
}