x404.co.uk http://www.x404.co.uk/forum/ |
|
Storing and using an unknown number of objects http://www.x404.co.uk/forum/viewtopic.php?f=4&t=7963 |
Page 1 of 1 |
Author: | Nick [ Tue Apr 27, 2010 3:11 pm ] |
Post subject: | Storing and using an unknown number of objects |
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? |
Author: | Fogmeister [ Tue Apr 27, 2010 3:58 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
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. |
Author: | Nick [ Tue Apr 27, 2010 4:07 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
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. |
Author: | Fogmeister [ Tue Apr 27, 2010 5:02 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
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 |
Author: | Fogmeister [ Tue Apr 27, 2010 5:07 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
In fact, Hashtable would be better. I think Dictionary is the abstract parent of it. |
Author: | Nick [ Tue Apr 27, 2010 5:28 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
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. ![]() |
Author: | forquare1 [ Tue Apr 27, 2010 6:17 pm ] | |||||||||
Post subject: | Re: Storing and using an unknown number of objects | |||||||||
I was going to suggest just the thing! [quote="Nick"I never seem to be able to find solutions like this. ![]() 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. |
Author: | finlay666 [ Tue Apr 27, 2010 6:29 pm ] | |||||||||
Post subject: | Re: Storing and using an unknown number of objects | |||||||||
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 ![]() |
Author: | Fogmeister [ Wed Apr 28, 2010 8:58 am ] | |||||||||
Post subject: | Re: Storing and using an unknown number of objects | |||||||||
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. |
Author: | Nick [ Mon May 03, 2010 11:42 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
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. |
Author: | forquare1 [ Tue May 04, 2010 8:09 am ] |
Post subject: | Re: Storing and using an unknown number of objects |
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/ |
Author: | Nick [ Tue May 04, 2010 7:14 pm ] |
Post subject: | Re: Storing and using an unknown number of objects |
Cool. I'm going to resist the temptation to have a look at that code until I'm finished. ![]() I'll post my code when I'm done, too. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |