network interface
This commit is contained in:
@@ -36,6 +36,13 @@
|
|||||||
<version>6.11</version>
|
<version>6.11</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>seng302</groupId>
|
||||||
|
<artifactId>racevisionGame</artifactId>
|
||||||
|
<version>2.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
package app;
|
package app;
|
||||||
|
|
||||||
|
import networkInterface.NetworkInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used when starting the matchmaking browser
|
* Used when starting the matchmaking browser
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
NetworkInterface networkInterface = new NetworkInterface();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-4
@@ -1,17 +1,23 @@
|
|||||||
package networkInterface;
|
package networkInterface;
|
||||||
|
|
||||||
import java.io.*;
|
import network.Exceptions.InvalidMessageException;
|
||||||
import java.net.*;
|
import network.MessageDecoders.HostedGamesRequestDecoder;
|
||||||
|
import network.Messages.HostGamesRequest;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.DatagramPacket;
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the output for the network for
|
* Holds the output for the network for
|
||||||
*/
|
*/
|
||||||
public class InInterface {
|
public class NetworkInterface {
|
||||||
private DatagramSocket serverSocket;
|
private DatagramSocket serverSocket;
|
||||||
private byte[] receiveData = new byte[1024];
|
private byte[] receiveData = new byte[1024];
|
||||||
private byte[] sendData = new byte[1024];
|
private byte[] sendData = new byte[1024];
|
||||||
|
|
||||||
public InInterface(){
|
public NetworkInterface(){
|
||||||
try {
|
try {
|
||||||
this.serverSocket = new DatagramSocket(3779);
|
this.serverSocket = new DatagramSocket(3779);
|
||||||
|
|
||||||
@@ -29,6 +35,14 @@ public class InInterface {
|
|||||||
serverSocket.receive(receivePacket);
|
serverSocket.receive(receivePacket);
|
||||||
|
|
||||||
//decode and update table
|
//decode and update table
|
||||||
|
HostedGamesRequestDecoder decoder = new HostedGamesRequestDecoder();
|
||||||
|
HostGamesRequest newKnownGames;
|
||||||
|
try{
|
||||||
|
newKnownGames = (HostGamesRequest) decoder.decode(receivePacket.getData());
|
||||||
|
System.out.println(newKnownGames.getKnownGames().get(0));
|
||||||
|
}catch (InvalidMessageException e){
|
||||||
|
System.err.println("Message received that is not a hostedGamesRequest packet");
|
||||||
|
}
|
||||||
|
|
||||||
//client ip and port
|
//client ip and port
|
||||||
InetAddress IPAddress = receivePacket.getAddress();
|
InetAddress IPAddress = receivePacket.getAddress();
|
||||||
@@ -36,6 +50,7 @@ public class InInterface {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// String capitalizedSentence = sentence.toUpperCase();
|
// String capitalizedSentence = sentence.toUpperCase();
|
||||||
// sendData = capitalizedSentence.getBytes();
|
// sendData = capitalizedSentence.getBytes();
|
||||||
// DatagramPacket sendPacket =
|
// DatagramPacket sendPacket =
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package networkInterface;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Holds the connection to the network for output
|
|
||||||
*/
|
|
||||||
public class OutInterface {
|
|
||||||
}
|
|
||||||
@@ -5,8 +5,6 @@ import network.Messages.AC35Data;
|
|||||||
import network.Messages.HostGame;
|
import network.Messages.HostGame;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static network.Utils.ByteConverter.intToBytes;
|
import static network.Utils.ByteConverter.intToBytes;
|
||||||
|
|
||||||
@@ -25,7 +23,7 @@ public class HostGameMessageEncoder implements MessageEncoder{
|
|||||||
//Downcast
|
//Downcast
|
||||||
HostGame hostGame = (HostGame) message;
|
HostGame hostGame = (HostGame) message;
|
||||||
|
|
||||||
ByteBuffer hostGameMessage = ByteBuffer.allocate(14);
|
ByteBuffer hostGameMessage = ByteBuffer.allocate(13);
|
||||||
|
|
||||||
ByteBuffer ipBytes = ByteBuffer.allocate(4);
|
ByteBuffer ipBytes = ByteBuffer.allocate(4);
|
||||||
String ip = hostGame.getIp();
|
String ip = hostGame.getIp();
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import network.Messages.HostGame;
|
|||||||
import network.Messages.HostGamesRequest;
|
import network.Messages.HostGamesRequest;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static network.Utils.ByteConverter.intToBytes;
|
import static network.Utils.ByteConverter.intToBytes;
|
||||||
|
|
||||||
@@ -26,7 +25,7 @@ public class HostedGamesRequestEncoder implements MessageEncoder{
|
|||||||
|
|
||||||
int numGames = hostGamesRequest.getKnownGames().size();
|
int numGames = hostGamesRequest.getKnownGames().size();
|
||||||
|
|
||||||
ByteBuffer hostedGamesRequestMessage = ByteBuffer.allocate(4+14*numGames);
|
ByteBuffer hostedGamesRequestMessage = ByteBuffer.allocate(4+13*numGames);
|
||||||
|
|
||||||
hostedGamesRequestMessage.put(intToBytes(numGames));
|
hostedGamesRequestMessage.put(intToBytes(numGames));
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package visualiser.network;
|
||||||
|
|
||||||
|
import network.BinaryMessageEncoder;
|
||||||
|
import network.Exceptions.InvalidMessageException;
|
||||||
|
import network.MessageEncoders.HostGameMessageEncoder;
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
|
||||||
|
import java.net.DatagramSocket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UDP interface for the matchBrowser to send out hosted game info and get in other hosts info
|
||||||
|
*/
|
||||||
|
public class MatchBrowserInterface {
|
||||||
|
DatagramSocket clientSocket;
|
||||||
|
|
||||||
|
public void sendOutGameInfo(AC35Data gameInfo){
|
||||||
|
byte[] fullMessageToSend;
|
||||||
|
try{
|
||||||
|
HostGameMessageEncoder encoder = new HostGameMessageEncoder();
|
||||||
|
byte[] message = encoder.encode(gameInfo);
|
||||||
|
BinaryMessageEncoder messageEncoder = new BinaryMessageEncoder(MessageType.HOST_GAME
|
||||||
|
,System.currentTimeMillis(), 1,(short) 14 ,message);
|
||||||
|
fullMessageToSend = messageEncoder.getFullMessage();
|
||||||
|
}catch (InvalidMessageException e){
|
||||||
|
System.err.println("HostGameMessage could not be encoded");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user