|
| 1 | +package javagameengine.networking; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.net.ServerSocket; |
| 10 | +import java.net.Socket; |
| 11 | + |
| 12 | +class socket_handler implements Runnable { |
| 13 | + public Thread t; |
| 14 | + public ServerSocket socket; // Socket to listen on |
| 15 | + public Socket i_socket; // Start listening on socket |
| 16 | + |
| 17 | + @Override |
| 18 | + public void run() { |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + public socket_handler(String _lineup_id) |
| 23 | + { |
| 24 | + t = new Thread(this, "t_client_" + _lineup_id); |
| 25 | + t.start(); |
| 26 | + try { |
| 27 | + socket = new ServerSocket(8074); |
| 28 | + i_socket = socket.accept(); |
| 29 | + } catch (IOException e) { |
| 30 | + throw new RuntimeException(e); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public InputStream input; |
| 35 | + public BufferedReader reader; |
| 36 | + |
| 37 | + public BufferData read_client() |
| 38 | + { |
| 39 | + String data; |
| 40 | + BufferData parsed = new BufferData(); |
| 41 | + try { |
| 42 | + input = i_socket.getInputStream(); |
| 43 | + |
| 44 | + reader = new BufferedReader(new InputStreamReader(input)); |
| 45 | + data = reader.readLine(); |
| 46 | + |
| 47 | + Gson json_parser = new Gson(); |
| 48 | + |
| 49 | + parsed = json_parser.fromJson(data, BufferData.class); |
| 50 | + } catch (IOException e) { |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + return parsed; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +public class Tcp_server extends Thread { |
| 58 | + |
| 59 | + public Tcp_server() |
| 60 | + { |
| 61 | + Tcp_server thread = new Tcp_server(); |
| 62 | + thread.start(); |
| 63 | + } |
| 64 | + |
| 65 | + // Connected lineup ** List of connected clients ** |
| 66 | + public int lineup_id = 0; |
| 67 | + |
| 68 | + public String client_joined() |
| 69 | + { |
| 70 | + lineup_id = lineup_id++; |
| 71 | + return Integer.toString(lineup_id); |
| 72 | + } |
| 73 | + |
| 74 | + public void client_left() |
| 75 | + { |
| 76 | + // We don't handle any queues. Meaning it doesn't matter what the ID is as long as they don't conflict. |
| 77 | + // So this is just a security measure to make sure that it doesn't happen. |
| 78 | + lineup_id = lineup_id++; |
| 79 | + } |
| 80 | + |
| 81 | + public void listenFor_lineup() |
| 82 | + { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + public void read_lineup() |
| 87 | + { |
| 88 | + } |
| 89 | +} |
0 commit comments