-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from stzups/temp
v0.3.2 Rework protocol, document serialization
- Loading branch information
Showing
61 changed files
with
1,148 additions
and
654 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
board-room/src/main/java/net/stzups/board/data/objects/canvas/Canvas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package net.stzups.board.data.objects.canvas; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import net.stzups.board.data.objects.canvas.object.CanvasObject; | ||
import net.stzups.board.data.objects.canvas.object.CanvasObjectType; | ||
import net.stzups.board.data.objects.canvas.object.CanvasObjectWrapper; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Canvas { | ||
private Map<CanvasObjectType, Map<Short, CanvasObject>> canvasObjects = new HashMap<>(); | ||
|
||
public Canvas() { | ||
|
||
} | ||
|
||
/** | ||
* Deserializes canvas from db | ||
*/ | ||
public Canvas(ByteBuf byteBuf) { | ||
for (int i = 0; i < byteBuf.readUnsignedByte(); i++) { | ||
CanvasObjectType canvasObjectType = CanvasObjectType.valueOf(byteBuf.readUnsignedByte()); | ||
Map<Short, CanvasObject> map = new HashMap<>(); | ||
canvasObjects.put(canvasObjectType, map); | ||
for (int j = 0; j < byteBuf.readUnsignedShort(); j++) { | ||
map.put(byteBuf.readShort(), CanvasObject.getCanvasObject(canvasObjectType, byteBuf)); | ||
} | ||
} | ||
} | ||
|
||
public void update(Map<CanvasObjectType, Map<Short, CanvasObjectWrapper>> updateCanvasObjects) { | ||
for (Map.Entry<CanvasObjectType, Map<Short, CanvasObjectWrapper>> entry : updateCanvasObjects.entrySet()) { | ||
Map<Short, CanvasObject> map = canvasObjects.get(entry.getKey()); | ||
if (map == null) { | ||
map = new HashMap<>(); | ||
} | ||
for (Map.Entry<Short, CanvasObjectWrapper> entry1 : entry.getValue().entrySet()) { | ||
map.put(entry1.getKey(), entry1.getValue().getCanvasObject()); | ||
} | ||
} | ||
} | ||
|
||
public void delete(Canvas canvas) { | ||
delete(canvas.canvasObjects); | ||
} | ||
|
||
public void delete(Map<CanvasObjectType, Map<Short, CanvasObject>> deleteCanvasObjects) { | ||
for (Map.Entry<CanvasObjectType, Map<Short, CanvasObject>> entry : deleteCanvasObjects.entrySet()) { | ||
Map<Short, CanvasObject> map = canvasObjects.get(entry.getKey()); | ||
if (map == null) { | ||
System.out.println("cant delete " + entry.getKey() + ", its already gone"); | ||
} else { | ||
for (Map.Entry<Short, CanvasObject> entry1 : entry.getValue().entrySet()) { | ||
map.remove(entry1.getKey()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void clear() { | ||
canvasObjects.clear(); | ||
} | ||
|
||
public void serialize(ByteBuf byteBuf) { | ||
byteBuf.writeByte((byte) canvasObjects.size()); | ||
for (Map.Entry<CanvasObjectType, Map<Short, CanvasObject>> entry : canvasObjects.entrySet()) { | ||
byteBuf.writeByte((byte) entry.getKey().getId()); | ||
byteBuf.writeShort((short) entry.getValue().size()); | ||
for (Map.Entry<Short, CanvasObject> entry1 : entry.getValue().entrySet()) { | ||
byteBuf.writeShort(entry1.getKey()); | ||
entry1.getValue().serialize(byteBuf); | ||
} | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return canvasObjects.isEmpty(); | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
board-room/src/main/java/net/stzups/board/data/objects/canvas/CanvasObject.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
board-room/src/main/java/net/stzups/board/data/objects/canvas/CanvasObjects.java
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
board-room/src/main/java/net/stzups/board/data/objects/canvas/Point.java
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
board-room/src/main/java/net/stzups/board/data/objects/canvas/object/CanvasObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package net.stzups.board.data.objects.canvas.object; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import net.stzups.board.data.objects.canvas.object.objects.Shape; | ||
|
||
public class CanvasObject { | ||
private short x; | ||
private short y; | ||
|
||
public CanvasObject(ByteBuf byteBuf) { | ||
x = byteBuf.readShort(); | ||
y = byteBuf.readShort(); | ||
} | ||
|
||
public void serialize(ByteBuf byteBuf) { | ||
byteBuf.writeShort(x); | ||
byteBuf.writeShort(y); | ||
} | ||
|
||
public static CanvasObject getCanvasObject(CanvasObjectType canvasObjectType, ByteBuf byteBuf) { | ||
CanvasObject canvasObject; | ||
switch (canvasObjectType) { | ||
case SHAPE: | ||
canvasObject = new Shape(byteBuf); | ||
break; | ||
default: | ||
canvasObject = null; | ||
} | ||
return canvasObject; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...data/objects/canvas/CanvasObjectType.java → ...jects/canvas/object/CanvasObjectType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...d-room/src/main/java/net/stzups/board/data/objects/canvas/object/CanvasObjectWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.stzups.board.data.objects.canvas.object; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
public class CanvasObjectWrapper { | ||
private byte dt; | ||
private CanvasObject canvasObject; | ||
|
||
public CanvasObjectWrapper(CanvasObjectType canvasObjectType, ByteBuf byteBuf) { | ||
dt = byteBuf.readByte(); | ||
canvasObject = CanvasObject.getCanvasObject(canvasObjectType, byteBuf); | ||
} | ||
|
||
public CanvasObject getCanvasObject() { | ||
return canvasObject; | ||
} | ||
|
||
public void serialize(ByteBuf byteBuf) { | ||
byteBuf.writeByte(dt); | ||
canvasObject.serialize(byteBuf); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
board-room/src/main/java/net/stzups/board/data/objects/canvas/object/objects/Shape.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.stzups.board.data.objects.canvas.object.objects; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import net.stzups.board.data.objects.canvas.object.CanvasObject; | ||
|
||
public class Shape extends CanvasObject { | ||
private int width; | ||
private int height; | ||
|
||
public Shape(ByteBuf byteBuf) { | ||
super(byteBuf); | ||
width = byteBuf.readUnsignedShort(); | ||
height = byteBuf.readUnsignedShort(); | ||
} | ||
|
||
public void serialize(ByteBuf byteBuf) { | ||
super.serialize(byteBuf); | ||
byteBuf.writeShort((short) width); | ||
byteBuf.writeShort((short) height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.