View Javadoc
1   /**
2    * Copyright (C) 2010-14 pvmanager developers. See COPYRIGHT.TXT
3    * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4    */
5   /*
6    * To change this template, choose Tools | Templates
7    * and open the template in the editor.
8    */
9   package org.epics.pvmanager.jca;
10  
11  import gov.aps.jca.Channel;
12  import gov.aps.jca.dbr.DBRType;
13  import org.epics.util.time.Timestamp;
14  
15  /**
16   * Represents the connection payload, which consists of the actual JCA
17   * Channel and the JCADataSource (which can be used to extract
18   * configuration parameters).
19   *
20   * @author carcassi
21   */
22  public class JCAConnectionPayload {
23      private final JCADataSource jcaDataSource;
24      private final Channel channel;
25      private final boolean connected;
26      private final boolean longString;
27      private final DBRType fieldType;
28      private final Timestamp eventTime = Timestamp.now();
29  
30      public JCAConnectionPayload(JCAChannelHandler channleHandler, Channel channel, JCAConnectionPayload previousPayload) {
31          this.jcaDataSource = channleHandler.getJcaDataSource();
32          this.channel = channel;
33          this.connected = channel != null && channel.getConnectionState() == Channel.ConnectionState.CONNECTED;
34          this.longString = channleHandler.isLongString();
35          if (channel.getFieldType().getClass() == null && previousPayload != null) {
36              // JNI sets the type to unknown on disconnect. We need
37              // to remember the type before the disconnection
38              this.fieldType = previousPayload.fieldType;
39          } else {
40              this.fieldType = channel.getFieldType();
41          }
42      }
43  
44      /**
45       * The JCADataSource that is using the channel.
46       * 
47       * @return the JCA data source
48       */
49      public JCADataSource getJcaDataSource() {
50          return jcaDataSource;
51      }
52  
53      /**
54       * The JCA channel.
55       * 
56       * @return JCA channel
57       */
58      public Channel getChannel() {
59          return channel;
60      }
61      
62      public DBRType getFieldType() {
63          return fieldType;
64      }
65      
66      /**
67       * True if the channel is not null and the connection state is connected.
68       * 
69       * @return ture if channel exists and is connected
70       */
71      public boolean isChannelConnected() {
72          return connected;
73      }
74      
75      /**
76       * True if the channel is not null, connected, and can be written to.
77       * 
78       * @return true if the channel is ready for write
79       */
80      public boolean isWriteConnected() {
81          return isChannelConnected() && channel.getWriteAccess();
82      }
83  
84      /**
85       * Whether the message payload should be handled as a long string.
86       * 
87       * @return true if long string support should be used
88       */
89      public boolean isLongString() {
90          return longString;
91      }
92  
93      /**
94       * Returns the local time of the connection event.
95       * 
96       * @return client connection/disconnection time
97       */
98      public Timestamp getEventTime() {
99          return eventTime;
100     }
101 
102     @Override
103     public String toString() {
104         return "JCAConnection [connected: " +isChannelConnected() + " writeConnected: " + isWriteConnected() + " channel: " + channel + "]";
105     }
106     
107 }