Skip to content

Commit

Permalink
Merge pull request #8 from stzups/feature/drawing
Browse files Browse the repository at this point in the history
Add real time drawing and instant invites
  • Loading branch information
griffinht authored Dec 22, 2020
2 parents 3c421db + 39d48c3 commit b2b45fc
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.stzups.board.protocol.client.ClientPacketType;

import javax.naming.OperationNotSupportedException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
Expand All @@ -37,7 +38,9 @@ protected void decode(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame,
packet = new ClientPacketDraw(points);
break;
case OPEN:
packet = new ClientPacketOpen();
byte[] buffer = new byte[byteBuf.readUnsignedByte()];
byteBuf.readBytes(buffer);
packet = new ClientPacketOpen(new String(buffer, StandardCharsets.UTF_8));
break;
default:
throw new OperationNotSupportedException("Unsupported packet type " + packetType+ " while decoding");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ protected void encode(ChannelHandlerContext ctx, List<ServerPacket> serverPacket
switch (serverPacket.getPacketType()) {
case ADD_CLIENT:
case REMOVE_CLIENT:
case WRONG_ROOM:
break;
case DRAW:
case DRAW: {
ServerPacketDraw packetDraw = (ServerPacketDraw) serverPacket;
Point[] points = packetDraw.getPoints();
byteBuf.writeShort((short) points.length);
Expand All @@ -41,12 +42,14 @@ protected void encode(ChannelHandlerContext ctx, List<ServerPacket> serverPacket
byteBuf.writeShort(point.y);
}
break;
case OPEN:
}
case OPEN: {
ServerPacketOpen serverPacketOpen = (ServerPacketOpen) serverPacket;
byte[] buffer = serverPacketOpen.getId().getBytes(StandardCharsets.UTF_8);
byteBuf.writeByte((byte) buffer.length);
byteBuf.writeBytes(buffer);
break;
}
default:
throw new UnsupportedOperationException("Unsupported packet type " + serverPacket + " while encoding");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package net.stzups.board.protocol.client;

public class ClientPacketOpen extends ClientPacket {
public ClientPacketOpen() {
private String id;
public ClientPacketOpen(String id) {
super(ClientPacketType.OPEN);
this.id = id;
}

public String getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.stzups.board.protocol.server;

public class ServerPacketOpen extends ServerPacket {
String id;
private String id;

public ServerPacketOpen(String id) {
super(ServerPacketType.OPEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum ServerPacketType {
REMOVE_CLIENT(1),
DRAW(2),
OPEN(3),
WRONG_ROOM(4),
;

private static Map<Integer, ServerPacketType> packetTypeMap = new IntObjectHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.stzups.board.protocol.server;

public class ServerPacketWrongRoom extends ServerPacket {

public ServerPacketWrongRoom() {
super(ServerPacketType.WRONG_ROOM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import net.stzups.board.Board;
import net.stzups.board.protocol.client.ClientPacket;
import net.stzups.board.protocol.client.ClientPacketDraw;
import net.stzups.board.protocol.client.ClientPacketOpen;
import net.stzups.board.protocol.server.ServerPacketDraw;
import net.stzups.board.protocol.server.ServerPacketWrongRoom;

import java.util.Collections;

public class PacketHandler extends SimpleChannelInboundHandler<ClientPacket> {
private Room room;
Expand All @@ -21,19 +25,26 @@ public void channelInactive(ChannelHandlerContext ctx) {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ClientPacket packet) {
switch (packet.getPacketType()) {
case DRAW:
case DRAW: {
ClientPacketDraw clientPacketDraw = (ClientPacketDraw) packet;
client.addPoints(clientPacketDraw.getPoints());
room.sendPacketExcept(new ServerPacketDraw(client.getId(), clientPacketDraw.getPoints()), client);
break;
case OPEN:
}
case OPEN: {
if (room == null) {
room = Room.getRoom();
client = room.addClient(ctx.channel());
ClientPacketOpen clientPacketOpen = (ClientPacketOpen) packet;
room = Room.getRoom(clientPacketOpen.getId());
if (room != null) {
client = room.addClient(ctx.channel());
} else {
ctx.writeAndFlush(Collections.singletonList(new ServerPacketWrongRoom()));
}
} else {
Board.getLogger().warning(client + " tried to open a new room when it was already open");
}
break;
}
default:
throw new UnsupportedOperationException("Unsupported packet type " + packet.getPacketType() + " sent by " + client);
}
Expand Down
11 changes: 5 additions & 6 deletions board-server/src/main/java/net/stzups/board/room/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private Room(String id) {
*
* @return the created room
*/
static Room createRoom() {
private static Room createRoom() {
String id;
do {
id = String.valueOf((int) (Math.random() * Math.pow(10, ROOM_ID_LENGTH)));
Expand All @@ -63,16 +63,15 @@ static Room createRoom() {
}

/**
* for testing purposes, get the first room that already exists
* if no rooms exist, one will be made
* Gets the corresponding room for an id
*
* @return the newly created or existing room
*/
static Room getRoom() {
if (rooms.size() == 0) {
static Room getRoom(String id) {
if (id.equals("")) {
return createRoom();
} else {
return rooms.values().iterator().next();
return rooms.get(id);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,19 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
}

final boolean keepAlive = HttpUtil.isKeepAlive(request);
final String uri = request.uri();
String uri = request.uri();
/*if (uri.equals("/")) { //default directory
sendRedirect(ctx, uri + "index.html");
return;
}*/
if (uri.startsWith("/r/")) {
//room code
uri = "/index.html";
} else if (!uri.endsWith("/") && !uri.contains(".")) {
uri += ".html";
} else if (uri.endsWith("/")) {
uri += "index.html";
}
final String path = sanitizeUri(uri);
if (path == null) {
sendError(ctx, HttpResponseStatus.FORBIDDEN); //todo return not found instead
Expand All @@ -89,7 +101,8 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) thr
}

if (file.isDirectory()) {
sendRedirect(ctx, uri + ((uri.endsWith("/") ? "" : "/") + "index.html"));
sendError(ctx, HttpResponseStatus.FORBIDDEN);
//sendRedirect(ctx, uri + ((uri.endsWith("/") ? "" : "/") + "index.html"));
return;
}

Expand Down
15 changes: 15 additions & 0 deletions board-web-client/about-board.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>About | Board</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="header">
<a href="/">board</a>
<a href="/about-board">about</a>
</div>
<h1>this is a very cool website that you should totally tell all your friend about</h1>
</body>
</html>
11 changes: 11 additions & 0 deletions board-web-client/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#canvas {
display: block;
}

#canvasWrapper {
flex: 1 1 auto;
}

#inviteButton {
margin-left: auto;
}
16 changes: 8 additions & 8 deletions board-web-client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
<head>
<meta charset="UTF-8">
<title>Board | Empty board</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="index.css">
<script type="module" src="scripts/WebSocketHandler.js"></script>
<script type="module" src="scripts/Client.js"></script>
<script type="module" src="scripts/main.js"></script>
<link rel="stylesheet" href="/style.css">
<link rel="stylesheet" href="/index.css">
<script type="module" src="/scripts/WebSocketHandler.js"></script>
<script type="module" src="/scripts/Client.js"></script>
<script type="module" src="/scripts/main.js"></script>
</head>
<body>
<div class="header">
<a href="index.html">board</a>
<a href="about.html">about</a>
<a href="/">board</a>
<a href="/about-board">about</a>
<button id="inviteButton">invite</button>
</div>
<div id="canvasWrapper">
<canvas id="canvas"></canvas>
</div>
<script src="scripts/globals.js"></script>
<script src="/scripts/globals.js"></script>
</body>
</html>
16 changes: 14 additions & 2 deletions board-web-client/scripts/WebSocketHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ export default class WebSocketHandler {
offset += 1;
let roomName = new TextDecoder().decode(event.data.slice(offset, offset + length));
offset += length;
window.history.pushState(roomName, document.title, '/r/' + roomName);
console.log(roomName, length);
inviteButton.innerHTML = roomName;//todo add spinner
break;
}
case 4: {//wrong room
inviteButton.innerHTML = 'Invalid room id';//todo add spinner
break;
}
default:
console.error('unknown payload type ' + type + ', offset ' + offset + ', event ', event);
}
Expand All @@ -115,13 +120,20 @@ export default class WebSocketHandler {
}

sendOpen() {
let buffer = new ArrayBuffer(1);
let encoded = new TextEncoder().encode(document.location.href.substring(document.location.href.lastIndexOf("/") + 1));
let buffer = new ArrayBuffer(2 + encoded.length);
let dataView = new DataView(buffer);
let offset = 0;

dataView.setUint8(offset, 0);
offset += 1;

dataView.setUint8(offset, encoded.byteLength);
offset += 1;

let newBuffer = new Uint8Array(buffer);
newBuffer.set(encoded, offset);

this.send(buffer);
this.send(newBuffer);
}
}
7 changes: 6 additions & 1 deletion board-web-client/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ inviteButton.addEventListener('click', (event) => {
if (socket == null) {
socket = new WebSocketHandler();
}
});
});

let index = document.location.href.lastIndexOf("/");
if (document.location.href.substring(index - 2, index + 1) === '/r/') {
socket = new WebSocketHandler();
}

0 comments on commit b2b45fc

Please sign in to comment.