Skip to content

Commit

Permalink
Merge pull request gre#3 from dbathily/master
Browse files Browse the repository at this point in the history
Thread safe (Java version)
  • Loading branch information
gre committed Jul 14, 2012
2 parents 5120de5 + 3f1bd59 commit 2ac9131
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 1,200 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
logs
project/project
project/target
target
tmp
.history
dist
.DS_Store
*.iml
.idea/
8 changes: 0 additions & 8 deletions java/.gitignore

This file was deleted.

16 changes: 9 additions & 7 deletions java/app/models/PaintRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class PaintRoom {

public String name;
// The list of all connected painters (identified by ids)
public Map<Integer, Painter> painters = new HashMap<Integer, Painter>(); // FIXME not thread safe, use ConcurrentMap
public int counter = 0; // FIXME not thread safe, use AtomicInteger
public int connections = 0; // FIXME not thread safe, use AtomicInteger
public Map<Integer, Painter> painters = new ConcurrentHashMap<Integer, models.Painter>();
public AtomicInteger counter = new AtomicInteger(0);
public AtomicInteger connections = new AtomicInteger(0);

public PaintRoom(String name) {
this.name = name;
}

public void createPainter(final WebSocket.In<JsonNode> in, final WebSocket.Out<JsonNode> out) {
counter++;
connections++;
final int pid = counter; // the painter id
counter.incrementAndGet();
connections.incrementAndGet();
final int pid = counter.intValue(); // the painter id

// in: handle messages from the painter
in.onMessage(new F.Callback<JsonNode>() {
Expand Down Expand Up @@ -67,7 +69,7 @@ public void invoke(JsonNode json) throws Throwable {
@Override
public void invoke() throws Throwable {
painters.remove(pid);
connections--;
connections.decrementAndGet();

ObjectNode json = Json.newObject();
json.put("type", "disconnect");
Expand Down
Loading

0 comments on commit 2ac9131

Please sign in to comment.