rendered paste bodypackage no.ntnu.fp.net.seperat.server;import java.io.EOFException;import java.io.IOException;import java.io.PrintStream;import java.io.PrintWriter;import java.net.ConnectException;import java.net.InetAddress;import java.net.SocketTimeoutException;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Iterator;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JOptionPane;import no.ntnu.fp.net.IPTools;import no.ntnu.fp.net.admin.Log;import no.ntnu.fp.net.admin.Settings;import no.ntnu.fp.net.co.Connection;import no.ntnu.fp.net.co.ConnectionImpl;import no.ntnu.fp.net.co.SimpleConnection;import no.ntnu.fp.net.separat.client.ChatClient;/** * @author Geir Vevle * * This is a Chat srver made only as a experiment, but we use it anyways :P */public class ChatServer extends JFrame { public static boolean SIMPLE_CONNECTION = false; public static int LYTTEPORT = 4525; public static String LOKALIP = IPTools.findPublicIPv4(); private Connection server;// private String addressServer = "localhost"; private String addressServer = IPTools.findPublicIPv4(); private ArrayList users; private int listenPort = LYTTEPORT; private boolean run = true; private InetAddress localAddress; private JList userlist = new JList(); //Lagrer info om hver og en bruker private class User { public String name; private RecieveThread recieveThread; public Connection conn; public User(String name, Connection conn) { this.name = name; this.conn = conn; recieveThread = new RecieveThread(); recieveThread.start(); } private class RecieveThread extends Thread { public boolean run = true; public void run() { run = true; while (run) { try { User.this.recieve(User.this.conn.receive()); } catch (ConnectException e) { e.printStackTrace(); }catch (EOFException e) { try { User.this.conn.close(); recieveThread.run = false; recieveThread = null; ChatServer.this.users.remove(this); ChatServer.this .broadcast(ChatServer.this.getUsers().toString()); try { this.finalize(); } catch (Throwable ee) { // TODO Auto-generated catch block ee.printStackTrace(); } } catch (Exception ee) { ee.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } } } private void recieve(String mess) { if (mess.length() == 0) { } else if (mess.equals(new String(name + " is closing"))) { System.out.println(name + " is closing");// try {// conn.close();// } catch (IOException e1) {// // TODO Auto-generated catch block// e1.printStackTrace();// }// ChatServer.this.users.remove(this);// ChatServer.this// .broadcast(ChatServer.this.getUsers().toString());// try {// this.finalize();// } catch (Throwable e) {// // TODO Auto-generated catch block// e.printStackTrace();// } } else if (mess.substring(0, 1).equals("/")) { if (mess.substring(1, 9).equals("newName:")) { name = mess.substring(10, mess.length()); ChatServer.this.broadcast(ChatServer.this.getUsers() .toString()); } } else { ChatServer.this.broadcast(mess); } } private void send(String mess) { try { conn.send(mess); } catch (ConnectException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void broadcast(String mess) { for (Iterator iter = users.iterator(); iter.hasNext();) { User element = (User) iter.next(); element.send(mess); } } public ChatServer(int port) { listenPort = port; setTitle("Server"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(300, 300); getContentPane().add(userlist); setVisible(true); } public void startServer() { try {// localAddress = InetAddress.getByName("localhost");// TODO: Sjekke om dette er riktig! localAddress = InetAddress.getByName(IPTools.findPublicIPv4()); } catch (UnknownHostException e1) { e1.printStackTrace(); } users = new ArrayList(); if (SIMPLE_CONNECTION) server = new SimpleConnection(listenPort); else server = new ConnectionImpl(listenPort); Thread listener = new Thread() { private Connection newConn; private String message = ""; public void run() { while (true) { try { System.out.println("Server lytter p:" + listenPort); Log.writeToLog("Server lytter igjen", "ChatServer::startserver"); newConn = server.accept(); message = newConn.receive(); if (message.substring(0, 6).equals("Hello:")) { User newUser; System.out.println("Fikk inn connection fra: " + message.substring(6, message.length())); users.add(newUser = new User(message.substring(6, message.length()), newConn)); broadcast(getUsers().toString()); } } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("Noe gikk galt, forsk igjen."); for(int i=0; i<e.getStackTrace().length ; i++) { Log.writeToLog("Something went wrong:"+e.getStackTrace()[i].toString(), "ChatServer::startserver"); } e.printStackTrace(); } } } }; listener.start(); } private void newMessage(String message, String from) { for (int i = 0; i < users.size(); i++) { } } private ArrayList getUsers() { ArrayList userList = new ArrayList(); for (int i = 0; i < users.size(); i++) { userList.add(((User) users.get(i)).name); } userlist.setListData(userList.toArray()); return userList; } public static void main(String[] args) { String address; int port; Log.setLogName("Server"); Settings settings = new Settings();// port = settings.getServerPort(); port = LYTTEPORT; SIMPLE_CONNECTION = settings.useSimpleConnection(); if (SIMPLE_CONNECTION){ System.out.println("Using SimpleConnection"); } ChatServer server = new ChatServer(port); server.startServer(); }}