Reply to topic  [ 12 posts ] 
Storing and using an unknown number of objects 
Author Message
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
So I'm working on my client/server chat application - and I'm running into a problem that I didn't have in my chess application.

In chess, I knew how many squares I had on the board, so I just stored and used the piece objects by addressing their location on the board. Simples.

The thing I can't get my head around in this application is how the server is going to store the clients' details, and then address them easily later.

It would be easy just to create a client class and throw the clients into an array or arraylist or vector etc but then when two clients want to transfer a message I'll need to loop through them all to find which one I need.

That just seems messy, and not very elegant. Is there a different way that I've missed?

_________________
Image


Tue Apr 27, 2010 3:11 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Lol, I thought about writing a chat client once. I got to exactly the point you are at now and didn't take it any further.

Coming back to it I can think of a potential way of getting it working.

Hmm... let me get back to you later on when I've got time to think it through.

It involves some sort of ClientPair object which links each client to the other client it is connected to (or something). Acting as a virtual chat room.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 27, 2010 3:58 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
I did start along those lines Oli - I created a "chat" object, but soon realised that I'd still have the same problem. Storing and accessing them elegantly.

Also - I thought it might end up being slower because you'd have to test for both clients in the chat object as of course each client could have more than one chat open.

:?

I've pretty much decided that the client will send data to the server in this format (assuming the client has an IP of 192.168.1.11 and the partner they want to communicate with is .22):

192.168.1.22:This is the text i want to send <-- out to server from .11 client

So the server rips out the target IP and repackages the text as:

192.168.1.11:This is the text i want to send <-- out to .22 client from server.including .11 IP so it can display text in correct chat window and reply

I've just got pretty much no idea of how I can store and use the client objects (which contain the socket connections) on the server, other than storing them all in an array of some sort and looping round testing if each object is the one I want. I don't want to do that because it just seems really sloppy, and I get the feeling there's a nicer way to do it that I've not thought of.

_________________
Image


Tue Apr 27, 2010 4:07 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hmm... I haven't used them much but have you looked at the Dictionary class? It's a sort of object/key list so to get an object y=out you reference it via a key.

i.e. user logs in.
create Client object with userID "Nick".
store client in dictionary with key "Nick".
second user logs in.
create Client object with userID "Fogmeister".
store in dictionary with key "Fogmeister".

Now when Nick requests a chat with Fogmeister it can get the user straight away from the userID. You would have to send userID info along with every chat so that you know which chat to put it in.

i.e
fromUID: Nick
toUID: Fogmeister
msg: Hello how are you?

HTH

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 27, 2010 5:02 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
In fact, Hashtable would be better. I think Dictionary is the abstract parent of it.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Apr 27, 2010 5:07 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Ah-ha. That sounds like it could be really useful.

How did you discover them?

I never seem to be able to find solutions like this. :?

_________________
Image


Tue Apr 27, 2010 5:28 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Fogmeister wrote:
In fact, Hashtable would be better. I think Dictionary is the abstract parent of it.


I was going to suggest just the thing!

[quote="Nick"I never seem to be able to find solutions like this. :?[/quote]

I learned about them in a module called "Algorithms and Data structures". I've used them in a number of instances as they are a method of storing data and randomly accessing it.


Tue Apr 27, 2010 6:17 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
forquare1 wrote:
I learned about them in a module called "Algorithms and Data structures". I've used them in a number of instances as they are a method of storing data and randomly accessing it.


Hashtables/Dictionaries in this instance are great because they have a unique key to access them by, this can be email, IP, name etc and then an object can be attached to it, it's also quite an efficient way of storing data :)

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Tue Apr 27, 2010 6:29 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Nick wrote:
How did you discover them?

I never seem to be able to find solutions like this. :?

I first used Dictionaries in Objective C when converting database info into program storage.

I have looked at Hashtables in Java before but never needed to use them.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Apr 28, 2010 8:58 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
I've gone with another solution to this problem.

Now, instead of having a central server it's going to be P2P.

Pretty cool - and also much snazzier in the way it handles objects. Less code too which is a bonus.

_________________
Image


Mon May 03, 2010 11:42 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
A mate of mine did a chat client. I started one back in 2007, but my HDD died and I lost it all. My mate then took my ideas and did something with it (was rather annoyed at that, but seeing as I'd gotten so far and the lost everything, didn't care to pick it back up).

He used P2P, and it only worked on the local network, but worked relatively successfully by all accounts.
Looks like his code is available here:
http://code.google.com/p/intranet-chat/


Tue May 04, 2010 8:09 am
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 11:36 pm
Posts: 3527
Location: Portsmouth
Reply with quote
Cool.

I'm going to resist the temptation to have a look at that code until I'm finished. :P

I'll post my code when I'm done, too.

_________________
Image


Tue May 04, 2010 7:14 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.