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 org.wpilib.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 */
013public final class NetworkTableEvent {
014  /** NetworkTable event kind. */
015  public enum Kind {
016    /**
017     * Initial listener addition. Set this to receive immediate notification of matches to other
018     * criteria.
019     */
020    IMMEDIATE(0x0001),
021
022    /** Client connected (on server, any client connected). */
023    CONNECTED(0x0002),
024
025    /** Client disconnected (on server, any client disconnected). */
026    DISCONNECTED(0x0004),
027
028    /** Any connection event (connect or disconnect). */
029    CONNECTION(0x0004 | 0x0002),
030
031    /** New topic published. */
032    PUBLISH(0x0008),
033
034    /** Topic unpublished. */
035    UNPUBLISH(0x0010),
036
037    /** Topic properties changed. */
038    PROPERTIES(0x0020),
039
040    /** Any topic event (publish, unpublish, or properties changed). */
041    TOPIC(0x0020 | 0x0010 | 0x0008),
042
043    /** Topic value updated (via network). */
044    VALUE_REMOTE(0x0040),
045
046    /** Topic value updated (local). */
047    VALUE_LOCAL(0x0080),
048
049    /** Topic value updated (network or local). */
050    VALUE_ALL(0x0080 | 0x0040),
051
052    /** Log message. */
053    LOG_MESSAGE(0x0100),
054
055    /** Time synchronized with server. */
056    TIME_SYNC(0x0200);
057
058    private final int value;
059
060    Kind(int value) {
061      this.value = value;
062    }
063
064    /**
065     * Returns the NetworkTable event kind value.
066     *
067     * @return The NetworkTable event kind value.
068     */
069    public int getValue() {
070      return value;
071    }
072  }
073
074  /**
075   * Handle of listener that was triggered. The value returned when adding the listener can be used
076   * to map this to a specific added listener.
077   */
078  public final int listener;
079
080  /**
081   * Determine if event is of a particular kind. For example, PUBLISH if the topic was not
082   * previously published. Also indicates the data included with the event:
083   *
084   * <ul>
085   *   <li>CONNECTED or DISCONNECTED: connInfo
086   *   <li>PUBLISH, UNPUBLISH, or PROPERTIES: topicInfo
087   *   <li>VALUE_REMOTE, VALUE_LOCAL: valueData
088   *   <li>LOG_MESSAGE: logMessage
089   * </ul>
090   *
091   * @param kind Kind
092   * @return True if event matches kind
093   */
094  public boolean is(Kind kind) {
095    return (m_flags & kind.getValue()) != 0;
096  }
097
098  private final int m_flags;
099
100  /** Connection information (for connection events). */
101  public final ConnectionInfo connInfo;
102
103  /** Topic information (for topic events). */
104  public final TopicInfo topicInfo;
105
106  /** Value data (for value events). */
107  public final ValueEventData valueData;
108
109  /** Log message (for log message events). */
110  public final LogMessage logMessage;
111
112  /** Log message (for log message events). */
113  public final TimeSyncEventData timeSyncData;
114
115  /**
116   * Constructor. This should generally only be used internally to NetworkTables.
117   *
118   * @param inst Instance
119   * @param listener Listener that was triggered
120   * @param flags Event flags
121   * @param connInfo Connection information
122   * @param topicInfo Topic information
123   * @param valueData Value data
124   * @param logMessage Log message
125   * @param timeSyncData Time sync data
126   */
127  public NetworkTableEvent(
128      NetworkTableInstance inst,
129      int listener,
130      int flags,
131      ConnectionInfo connInfo,
132      TopicInfo topicInfo,
133      ValueEventData valueData,
134      LogMessage logMessage,
135      TimeSyncEventData timeSyncData) {
136    this.m_inst = inst;
137    this.listener = listener;
138    this.m_flags = flags;
139    this.connInfo = connInfo;
140    this.topicInfo = topicInfo;
141    this.valueData = valueData;
142    this.logMessage = logMessage;
143    this.timeSyncData = timeSyncData;
144  }
145
146  /* Network table instance. */
147  private final NetworkTableInstance m_inst;
148
149  /**
150   * Gets the instance associated with this event.
151   *
152   * @return Instance
153   */
154  public NetworkTableInstance getInstance() {
155    return m_inst;
156  }
157}