001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.networktables;
006
007/**
008 * NetworkTables event.
009 *
010 * <p>There are different kinds of events. When creating a listener, a combination of event kinds
011 * can be listened to by building an EnumSet of NetworkTableEvent.Kind.
012 */
013@SuppressWarnings("MemberName")
014public final class NetworkTableEvent {
015  /** NetworkTable event kind. */
016  public enum Kind {
017    /**
018     * Initial listener addition. Set this to receive immediate notification of matches to other
019     * criteria.
020     */
021    kImmediate(0x0001),
022
023    /** Client connected (on server, any client connected). */
024    kConnected(0x0002),
025
026    /** Client disconnected (on server, any client disconnected). */
027    kDisconnected(0x0004),
028
029    /** Any connection event (connect or disconnect). */
030    kConnection(0x0004 | 0x0002),
031
032    /** New topic published. */
033    kPublish(0x0008),
034
035    /** Topic unpublished. */
036    kUnpublish(0x0010),
037
038    /** Topic properties changed. */
039    kProperties(0x0020),
040
041    /** Any topic event (publish, unpublish, or properties changed). */
042    kTopic(0x0020 | 0x0010 | 0x0008),
043
044    /** Topic value updated (via network). */
045    kValueRemote(0x0040),
046
047    /** Topic value updated (local). */
048    kValueLocal(0x0080),
049
050    /** Topic value updated (network or local). */
051    kValueAll(0x0080 | 0x0040),
052
053    /** Log message. */
054    kLogMessage(0x0100),
055
056    /** Time synchronized with server. */
057    kTimeSync(0x0200);
058
059    private final int value;
060
061    Kind(int value) {
062      this.value = value;
063    }
064
065    /**
066     * Returns the NetworkTable event kind value.
067     *
068     * @return The NetworkTable event kind value.
069     */
070    public int getValue() {
071      return value;
072    }
073  }
074
075  /**
076   * Handle of listener that was triggered. The value returned when adding the listener can be used
077   * to map this to a specific added listener.
078   */
079  public final int listener;
080
081  /**
082   * Determine if event is of a particular kind. For example, kPublish if the topic was not
083   * previously published. Also indicates the data included with the event:
084   *
085   * <ul>
086   *   <li>kConnected or kDisconnected: connInfo
087   *   <li>kPublish, kUnpublish, or kProperties: topicInfo
088   *   <li>kValueRemote, kValueLocal: valueData
089   *   <li>kLogMessage: logMessage
090   * </ul>
091   *
092   * @param kind Kind
093   * @return True if event matches kind
094   */
095  public boolean is(Kind kind) {
096    return (m_flags & kind.getValue()) != 0;
097  }
098
099  private final int m_flags;
100
101  /** Connection information (for connection events). */
102  public final ConnectionInfo connInfo;
103
104  /** Topic information (for topic events). */
105  public final TopicInfo topicInfo;
106
107  /** Value data (for value events). */
108  public final ValueEventData valueData;
109
110  /** Log message (for log message events). */
111  public final LogMessage logMessage;
112
113  /** Log message (for log message events). */
114  public final TimeSyncEventData timeSyncData;
115
116  /**
117   * Constructor. This should generally only be used internally to NetworkTables.
118   *
119   * @param inst Instance
120   * @param listener Listener that was triggered
121   * @param flags Event flags
122   * @param connInfo Connection information
123   * @param topicInfo Topic information
124   * @param valueData Value data
125   * @param logMessage Log message
126   * @param timeSyncData Time sync data
127   */
128  public NetworkTableEvent(
129      NetworkTableInstance inst,
130      int listener,
131      int flags,
132      ConnectionInfo connInfo,
133      TopicInfo topicInfo,
134      ValueEventData valueData,
135      LogMessage logMessage,
136      TimeSyncEventData timeSyncData) {
137    this.m_inst = inst;
138    this.listener = listener;
139    this.m_flags = flags;
140    this.connInfo = connInfo;
141    this.topicInfo = topicInfo;
142    this.valueData = valueData;
143    this.logMessage = logMessage;
144    this.timeSyncData = timeSyncData;
145  }
146
147  /* Network table instance. */
148  private final NetworkTableInstance m_inst;
149
150  /**
151   * Gets the instance associated with this event.
152   *
153   * @return Instance
154   */
155  public NetworkTableInstance getInstance() {
156    return m_inst;
157  }
158}