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