001/*
002 * JPPF.
003 * Copyright (C) 2005-2019 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.management;
020
021import java.io.Serializable;
022
023/**
024 * Instances of this class represent the state of a node.
025 * They are used as the result of node JMX monitoring request.
026 * @author Laurent Cohen
027 */
028public class JPPFNodeState implements Serializable {
029  /**
030   * Enumeration of connection states.
031   */
032  public enum ConnectionState {
033    /**
034     * The state is not yet known.
035     */
036    UNKNOWN("unknown"),
037    /**
038     * The node is connected.
039     */
040    CONNECTED("node.connected"),
041    /**
042     * The node is disconnected.
043     */
044    DISCONNECTED("node.disconnected");
045
046    /**
047     * The name to display.
048     */
049    private final String displayName;
050
051    /**
052     * Initialize this enum element with the specified localized display name.
053     * @param msg the display name or localize.
054     */
055    private ConnectionState(final String msg) {
056      displayName = msg;
057    }
058
059    /**
060     * Get the localizable display name.
061     * @return the key of a message ot localize.
062     */
063    public String getDisplayName() {
064      return displayName;
065    }
066  }
067
068  /**
069   * Enumeration of execution states.
070   */
071  public enum ExecutionState {
072    /**
073     * The state is not yet known.
074     */
075    UNKNOWN("unknown"),
076    /**
077     * The node is connected.
078     */
079    IDLE("node.idle"),
080    /**
081     * The node is disconnected.
082     */
083    EXECUTING("node.executing");
084
085    /**
086     * The name to display.
087     */
088    private final String displayName;
089
090    /**
091     * Initialize this enum element with the specified localized display name.
092     * @param msg the display name ot localize.
093     */
094    private ExecutionState(final String msg) {
095      displayName = msg;
096    }
097
098    /**
099     * Get the localizable display name.
100     * @return the key of a message ot localize.
101     */
102    public String getDisplayName() {
103      return displayName;
104    }
105  }
106
107  /**
108   * Explicit serialVersionUID.
109   */
110  private static final long serialVersionUID = 1L;
111  /**
112   * Status of the connection between the node and the server.
113   */
114  private ConnectionState connectionStatus = ConnectionState.UNKNOWN;
115  /**
116   * Latest execution status of the node.
117   */
118  private ExecutionState executionStatus = ExecutionState.IDLE;
119  /**
120   * The number of tasks executed by the node.
121   */
122  private int nbTasksExecuted = 0;
123  /**
124   * The total cpu time used by the task processing threads.
125   */
126  private long cpuTime = 0L;
127  /**
128   * Size of the node's thread pool.
129   */
130  private int threadPoolSize = -1;
131  /**
132   * Priority of the threads in the pool.
133   */
134  private int threadPriority = -1;
135  /**
136   * The current pending action for the node.
137   */
138  private NodePendingAction pendingAction = NodePendingAction.NONE;
139
140  /**
141   * Get the number of tasks executed by the node.
142   * @return the number of tasks as an int.
143   */
144  public synchronized int getNbTasksExecuted() {
145    return nbTasksExecuted;
146  }
147
148  /**
149   * Set the number of tasks executed by the node.
150   * @param nbTasksExecuted the number of tasks as an int.
151   */
152  public synchronized void setNbTasksExecuted(final int nbTasksExecuted) {
153    this.nbTasksExecuted = nbTasksExecuted;
154  }
155
156  /**
157   * Get the status of the connection between the node and the server.
158   * @return the connection status.
159   */
160  public synchronized ConnectionState getConnectionStatus() {
161    return connectionStatus;
162  }
163
164  /**
165   * Set the status of the connection between the node and the server.
166   * @param connectionStatus the connection status.
167   */
168  public synchronized void setConnectionStatus(final ConnectionState connectionStatus) {
169    this.connectionStatus = connectionStatus;
170  }
171
172  /**
173   * Get the latest execution status of the node.
174   * @return the execution status.
175   */
176  public synchronized ExecutionState getExecutionStatus() {
177    return executionStatus;
178  }
179
180  /**
181   * Get the latest execution status of the node.
182   * @param executionStatus the execution status.
183   */
184  public synchronized void setExecutionStatus(final ExecutionState executionStatus) {
185    this.executionStatus = executionStatus;
186  }
187
188  /**
189   * Get the size of the node's thread pool.
190   * @return the size as an int.
191   */
192  public int getThreadPoolSize() {
193    return threadPoolSize;
194  }
195
196  /**
197   * Set the size of the node's thread pool.
198   * @param threadPoolSize the size as an int.
199   */
200  public void setThreadPoolSize(final int threadPoolSize) {
201    this.threadPoolSize = threadPoolSize;
202  }
203
204  /**
205   * Get the total cpu time used by the task processing threads.
206   * @return the cpu time in milliseconds.
207   */
208  public synchronized long getCpuTime() {
209    return cpuTime;
210  }
211
212  /**
213   * Set the total cpu time used by the task processing threads.
214   * @param cpuTime the cpu time in milliseconds.
215   */
216  public synchronized void setCpuTime(final long cpuTime) {
217    this.cpuTime = cpuTime;
218  }
219
220  /**
221   * Get the priority of the threads in the pool.
222   * @return the priority as an int value.
223   */
224  public int getThreadPriority() {
225    return threadPriority;
226  }
227
228  /**
229   * Set the priority of the threads in the pool.
230   * @param threadPriority the priority as an int value.
231   */
232  public void setThreadPriority(final int threadPriority) {
233    this.threadPriority = threadPriority;
234  }
235
236
237  /**
238   * Get the current pending action for the node.
239   * @return a {@link NodePendingAction} enum element.
240   */
241  public synchronized NodePendingAction getPendingAction() {
242    return pendingAction;
243  }
244
245  /**
246   * Set the current pending action for the node.
247   * @param pendingAction a {@link NodePendingAction} enum element.
248   */
249  public synchronized void setPendingAction(final NodePendingAction pendingAction) {
250    this.pendingAction = pendingAction != null ? pendingAction : NodePendingAction.NONE;
251  }
252
253  /**
254   * Make a copy of this node state.
255   * @return a <code>JPPFNodeState</code> instance.
256   */
257  public JPPFNodeState copy() {
258    final JPPFNodeState s = new JPPFNodeState();
259    s.setNbTasksExecuted(getNbTasksExecuted());
260    s.setConnectionStatus(getConnectionStatus());
261    s.setExecutionStatus(getExecutionStatus());
262    s.setThreadPoolSize(getThreadPoolSize());
263    s.setThreadPriority(getThreadPriority());
264    s.setCpuTime(getCpuTime());
265    s.setPendingAction(getPendingAction());
266    return s;
267  }
268
269  @Override
270  public String toString() {
271    final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
272    sb.append("threadPoolSize=").append(threadPoolSize);
273    sb.append(", threadPriority=").append(threadPriority);
274    sb.append(", nbTasksExecuted=").append(nbTasksExecuted);
275    sb.append(", executionStatus=").append(executionStatus);
276    sb.append(", connectionStatus=").append(connectionStatus);
277    sb.append(", cpuTime=").append(cpuTime);
278    sb.append(", pendingAction=").append(pendingAction);
279    sb.append(']');
280    return sb.toString();
281  }
282
283  @Override
284  public int hashCode() {
285    final int prime = 31;
286    int result = 1;
287    result = prime * result + nbTasksExecuted;
288    //result = prime * result + (int) (cpuTime ^ (cpuTime >>> 32));
289    result = prime * result + ((executionStatus == null) ? 0 : executionStatus.hashCode());
290    result = prime * result + ((connectionStatus == null) ? 0 : connectionStatus.hashCode());
291    result = prime * result + threadPoolSize;
292    result = prime * result + threadPriority;
293    result = prime * result + ((pendingAction == null) ? 0 : pendingAction.hashCode());
294    return result;
295  }
296
297  @Override
298  public boolean equals(final Object obj) {
299    if (this == obj) return true;
300    if (obj == null) return false;
301    if (getClass() != obj.getClass()) return false;
302    final JPPFNodeState other = (JPPFNodeState) obj;
303    if (nbTasksExecuted != other.nbTasksExecuted) return false;
304    //if (cpuTime != other.cpuTime) return false;
305    if (executionStatus != other.executionStatus) return false;
306    if (connectionStatus != other.connectionStatus) return false;
307    if (threadPoolSize != other.threadPoolSize) return false;
308    if (threadPriority != other.threadPriority) return false;
309    if (pendingAction != other.pendingAction) return false;
310    return true;
311  }
312}