1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
| package mainMenu;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.border.*;
import Model.GameConstants;
import Model.GameData;
import Model.NetClient;
import Model.NetConfig;
import Test.Log;
import tableView.TableGUI;
/**
* A panel for launch & option
*/
@SuppressWarnings("serial")
public class Network extends JPanel implements GameConstants {
private JTextField address;// zone de saisie de texte
private JTextField port;
public TableGUI table;
private InetAddress serverAddress;
private JButton launchButton;
public JFrame mainmenu;
private NetworkDecision decision;
private JTextField name;
private JLabel txt;
public Socket socket;
private NetConfig netconfig;
private GameData game=null; /* init auto si pas ds le construct? */
private JButton joinButton;
private Log log;
// -----------------------------------------------------------------------------------------------------
/**
* Create a new panel.Constructeur , agence les 5 sous-zones(composants)
* selon le lyout cardinal
*
* @param mainmenu
*/
public Network(NetworkDecision netDecision,JFrame mainmenu,Log log,NetConfig netconfig)
{
this.log=log;
this.netconfig=netconfig;
this.mainmenu=mainmenu;
this.decision=netDecision;
setLayout(new GridLayout(3,1));
JLabel title = new JLabel(" Configuration réseau");
title.setFont(new Font("Impact", Font.PLAIN, 24));
add(title);
add(launchPanel());
add(joinPanel());
this.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.GRAY));
}
/**
* -----------------------------------------------------------------------------------
* A panel containing the "Finished" button. Methode qui retourne un
* container( comme les autres); Pq une metode = trop style ^^,
* non juste une faeon de faire comme une autre
*/
private JPanel launchPanel()
{
launchButton = new JButton("Heberger");
launchButton.addMouseListener(new BullListener("Heberge un jeu."));
//launchButton.setPreferredSize(new Dimension(400, 70)); // ok
launchButton.addActionListener(new DoneListener());
JPanel launchPanel = new JPanel();
launchPanel.add(launchButton);
return launchPanel;
}
/**
* -----------------------------------------------------------------------------------
* A panel containing the "Finished" button. Methode qui retourne un
* container( comme les autres); Pq une metode = trop style ^^,
* non juste une faeon de faire comme une autre
*/
private JPanel joinPanel()
{
address = new JTextField(15);
address.setText("192.168.2.3");
port = new JTextField(5);
port.setText(""+PORT);
joinButton = new JButton("Connexion directe");
joinButton.addMouseListener(new BullListener("Joindre une partie reseau. !"));
//joinButton.setPreferredSize(new Dimension(400, 70)); // ok
joinButton.addActionListener(new joinListener());
name = new JTextField(8);
name.setText("Your name");
txt = new JLabel("IP address");
JPanel joinPanel = new JPanel();
joinPanel.add(txt);
joinPanel.add(address);
joinPanel.add(port);
joinPanel.add(name);
joinPanel.add(joinButton);
return joinPanel;
}
/*-----------------------------------------------------------------------------------*/
public void setVisible(Boolean b) {
this.setVisible(b);
}
public boolean isOn() {
return this.isVisible();
}
public void setPort(JTextField port) {
this.port = port;
}
public JTextField getPort() {
return port;
}
private void netBug()
{
JFrame frame = new JFrame("Message Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(frame, "Erreur Réseau ...", "Attention",
JOptionPane.WARNING_MESSAGE);
return;
}
private void clientLaunch()
{
game.setNameB(name.getText().concat(SPACES).substring(0, NAME_LENGTH));
game.setCpu(false);
table = new TableGUI(game, mainmenu,netconfig);
}
//-----------------------------------------------------------------------------------------------------
/**
* ------------------------------------------------------------------------------
* An object that
* handles a press of the Launch button.
*/
private class DoneListener implements ActionListener, GameConstants
{
public void actionPerformed(ActionEvent e)
{
decision.setVisible(true);
Network.this.setVisible(false);
}
}
/*
* Direct Connexion
*/
public class joinListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
/*Rend l'interface réactive durant la connection, car doit attendre réponse server*/
/* après X secondes, doit s'arrêter*/
new Thread(new Connecter(),"ConnecterClient").start();
joinButton.setEnabled(false);
txt.setText("Waiting respons ...");
}
}
public static byte[] getIP(String str)
{
String[] temp = str.split("[.]");
if (temp.length!=4)
{
return null;
}
byte[] res = new byte[4];
for(int i=0; i<4; i++)
{
try
{
res[i]=new Integer(temp[i]).byteValue();
}
catch(NumberFormatException e)
{
IPerror();
return null;
}
}
return res;
}
/*
* Test
*/
private static void IPerror()
{
JFrame frame = new JFrame("Message Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(frame, "Adresse IP invalide", "Attention",
JOptionPane.WARNING_MESSAGE);
return;
}
/*
* TESTS
public static void main(String[] args)
{
String test = null;
try {
test = InetAddress.getLocalHost().getHostAddress() ;
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(test);
byte[] add = getIP(test);
try {
System.out.println(InetAddress.getByAddress(add).getHostAddress().equals(test));
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0; i<add.length; i++)
{
System.out.println(add[i]);
}
}*/
private class Connecter implements Runnable,GameConstants
{
public void run()
{
/* Récup et valide l'addresse du champ ip*/
byte[] ip= getIP(address.getText());
if (ip==null) { IPerror();}
try
{
serverAddress =InetAddress.getByAddress(ip) ;
}
catch (UnknownHostException e)
{
netBug();
}
/* Ouvre un socket vers le serveur*/
try
{
socket = new Socket(serverAddress,PORT);
}
catch (IOException e1)
{
JFrame frame = new JFrame("Message Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(frame, "Aucune réponse serveur ...", "Attention",
JOptionPane.WARNING_MESSAGE);
txt.setText("IP Server :");
joinButton.setEnabled(true);
return;
}
/*init interfaceur réseau*/
netconfig.setClient(true);
netconfig.setSocket(socket);
log.write("Client Network Connecter :",socket+"");
netconfig.openStreams();
/* Envoi la requete = son nom*/
String request = name.getText();
netconfig.writeObj(request);
/* Attend ensuite la réponse du serveur*/
while(game==null)
{
game = (GameData) netconfig.readObj(); /* Instruction bloquante, pas besoin de While?*/
System.out.println("while");
}
txt.setText("IP Server :");
clientLaunch();
joinButton.setEnabled(true);
}
}
} |
Partager