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 */ 029public class RunnerStateChange extends Event<Void> { 030 031 /** 032 * The states. 033 */ 034 public enum RunState { 035 INITIALIZING, STARTING, BOOTING, BOOTED, TERMINATING, STOPPED; 036 037 /** 038 * Checks if the state is one of the states in which the VM is running. 039 * 040 * @return true, if is running 041 */ 042 public boolean vmRunning() { 043 return EnumSet.of(BOOTING, BOOTED, TERMINATING).contains(this); 044 } 045 046 /** 047 * Checks if the state is one of the states in which the VM is active. 048 * 049 * @return true, if is active 050 */ 051 public boolean vmActive() { 052 return EnumSet.of(BOOTING, BOOTED).contains(this); 053 } 054 } 055 056 private final RunState state; 057 private final String reason; 058 private final String message; 059 private final boolean failed; 060 061 /** 062 * Instantiates a new runner state change. 063 * 064 * @param state the state 065 * @param reason the reason 066 * @param message the message 067 * @param channels the channels 068 */ 069 public RunnerStateChange(RunState state, String reason, String message, 070 Channel... channels) { 071 this(state, reason, message, false, channels); 072 } 073 074 /** 075 * Instantiates a new runner state change. 076 * 077 * @param state the state 078 * @param reason the reason 079 * @param message the message 080 * @param failed the failed 081 * @param channels the channels 082 */ 083 public RunnerStateChange(RunState state, String reason, String message, 084 boolean failed, Channel... channels) { 085 super(channels); 086 this.state = state; 087 this.reason = reason; 088 this.failed = failed; 089 this.message = message; 090 } 091 092 /** 093 * Returns the new state. 094 * 095 * @return the state 096 */ 097 public RunState runState() { 098 return state; 099 } 100 101 /** 102 * Gets the reason. 103 * 104 * @return the reason 105 */ 106 public String reason() { 107 return reason; 108 } 109 110 /** 111 * Gets the message. 112 * 113 * @return the message 114 */ 115 public String message() { 116 return message; 117 } 118 119 /** 120 * Checks if is failed. 121 * 122 * @return the failed 123 */ 124 public boolean failed() { 125 return failed; 126 } 127 128 @Override 129 public String toString() { 130 StringBuilder builder = new StringBuilder(50); 131 builder.append(Components.objectName(this)) 132 .append(" [").append(state).append(": ").append(reason); 133 if (failed) { 134 builder.append(" (failed)"); 135 } 136 if (channels() != null) { 137 builder.append(", channels=").append(Channel.toString(channels())); 138 } 139 builder.append(']'); 140 return builder.toString(); 141 } 142 143}