Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3
  ignore  |  Print  
  Networked Gaming  (Read 13187 times)
0 Members and 1 Guest are viewing this topic.
Offline Mr_Light

Senior Member




shiny.


« Reply #30 - Posted 2008-11-20 01:18:26 »

For turn based you can easily use event based triggering, listener etc.

To trigger events on the event dispatcher thread use Eventqueue.invokeAndWait() This should allow you to avoid most of the complexityeffort involved with multi threading. You just need to focus a bit on managing the 'remote game'(network connection). Creating your own eventqueue isn't that difficult either if you do all the drawing you self - but since it's turn based your probably not using active rendering.

Oh and looking at executors is always a good idea, though with the event based approach you won't need it on the client. server-side it might work out depending on your approach.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #31 - Posted 2008-11-20 01:54:59 »

i am using active rendering however im still relatively new to java so i dont quite understand the eventqueue you mentioned.  could you explain it a little more fully or is there a good site with some reading on this subject?
Offline Mr_Light

Senior Member




shiny.


« Reply #32 - Posted 2008-11-20 15:29:41 »

Eventqueue is a way to have  inter thread communication where no thread ends up waiting for one an other at the sacrifice of the 'sending' thread not knowing exactly when the other thread notices it.

A real life example is the mail-man and you, you might never see the mail man but you do receive mail. This is because you have a mailbox which you occasionally check. Since you don't have face to face contact you never have to wait for the other to be ready to receive. (in other words synchronise yourself with the mail-man) Other analogy would probably be 'just leave it on my desk if I'm not there'.

Depending on where you want the code(/logic) to reside you can either submit runnable's or events to a ConcurrentQueue. Or hybrid approach off-course (event containing a runnable).

It all sounds harder then it actually is, trust me.

If you want to read something I gues there is http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html (theres a link to a paper there, haven't read it, but it might give you some pointers) judging from that page the magic word is 'wait-free'.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #33 - Posted 2008-11-21 00:17:25 »

your right that does sound difficult lol.   so would i have two threads or just one?   so is eventQueue just a way of handling multiple threads without using synchro?

if not maybe a little example code would help if its not too much trouble....
Offline Mr_Light

Senior Member




shiny.


« Reply #34 - Posted 2008-11-21 08:10:26 »

code needs some work to actually show the simplicity of it - my apologies was pulling an all-nighter and at the end I though I might type out this as well.  Roll Eyes

The idea is that you can simply share the queue with both threads and safely call add() and poll() all synchronizing happens through the queue allowing to pretty much not care about thread safety.

ps. instead of positions you can also have a queue of runnables.


//edit
hmm whatever you pass on to the queue should probably either be immutable or copied, hmm need sleep.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #35 - Posted 2008-11-24 05:40:12 »

i downloaded the jar you gave but how do i run the program and read the code?  Im sorry i have never used a jar before....

also i think there has been some miscommunication because as i was thinking about it the postman and me example isnt really what im talking about.   Everything im talking about is client side.  Basically i want animations to continue while the client is waiting for info to come in.  Basically i want my animations thread to check, somewhere in the loop, to see if there is a new message and if not continue doing whatever it was doing.   

so to give an example say im using an instant messanger.    Im doing laundry but as soon as my friend sends me a message i leave and go see a movie or whatever.  i am the thread and while doing laundry i periodically check to see if there is a new message, if not i continue what i was doing, if so i alter what i was doing. The problem i run into in my program is that i cannot wait for a message while doing laundry.  I have to wait for a message to come in and then i can continue the laundry.

^^^ stupid example i know  Smiley
Offline neoskunk

Junior Member





« Reply #36 - Posted 2008-11-24 07:25:23 »

hmmm after more consideration maybe you do know what im talking about..... so is the eventQueue like a mailbox?  when one thread reads in data it writes something to the queue and the animation loop checks the queue for new info periodically?  and then if new data is recieved it acts accordingly and removes the data?

if you could somehow make eventQueue analogous to real life it might help....
Offline Mr_Light

Senior Member




shiny.


« Reply #37 - Posted 2008-11-24 16:46:09 »

hmmm after more consideration maybe you do know what im talking about..... so is the eventQueue like a mailbox?  when one thread reads in data it writes something to the queue and the animation loop checks the queue for new info periodically?  and then if new data is recieved it acts accordingly and removes the data?
Bingo.

I need to stick the source in a jar here because zip can't be attached to messages. Since a jar is basicly a zip, you can just unzip the jar. (renaming it works)

Quote
if you could somehow make eventQueue analogous to real life it might help....
Network Thread is your friend.
You are the gameloop.
EventQueue are your mobile phones.

I think you already nailed it earlier.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #38 - Posted 2008-11-24 18:38:51 »

so just to clarify on the client side i have two threads.....

the animation one looks like this?
1  
2  
3  
4  
5  
6  
public void run()
{
         animation stuff....
         check to see if new message....
         according to message status act accordingly
}


and then the second thread would look like this?

1  
2  
3  
4  
5  
public void run()
{
         in.readLine();
         write to eventQueue
}


that should work right because although the second thread will wait for data over the socket the first thread will continue to run uninteruppted?
Offline Mr_Light

Senior Member




shiny.


« Reply #39 - Posted 2008-11-25 01:04:29 »

Yes

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline neoskunk

Junior Member





« Reply #40 - Posted 2008-11-25 02:09:19 »

awesome.  thanks for the help!
Offline neoskunk

Junior Member





« Reply #41 - Posted 2008-11-25 16:35:19 »

hey what happened to that jar file?   and i am attempting to do what you said but for some reason the applet just says applet started and displays a blank white screen.
Offline Mr_Light

Senior Member




shiny.


« Reply #42 - Posted 2008-11-25 18:15:19 »

hey what happened to that jar file?   and i am attempting to do what you said but for some reason the applet just says applet started and displays a blank white screen.
applet?

  • download
  • rename to .zip
  • extract

Run server,
Run client

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #43 - Posted 2008-11-26 10:51:35 »

i know all that but the link to the download doesnt appear to me anymore.   and as for the applet thing my game is an applet but i think i figured it all out so i don't need the jar anymore.   

also one quick thing.... if you know a lot about networked gaming could you briefly summarize how lobbies are made?   just a simple one where users can chat and possibly challange each other to a game....
Offline Mr_Light

Senior Member




shiny.


« Reply #44 - Posted 2008-11-26 17:28:17 »

If you conceptualise your problem and group actions that are similar The design should be straight forward.

Technically for chat you could do the same thing but on the server aggregate any messages on a queue then at the head of the queue poll and 'broadcast' it back to every client. As for lobby like functionality depends on what you want battle.net like: separate chat separate list.  Integrated send invite messages to ppl in the chat. I suppose I could draw that up for you but that would only make you slowly more and more depended on others to come up with the design. Diminishing your usefulness for a company and making future job interviews more difficult for yourself. As questions related to these kind of problems usually is the meat on the bone(or w/e the right metaphor is.)

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #45 - Posted 2008-11-26 23:57:06 »

i was not asking you to write the code for me (but its very kind of you to offer)  nor am i ever going to work in the field of game programming.  im a mechanical engineer major at the university of texas and recently took a class on java and thought it might be fun to develop a game.

i was merely asking conceptually how lobbies are handled.  i was thinking the queue thing would be good for messages like you said however how do you handle the list of people in the lobby?  Currently with the game i have when a user connects to the server it creates an object out of that player and then it matches players up together and creates a game object and passes the two player objects as parameters.  so i was thinking since each user is already an object it wouldn't be too hard to use maybe a resizable array and as each new user connects to the server add them to the array?   

probably not the best way?   am i at least heading in the right direction making each new user a player object? 
Offline Mr_Light

Senior Member




shiny.


« Reply #46 - Posted 2008-11-27 06:32:02 »

Arrays are like udp you don't need them unless you know for an unmistakable fact that you do.

Always use an List or a Set or if you write some method that does some function that can be applied to both use a collection - since most of those methods are already made for you: use an List or Set.

Yes from a servers point of view a socket can be seen as abstraction of a connection / remote node / client / player. Since you likely want to store information more information(like name for example) wrap the socket in a object - use the highest possible abstraction and code against it. Put these in a Set and your there. No one ever said anything needs to be hard or complex to work, so yes that's the right direction.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #47 - Posted 2008-11-27 09:41:51 »

so if you had to suggest one would you use set or list?  whats the difference?   

you already addressed my next question and that was giving each player a username.   
this is what my ShotPlayer object looks like right now. 

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  
import java.io.*;
import java.net.*;

class ShotPlayer extends SocketConnect
{
   private ShotDaemon daemon = null;

   public ShotPlayer(ShotDaemon server, Socket sock)
   {
      super(sock);
      daemon = server;
   }

   public void run()
   {
      daemon.waitForGame(this).playGame(this);
   }

   public void closeConnection()
   {
      super.closeConnection();
      if(outStream != null)
      {
         send("GAMEOVER");
      }
   }
}


whats the best way to link a name to a particular socket?   have the server accept the username from the client and then create another object out of the shotplayer and the username? <<< doesnt seem like the best way but was what i was thinking.
Offline Mr_Light

Senior Member




shiny.


« Reply #48 - Posted 2008-11-27 18:06:23 »

Difference between a Set and a List is the same as the mathematical interpretation.

They are both collections, Set is by default unordered and contains no duplicates (SortedSet is an ordered one without duplicates, equality is defined by the equals() method defined in object (override where necessary together with hashcode() -> why they are linked check google) with a List the determining factor is the order and permits duplicates.

Whether null element is allowed is dependent on the implementation.

Implementations to use as defaults are as follows:
List - ArrayList
Set - HashSet
SortedSet - TreeSet
Don't change them unless profiling dictates that they should be changed.

As for your code, in this particular case I would go for composition. (just google Composition versus Inheritance)

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #49 - Posted 2008-12-04 07:03:25 »

i think im having a bit of a problem with the event dispatcher.  im trying to create my lobby with chat and so far i have a text field for the user to type messages in and a text area that displays all messages and a button to send messages with.  However, after the client has recieved the message from the server i cannot get it to display with the textarea.append(String) method. i have tried researching it on google but it doesn't make much sense to me. 

any advice?   i know for a fact the problem is not the server because if append is called from a button click it will work but not when called in a run method.
Online CommanderKeith
« Reply #50 - Posted 2008-12-04 07:31:53 »

It's probably an event dispatch thread (EDT) problem - when called from the button press actionPerformed method, the code is run from the EDT, but in your own run method it's not.

Try running the code in SwingUtilities.invokeLater(new Runnable(){public void run(){code...}});

Offline neoskunk

Junior Member





« Reply #51 - Posted 2008-12-04 21:09:44 »

ok but im not using swing im just using regular textField and textArea.....

so the method you mentioned i put in my run method?
Offline neoskunk

Junior Member





« Reply #52 - Posted 2008-12-04 21:20:13 »

ok i got it to work using what you said.... does it matter that im not using swing though?  is there a better method to use?
Offline Mr_Light

Senior Member




shiny.


« Reply #53 - Posted 2008-12-05 02:58:49 »

ok i got it to work using what you said.... does it matter that im not using swing though?  is there a better method to use?

That method directly calls java.awt.EventQueue Swing utilities just has some additional handy methods. - I wouldn't be put off too much by swing being in the name.

Is there a particular reason your sticking to awt?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #54 - Posted 2008-12-05 21:27:06 »

no not really.  i was actually thinking of switching to swing but i think when i initially started making a game some people on this forum suggested i stick with awt.  Would you suggest using swing?

I know swing components are supposed to be lightweight and there appearance is platform independent so it does seem like i should switch.
Offline Mr_Light

Senior Member




shiny.


« Reply #55 - Posted 2008-12-06 05:07:59 »

Well the general rule of tumb has been that if you use active rendering you should stay away swing.

Dependence on how your gui is set-up. If the components are going to be in your in your active rendering bit I would chose neither as far as widgets are concerned. Make your own as it's probably less work and ppl don't expect a game to integrate with an OS anyway. I mean games are suppose to be fun - OS's are dull. the only thing you should perhaps pay attention to is that those self written widgets should grow as the resolution increases. But since those aren't 'part' of your game you can just draw them bigger.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #56 - Posted 2008-12-06 08:43:05 »

you pretty much addressed a question i would have asked down the line becuase you are right the supplied textField and buttons are boring.  How do you go about creating your own?

as for my lobby system i have a chat system up and running however now i want each client to be able to see a list of the users connected (and possibly at some point have private chat and challange a specific user to a game).  im using arraylist on the server side for all users that are connected but how in the world are you supposed to transfer and entire arraylist to the client and also update it as people come and go?
Offline Mr_Light

Senior Member




shiny.


« Reply #57 - Posted 2008-12-07 16:06:53 »

Quote
but how in the world are you supposed to transfer and entire arraylist to the client and also update it as people come and go?
Same way you send anything - serialise to bytes and de-serialise back.

ok, that was just mean.  Tongue     -And might also lead you down the wrong path of using java's buildin serialisation.

I'll try to write up some stuff but first I need to catch so deadline's, I'll edit / reply to this post a bit later.


It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Mr_Light

Senior Member




shiny.


« Reply #58 - Posted 2008-12-10 09:22:24 »

Sorry for not following up earlier.
you pretty much addressed a question i would have asked down the line because you are right the supplied textField and buttons are boring.  How do you go about creating your own?
Just draw them as any thing else in your game but don't make them apart of your logic and draw them a-top of everything else, on mouseclicks check if there is a 'component' on that coordinate and dispatch a mouse event to that component. That should make implementing buttons straightforward. as for text fields, have a object in which you track focus, have it contain the object that has focus and what (keyboard)events it's interested in. Append keys to the string resp remove on backspace. All the tedious stuff like validation etc are probably overkill for your game just filter A-Z + -_ or something for names. As for tracking which 'component' should have focus just add additional logic between the 'check-if-theres-a-component-at-that-coordinate' and 'dispatch-event-to-component'

as for my lobby system i have a chat system up and running however now i want each client to be able to see a list of the users connected (and possibly at some point have private chat and challange a specific user to a game).  im using arraylist on the server side for all users that are connected but how in the world are you supposed to transfer and entire arraylist to the client and also update it as people come and go?
First you must figure what behaviour you demand from client. Is it really that much of a problem if the user list goes out of sync if so how short should syncing be. Since your using tcp with it's guaranteed delivery as long as your connected, you can also go with sending the chat list on enter  and publish modifications(join/leave) to your clients. You can also go hybrid and send modifications and do a complete sync occationally.(Considering your using tcp - going out of sync, given that you can guaranty that publishing is always done, is impossible.)

As for the next step, make sure your familiar with (de)multiplexing - (data and it's context) vs (data + metadata outside of context).(there are perhaps easier explanations of this concept google) Context takes up no space so it's 'free' - how things are put into context is defined by a protocol.

Some examples:
(if ordered:)
Does cas like pie?
yes.
Is Java hard to learn?
no

(from the context you can derive that the answer follows directly after the question. - human protocol - humans aren't physic so it could hardly be any other way)

What where the answers to the teacher's questions?
yes, no

(almost no one is going to answer that way as ppl generally mess up the order - if they remember the questions at all. Also if the questions where asked on different occasions, you might not even be sure you got the same questions)

What where the answers to the teacher's questions?
Java wasn't hard to learn.
Cas likes pie.

As you can probably spot if you can make use of the context you need less text to answer.

An more programming-like example would (I gues) be:
'cas,mark,michael,kev'
vs
'1 cas'
'2 mark'
'3 michael'
'4 kev'

A slightly more complex example:

'100 cas,mark,michael,kev'

'101 cas'
'102 mark'
'103 michael'
'104 kev'

You can derive the same information from both data one just makes use of the context and is smaller. - off-course this only matters if you care about bandwidth esp as constructing information from context might eat away at other resources.

An example would be vectorised graphics, they are smaller but require more processing power to put on the screen.

As you might have picked up from earlier examples transfering a List Strings can be  represent a a String itself easily enough. Strings can easily be converted into bytes which can be send. Now you don't have a list of Strings you probably have a list of player objects. Select first what you actually want to share(which data on the server you might keep the ip adress around on the player object - doesn't mean you have to send it to your clients) make up a format convert it to string or directly to bytes.

A pointer here:

class Player {
 String name;
 ...
}

Player one = new Player("Michael");
one.asMessage() -> "Michael"
Player two = new Player("Michael");

one.equals(two) -> false - please note that you did have loss of information here: whilst multiplexing and demultiplexing you lost object identity, you probably don't care about this but in other similar causes you might care.
Simple solution is to define your object identity differently (override equals + hashcode to use name).

Regarding identifying - say you uniquely define players by firstname lastname it's probably not a good idea to repeat that every message as that takes up relatively a lot of space esp if your identifier uses a lot of fields, now dba's figured out long ago that using a unique number is usually more efficient.

A final timbit of information: a integer 1000 takes a lot less bytes as four chars: '1' '0' '0' '0'. - again this might or might not matter also if your lazy you can always just compress your stream characters Wink though hand tuned 'compression' is probably more efficient.

I would recommend sticking as doing clever things with bytes is probably premature optimalisation.

hope these pointers help, feel free to specify where I messed up -> made it way too obscure.

//edit hmm should probably add a timbit about separators and escape characters data

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline neoskunk

Junior Member





« Reply #59 - Posted 2008-12-11 06:13:28 »

first of all wow thanks for the help i cant imagine how long it took you to write all that.

creating widgets seems simple enough however textField seems like it could be a bit complicated.  For instance scrolling? in the chat i would like each new message to be on a new line and obviously when the text reaches the bottom of the screen or widget it should be scrollable. how do you implement that?


as for transfering the userlist i already have each player objected created with a userName to identify it and yes thats all i need to transfer not the actual object.  I believe i will go with when a user connects the server sends the current user list and later on sends an update with a leave/join.

getting into protocol is kinda where things get confusing because the server and client have to send so many different types of messages.  im having trouble making the client understand what to do with the data it recieves.  it seems like an easy way to do this (tell me if this is even possible)  would be to say:

update: userName
remove: userName
message: "message"
etc. 

preface the type of message like above or even shorten it to just one letter and a colon to represent the type of message. however after that is there a way to remove a portion of a string?  for instance with message: "message" i would have to remove "message:" and display the rest to the user.

sorry if thats a little confusing...
Pages: 1 [2] 3
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Riven (20 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!