View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.magicproject.network;
20  
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.Socket;
26  
27  import net.sf.magicproject.deckbuilder.Deck;
28  import net.sf.magicproject.deckbuilder.DeckReader;
29  import net.sf.magicproject.stack.StackManager;
30  import net.sf.magicproject.tools.Configuration;
31  import net.sf.magicproject.ui.MagicUIComponents;
32  import net.sf.magicproject.ui.component.LoaderConsole;
33  
34  import org.apache.commons.io.IOUtils;
35  
36  /***
37   * This class is representating a client or a serveur, so contains nickname,
38   * connection's port and streams of opened socket.
39   * 
40   * @since 0.2d
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   */
43  public abstract class NetworkActor extends Thread {
44  
45  	/***
46  	 * Creates a new instance of NetworkActor <br>
47  	 * 
48  	 * @param deck
49  	 *          the dof this network component.
50  	 * @param passwd
51  	 *          is the password needed to connect to this play
52  	 */
53  	protected NetworkActor(Deck deck, char[] passwd) {
54  		this.nickName = StackManager.PLAYERS[0].getNickName();
55  		this.deck = deck;
56  		this.passwd = passwd;
57  		this.port = Configuration.getInt("port", 1299);
58  		ConnectionManager.enableConnectingTools(true);
59  		bigPipe = null;
60  		cancelling = false;
61  		freePlaces();
62  	}
63  
64  	/***
65  	 * Initialize the pipe.
66  	 */
67  	protected void initBigPipe() {
68  		if (bigPipe != null) {
69  			// is already running!
70  			return;
71  		}
72  		MagicUIComponents.chat = new MChat();
73  		bigPipe = new MBigPipe(inBin, outBin, MagicUIComponents.chat);
74  		bigPipe.start();
75  	}
76  
77  	/***
78  	 * end this server, close all connexions with connected clients
79  	 */
80  	public void closeConnexion() {
81  		MagicUIComponents.timer.stop();
82  		cancelling = true;
83  		try {
84  			// close stream
85  			if (clientSocket != null) {
86  				clientSocket.close();
87  			}
88  			IOUtils.closeQuietly(outBin);
89  			IOUtils.closeQuietly(inBin);
90  			// free pointers
91  		} catch (Exception e) {
92  			// Nothing to do
93  		}
94  		outBin = null;
95  		inBin = null;
96  	}
97  
98  	/***
99  	 * flush the buffer or the current OutputStream
100 	 */
101 	public void flush() {
102 		try {
103 			outBin.flush();
104 		} catch (IOException e) {
105 			throw new InternalError("when flush outBin stream");
106 		}
107 	}
108 
109 	/***
110 	 * send the specified array of int to the opponent
111 	 * 
112 	 * @param msg
113 	 *          is the message to send
114 	 */
115 	public void send(int... msg) {
116 		bigPipe.sendGameMessage(msg);
117 	}
118 
119 	/***
120 	 * cancel the join/create action, close current connections and set the main
121 	 * frame visible.
122 	 */
123 	public void cancelConnexion() {
124 		closeConnexion();
125 		freePlaces();
126 		LoaderConsole.endTask();
127 	}
128 
129 	/***
130 	 * Remove from all places the existing components and set to zero the mana
131 	 * pools.
132 	 */
133 	private void freePlaces() {
134 		StackManager.PLAYERS[0].zoneManager.reset();
135 		StackManager.PLAYERS[1].zoneManager.reset();
136 	}
137 
138 	/***
139 	 * Read and validate the opponent's deck.
140 	 * 
141 	 * @return <code>true</code> when the opponent's deck is valid.
142 	 * @throws IOException
143 	 *           If some other I/O error occurs
144 	 */
145 	protected boolean readAndValidateOpponentDeck() throws IOException {
146 		Deck opponentDeck = DeckReader.getDeck(
147 				new MInputStream(inBin));
148 		if (!DeckReader.validateDeck(MagicUIComponents.magicForm, opponentDeck,
149 				deck.getConstraint().getName())) {
150 			cancelling = true;
151 			return false;
152 		}
153 		StackManager.PLAYERS[1].zoneManager.giveCards(opponentDeck, dbStream);
154 		return true;
155 	}
156 
157 	/***
158 	 * the passord needed to be connected
159 	 */
160 	protected final char[] passwd;
161 
162 	/***
163 	 * The nickname of this actor (client or server)
164 	 */
165 	protected final String nickName;
166 
167 	/***
168 	 * the socket of this connexion
169 	 */
170 	protected Socket clientSocket;
171 
172 	/***
173 	 * Port number
174 	 */
175 	protected final int port;
176 
177 	/***
178 	 * Deck.
179 	 */
180 	protected Deck deck;
181 
182 	/***
183 	 * The optional OutputStream of deck.
184 	 */
185 	protected OutputStream outBin;
186 
187 	/***
188 	 * InputStream of deck. May be remote deck.
189 	 */
190 	protected InputStream inBin;
191 
192 	/***
193 	 * The opened stream of mdb.
194 	 */
195 	protected FileInputStream dbStream;
196 
197 	private MBigPipe bigPipe;
198 
199 	/***
200 	 * Is cancelling?
201 	 */
202 	public static boolean cancelling;
203 }