001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.actions;
024:
025: import net.fenyo.gnetwatch.Config;
026: import net.fenyo.gnetwatch.GeneralException;
027: import net.fenyo.gnetwatch.activities.Background;
028: import net.fenyo.gnetwatch.targets.*;
029: import net.fenyo.gnetwatch.GUI.*;
030:
031: import java.io.*;
032: import java.util.Date;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.eclipse.swt.graphics.Image;
037:
038: /**
039: * Action is the base class for any action: ActionFlood, ActionPing, ActionSNMP, etc.
040: * An action applies an operation to a target and adds events to this target depending
041: * on the result of the operation and the computed RTT.
042: * @author Alexandre Fenyo
043: * @version $Id: Action.java,v 1.20 2007/03/12 05:04:15 fenyo Exp $
044: */
045:
046: public class Action extends VisualElement {
047: private static Log log = LogFactory.getLog(Action.class);
048:
049: // GUI & Queue threads
050: // supports any thread
051: private Target target;
052: private Background background;
053:
054: public enum InterruptCause {
055: timeout, exiting, removed
056: };
057:
058: /**
059: * Constructor.
060: * @param target target this action works on.
061: * @param background queue manager by which this action will add events.
062: */
063: // GUI thread
064: protected Action(final Target target, final Background background) {
065: this .target = target;
066: this .background = background;
067: setType("action");
068: }
069:
070: /**
071: * Constructor.
072: * @param none.
073: */
074: // GUI thread
075: protected Action() {
076: setType("action");
077: target = null;
078: background = null;
079: }
080:
081: /**
082: * Sets the target.
083: * @param target target.
084: * @return void.
085: */
086: public void setTarget(final Target target) {
087: this .target = target;
088: }
089:
090: /**
091: * Sets the background manager.
092: * @param background background manager.
093: * @return void.
094: */
095: public void setBackground(final Background background) {
096: this .background = background;
097: }
098:
099: /**
100: * Called to inform about the current GUI.
101: * @param gui current GUI instance.
102: * @return void.
103: */
104: protected void initialize(final GUI gui) {
105: super .initialize(gui);
106: if (gui != null)
107: setImageExec();
108: }
109:
110: /**
111: * Returns the associated target.
112: * @param none.
113: * @return Target associated target.
114: */
115: // Queue thread
116: protected Target getTarget() {
117: return target;
118: }
119:
120: /**
121: * Returns the preferred queue.
122: * @param none.
123: * @return String preferred queue.
124: */
125: // any thread
126: public String getQueueName() {
127: return "standard";
128: }
129:
130: /**
131: * Returns the timeout associated with this action.
132: * @param none.
133: * @return long timeout.
134: */
135: // any thread
136: public long getMaxDelay() {
137: return 0;
138: }
139:
140: /**
141: * Asks this action to stop rapidely.
142: * @param cause cause.
143: * @return void.
144: * @throws IOException IO exception.
145: */
146: // main & Background threads
147: public void interrupt(final InterruptCause cause)
148: throws IOException {
149: }
150:
151: /**
152: * Asks this action to do its job.
153: * @param none.
154: * @return void.
155: * @throws IOException IO exception.
156: * @throws InterruptedException exception.
157: */
158: // Queue thread
159: public void invoke() throws IOException, InterruptedException {
160: }
161:
162: /**
163: * Checks that another visual element type can be under this one.
164: * @param visual_element element to check against.
165: * @return boolean true if this element can be under this action.
166: */
167: public boolean canManageThisChild(final VisualElement visual_element) {
168: return false;
169: }
170:
171: /**
172: * Called when this element is being removed.
173: * @param none.
174: * @return void.
175: */
176: protected void disposed() {
177: super .disposed();
178: try {
179: // PB : this.invoke() pourra qd meme etre invoqué 1 fois ou peut meme etre en cours d'invocation
180: background.removeActionQueue(this );
181: } catch (final GeneralException ex) {
182: log.error("Exception", ex);
183: }
184: }
185: }
|