Monday, 7 November 2011

Java: A Network Application (3)

Somehow, I didn't get enough sleep, still. Oh well, time for more postin'! Today, we're continuing right off the network node from Saturday, and we'll be done with that. Tomorrow, we go to the node listener, then Wednesday we write the actual application. I'll give you until then to throw out any questions or requests about anything before we move on to those (in all probability, shitty) games!

So, diving into it:

Since we're done with the node run method, we just need to focus on how other classes will access information they need. Lines 48-57 hold the getMsg() method, which will be used for the NodeListener class to....get the message.
Line 51, the wait line, blocks off threads related to the NodeListener. They just hold nice and still, and the next few lines are just trying to fit a huge string onto one line, because I like keeping my images to original size so they're legible :P
It sucks when you're forced to catch exceptions, but you don't really plan on doing anything with them, but at least it's easy to deal with, just like you can see on Line 55.

Next up we just have our static array to hold all our nodes in, then an actual way to construct them. Remember, the constructor was private, so this will be the only way we can actually make any! We don't want this being called more than once, because that's just more of a headache, so we'll just throw an error at the user if they try.
Other than the error throwing, this is a fairly straightforward array construction, Line 65 initializes the array, then we have a for loop to fill it up, and start each node running.

TL;DR: A method to get the message (for use by the listener), and then a method to create an array of nodes for usage. Don't forget to block the threads when getting the message, and don't forget to start them when initializing.

Here, we've got a couple more administrative methods. Nothing spectacular, a way to get a specific node on Lines 72-83. You could just use 1 line for this, "return index[i]", but then again, that doesn't account for the 2 errors that I throw exceptions for. Namely, asking for a node when none have been made, or going out of bounds.

The very last method is just as simple, if not more so, due to lack of user-defined parameters. It just gives you the length of the node array. Only error to take care of here is if its called before anything happens.

And that's it, we're done with the NetworkNode itself, we just need a couple more classes to test this out. But honestly, if its as confusing as this is, the only thing I'll learn is how fucked I am if I try to do anything important with code :P

See you tomorrow, folks, questions and comments, as always, welcome!


  1. amazing! thanks for sharing and teaching me :p

  2. This is getting more interesting. (:

  3. No questions from me! Can't wait for tomorrow!

  4. Have more faith in your abilities, I'm sure any games you make will be fine. :P

    Well, assuming you get more rest. You're going to end up like me at this rate.