network interface

This commit is contained in:
hba56
2017-08-30 12:13:17 +12:00
parent ca0a3f2a8b
commit b2bd4fdc1c
7 changed files with 66 additions and 16 deletions
+7
View File
@@ -36,6 +36,13 @@
<version>6.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>seng302</groupId>
<artifactId>racevisionGame</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<properties>
+5
View File
@@ -1,7 +1,12 @@
package app;
import networkInterface.NetworkInterface;
/**
* Used when starting the matchmaking browser
*/
public class Main {
public static void main(String[] args) {
NetworkInterface networkInterface = new NetworkInterface();
}
}
@@ -1,17 +1,23 @@
package networkInterface;
import java.io.*;
import java.net.*;
import network.Exceptions.InvalidMessageException;
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
*/
public class InInterface {
public class NetworkInterface {
private DatagramSocket serverSocket;
private byte[] receiveData = new byte[1024];
private byte[] sendData = new byte[1024];
public InInterface(){
public NetworkInterface(){
try {
this.serverSocket = new DatagramSocket(3779);
@@ -29,6 +35,14 @@ public class InInterface {
serverSocket.receive(receivePacket);
//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
InetAddress IPAddress = receivePacket.getAddress();
@@ -36,6 +50,7 @@ public class InInterface {
// String capitalizedSentence = sentence.toUpperCase();
// sendData = capitalizedSentence.getBytes();
// 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 java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import static network.Utils.ByteConverter.intToBytes;
@@ -25,7 +23,7 @@ public class HostGameMessageEncoder implements MessageEncoder{
//Downcast
HostGame hostGame = (HostGame) message;
ByteBuffer hostGameMessage = ByteBuffer.allocate(14);
ByteBuffer hostGameMessage = ByteBuffer.allocate(13);
ByteBuffer ipBytes = ByteBuffer.allocate(4);
String ip = hostGame.getIp();
@@ -6,7 +6,6 @@ import network.Messages.HostGame;
import network.Messages.HostGamesRequest;
import java.nio.ByteBuffer;
import java.util.List;
import static network.Utils.ByteConverter.intToBytes;
@@ -26,7 +25,7 @@ public class HostedGamesRequestEncoder implements MessageEncoder{
int numGames = hostGamesRequest.getKnownGames().size();
ByteBuffer hostedGamesRequestMessage = ByteBuffer.allocate(4+14*numGames);
ByteBuffer hostedGamesRequestMessage = ByteBuffer.allocate(4+13*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");
}
}
}