gameServer.java:
import java.util.ArrayList;
public class gameServer{
public static server server;
public static gameRunner gameRunner;
public static ArrayList<packet> packets = new ArrayList<packet>();
public static void main(String[] args) throws Exception {
server = new server();
server.startServer(5050);
Runnable listenServer = new Runnable() {
public void run() {
while(true) {
packets.add(server.getPacket());
}
}
};
new Thread(listenServer).start();
gameRunner = new gameRunner();
while(true) {
if(packets.size() <= 0)
continue;
gameRunner.executeMessage(packets.get(0));
packets.remove(0);
}
}
}
server.js:
import java.net.*;
import java.util.Date;
public class server {
public static DatagramSocket socketServer;
private Date date = new Date();
public void startServer (int port) throws SocketException {
socketServer = new DatagramSocket(port);
System.out.println("Started Server on port: " + port);
}
public void sendPacket (packet packet) {
String message = null;
for(int i = 0; i < packet.header.length; i++) {
message += packet.header[i] + "\r\n";
}
message += "\r\n" + packet.message;
String[] info = packet.address.split(":");
try {
sendPacket(message, (Inet4Address)InetAddress.getByName(info[0]), Integer.parseInt(info[1]));
} catch (Exception e) {
System.out.println("failed to determine host");
}
}
}
gameRunner.js:
import java.util.ArrayList;
import java.util.Date;
public class gameRunner {
public static ArrayList<object> objects = new ArrayList<object>();
public static player[] players = new player[1000];
public Date date = new Date();
Runnable updatePlayers = new Runnable() {
public void run () {
while(true) {
long StartTime = date.getTime();
for(int i = 0; i < players.length; i++) {
if(players[i] == null)
continue;
players[i].updatePos();
}
while(StartTime + 100 < date.getTime()) { }
}
}
};
Runnable sendPlayerPackets = new Runnable () {
public void run () {
while(true) {
parent.gameServer.sendPacket(player[0].packet); //<<<----
}
}
};
}
I am new to classes, abstract, inherent and such. I need to be able to access socketServer object in gameServer from gameRunner to send a message. I am not sure how to do this, thanks in advance.
EDIT1 guessed right 🙂
Some words on design:
Most of these statics are horrible, you should only make properties static when they are part of the class such as default values, Singleton business and when you are really drunk and lazy – but that’s a different topic (you will get the hang of that if you keep coding)
Actually you should not make properties public, they should always be private or protected, with a public getter/setter method – that way you can restrict access to the properties and can control what happens on a get or set. (but this tends to be very tedious in java)
Now, keeping with the statics you could easily send a message like this (in sendPlayerPackets.run()):
server.sendPacket(player[0].packet);To pass the server along to the gameRunner you would do:
(in gameRunner.java)
and in gameServer.java:
old part
Purely on instinct I guess you want this:
In short: you have to pass the parent to the child for it to be able to access it.
Note that in this example B has to be initialized before C, to get around this you would need events i guess. Anyways, the idea does not seem well defined, you should think about what you want to accomplish again (and tell us).