001/* 002 * VM-Operator 003 * Copyright (C) 2023 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.vmoperator.runner.qemu.events; 020 021import java.util.EnumSet; 022import org.jgrapes.core.Channel; 023import org.jgrapes.core.Components; 024import org.jgrapes.core.Event; 025 026/** 027 * The Class RunnerStateChange. 028 */ 029@SuppressWarnings("PMD.DataClass") 030public class RunnerStateChange extends Event<Void> { 031 032 /** 033 * The states. 034 */ 035 public enum RunState { 036 INITIALIZING, STARTING, BOOTING, BOOTED, TERMINATING, STOPPED; 037 038 /** 039 * Checks if the state is one of the states in which the VM is running. 040 * 041 * @return true, if is running 042 */ 043 public boolean vmRunning() { 044 return EnumSet.of(BOOTING, BOOTED, TERMINATING).contains(this); 045 } 046 047 /** 048 * Checks if the state is one of the states in which the VM is active. 049 * 050 * @return true, if is active 051 */ 052 public boolean vmActive() { 053 return EnumSet.of(BOOTING, BOOTED).contains(this); 054 } 055 } 056 057 private final RunState state; 058 private final String reason; 059 private final String message; 060 private final boolean failed; 061 062 /** 063 * Instantiates a new runner state change. 064 * 065 * @param state the state 066 * @param reason the reason 067 * @param message the message 068 * @param channels the channels 069 */ 070 public RunnerStateChange(RunState state, String reason, String message, 071 Channel... channels) { 072 this(state, reason, message, false, channels); 073 } 074 075 /** 076 * Instantiates a new runner state change. 077 * 078 * @param state the state 079 * @param reason the reason 080 * @param message the message 081 * @param failed the failed 082 * @param channels the channels 083 */ 084 public RunnerStateChange(RunState state, String reason, String message, 085 boolean failed, Channel... channels) { 086 super(channels); 087 this.state = state; 088 this.reason = reason; 089 this.failed = failed; 090 this.message = message; 091 } 092 093 /** 094 * Returns the new state. 095 * 096 * @return the state 097 */ 098 public RunState runState() { 099 return state; 100 } 101 102 /** 103 * Gets the reason. 104 * 105 * @return the reason 106 */ 107 public String reason() { 108 return reason; 109 } 110 111 /** 112 * Gets the message. 113 * 114 * @return the message 115 */ 116 public String message() { 117 return message; 118 } 119 120 /** 121 * Checks if is failed. 122 * 123 * @return the failed 124 */ 125 public boolean failed() { 126 return failed; 127 } 128 129 @Override 130 public String toString() { 131 StringBuilder builder = new StringBuilder(50); 132 builder.append(Components.objectName(this)) 133 .append(" [").append(state).append(": ").append(reason); 134 if (failed) { 135 builder.append(" (failed)"); 136 } 137 if (channels() != null) { 138 builder.append(", channels=").append(Channel.toString(channels())); 139 } 140 builder.append(']'); 141 return builder.toString(); 142 } 143 144}