Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How do I use Ashley entity framework alongside Kryonet?  (Read 289 times)
0 Members and 1 Guest are viewing this topic.
Offline Runcible

Senior Newbie





« Posted 2014-08-22 06:10:39 »

Hi,

I've been experimenting with both the Kryonet and Ashley frameworks separately and really like both, and now I'd like to use both in a single project.

My question is how do I hook them up together? Kryonet works with listeners and there is no 'loop', but Ashley needs a loop with a delta passed into it's update method.

I've seen a few posts where people do use a loop on the server, and Kryonet's listener simply add to a queue. This queue then gets processed during every update. Is this a good way to organise things? All of the examples in the Kryonet repository simply handle everything directly in the listener itself at the time of the event.

Thanks!
Offline Mike

JGO Wizard


Medals: 84
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #1 - Posted 2014-08-22 06:46:19 »

Hi,

I read a recommendation from Nate a couple of days ago that explained that due to the listener also does the (de)serializing of the objects he recommended to pass it off to another thread on busy network applications. I changed my code around to use another thread and it resolved a couple of queuing issues as well, so I'd say go for it! Smiley

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Runcible

Senior Newbie





« Reply #2 - Posted 2014-08-22 09:12:25 »

Hi Mike,

Thanks for that reply. So you just have a loop running in it's own thread which processes the queue? Would it be possible to post any code snippet of your loop setup?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mike

JGO Wizard


Medals: 84
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #3 - Posted 2014-08-22 12:26:56 »

Hi,

Sure, there is probably a much nicer way to do it though, but this works fine for me. As I'm not for-eaching the arraylist I didn't see any issues so far of not synchronizing it.

I'm sure there are a lot of people on the forum that knows a much neater way to do it though Smiley

Mike

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
public class HandleMessageThread extends Thread {

    private boolean threadRunning = true; //Check if the thread should keep running

    private final static ArrayList<Message> receivedMessages = new ArrayList<Message>();

    @Override
    public void run() {
        while (isAlive() && threadRunning) {
            try {
                while (receivedMessages.size() > 0) {
                    Message message = null;
                    try {
                        message = receivedMessages.get(0);
                        recievedMessage(message.getConnection(), message.getMessage());
                        receivedMessages.remove(0);
                    } catch (final Throwable t) {
                        if (message != null) {
                            Main.getKryoClient().submitError("Error processing message " + message.getMessage().toString(), t);
                        } else {
                            Main.getKryoClient().submitError("Error processing message", t);
                        }
                        receivedMessages.remove(0);
                    }
                }
            } catch (final Throwable e) {
                Main.getKryoClient().submitError("Executing message", e);
            }
            try {
                Thread.sleep(10);
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void recievedMessage(Connection c, Object p) {
        if (p instanceof SessionReturnMessage) {
            SessionReturn.process((SessionReturnMessage)p);
        } else if (p instanceof PingMessage) {
            [...]
        } else {
            Main.getKryoClient().submitError("Cannot find method: " + p.toString());
        }
    }

    public void stopThread() {
        threadRunning = false;
    }

    public void received(Connection c, Object p) {
        receivedMessages.add(new Message(c, p));
    }
}

public class Message {

    private final Connection connection;

    private final Object message;

    public Message(Connection c, Object p) {
        connection = c;
        message = p;
    }

    public Connection getConnection() {
        return connection;
    }

    public Object getMessage() {
        return message;
    }

}

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2014-08-22 12:49:59 »

If you add items to an ArrayList on thread A, and get/remove them from thread B, you must synchronize access to this ArrayList, or the behavior of the ArrayList is undefined, as shown by the following example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Queue {
   public static void main(String[] args) {
      final int pushCount = 16 * 1024 * 1024;

      new Thread(new Runnable() {
         @Override
         public void run() {
            for(int i = 0; i < pushCount; i++) {
               push(Integer.valueOf(i));
            }
         }
      }).start();

      new Thread(new Runnable() {
         @Override
         public void run() {
            Set<Integer> set = new HashSet<>();
            for(int i = 0; i < pushCount; i++) {
               System.out.println("queue size: " + queue.size() + ", polled count: " + set.size());
               Integer got = poll();
               if(got == null)
                  continue;
               if(!set.add(got))
                 throw new IllegalStateException("repeated poll result!");
            }
         }
      }).start();
   }

   static final ArrayList<Integer> queue = new ArrayList<Integer>();

   public static void push(Integer s) {
      queue.add(s);
   }

   public static Integer poll() {
      return queue.isEmpty() ? null : queue.remove(0);
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
queue size: 3313, polled count: 0
queue size: 4569, polled count: 1
queue size: 4786, polled count: 2
queue size: 4989, polled count: 3
queue size: 5245, polled count: 4
queue size: 5499, polled count: 5
queue size: 5621, polled count: 6
queue size: 5902, polled count: 7
queue size: 6222, polled count: 8
queue size: 9369, polled count: 9
queue size: 9629, polled count: 10
queue size: 10100, polled count: 11
queue size: 10548, polled count: 12
queue size: 12009, polled count: 13
queue size: 12779, polled count: 14
queue size: 13314, polled count: 15
queue size: 13764, polled count: 16
queue size: 14052, polled count: 17
queue size: 14416, polled count: 18
queue size: 15165, polled count: 19
queue size: 15738, polled count: 20
queue size: 16326, polled count: 21
queue size: 16884, polled count: 22
queue size: 17444, polled count: 23
queue size: 18025, polled count: 24
queue size: 18696, polled count: 25
queue size: 19918, polled count: 26
queue size: 20622, polled count: 27
queue size: 21036, polled count: 28
queue size: 21078, polled count: 29
queue size: 21773, polled count: 30
Exception in thread "Thread-1" java.lang.IllegalStateException: repeated poll result!
   at nav.Queue$2.run(Queue.java:30)
   at java.lang.Thread.run(Thread.java:745)


Solve with:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
static final ArrayList<Integer> queue = new ArrayList<Integer>();

   public static void push(Integer s) {
      synchronized (queue) {
         queue.add(s);
      }
   }

   public static Integer poll() {
      synchronized (queue) {
         return queue.isEmpty() ? null : queue.remove(0);
      }
   }


Or much better: (among other things, due to the capped queue size)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   static final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(n);

   public static void push(Integer s) {
      while (true) {
         try {
            queue.put(s);
            break;
         }
         catch (InterruptedException e) {
            // err
        }
      }
   }

   public static Integer poll() {
      return queue.poll();
   }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mike

JGO Wizard


Medals: 84
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #5 - Posted 2014-08-22 20:11:28 »

Awesome as always Riven, guess I was just lucky that it didn't break yet. I used to use a synchronize block but thought it wasn't needed as I wasn't looping through it. I'm now using a ArrayBlockingQueue instead.

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!