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