Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  [Networking] (Client) Unique User Identification Number  (Read 1741 times)
0 Members and 1 Guest are viewing this topic.
Offline GabrielBailey74
« Posted 2013-06-03 01:37:55 »

Hello JGO before I dig in on this I was wondering how I should go about giving each client that connects a Unique ID.
(Abstract / importable networking kit)

Not like:
Client #0 ID: 937
Client #1 ID: 238

But rather in 1 + 1 order
:
Client #0 ID: 0
Client #1 ID: 1

The part that I think will confuzzle me is when a client disconnects.
Before I post any code that will probably make no sense, I'd like some feedback on a good approach to take on achieving this ^_^

Thanks JGO, hope somebody can help ~_^

Offline GabrielBailey74
« Reply #1 - Posted 2013-06-03 01:43:28 »

My only idea so far 0_0

Pseudo-Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
         // clients[0] = 0
         // clients[1] = 1
         // clients[2] = 2
         // clients[3] = 3
         // clients[4] = 4
         // clients[5] = 5
         
         // clients # 3 disconnects:
         // 100 clients online
         for (int i = 0; i < clients.length; i++) {
            final Client c = clients[i];
            if (c.getID() >= 3) {
               for (int j = 3; j < 100; j++) {
                  clients[j].ID -= 1;                  
                  clients[j].update();
               }
            }
         }

Offline Sammidysam
« Reply #2 - Posted 2013-06-03 01:57:14 »

If you were to use an ArrayList instead of array, couldn't you easily reorder the IDs without any extra work on your side?  The remove method will easily do this.  Like:

1  
2  
3  
ArrayList<Client> clients = new ArrayList<Client>(100); //initial capacity of 100 but I prefer 0 and just let it increase and decrease capacity as necessary
//client #3 disconnects
clients.remove(3); //removes client 3


clients.get(3).getID() would now equal 4.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2013-06-03 01:58:40 »

If you were to use an ArrayList instead of array, couldn't you easily reorder the IDs without any extra work on your side?

That's what I was thinking too. You could remove a client and then all of the other id's would automatically drop one.

Offline ra4king

JGO Kernel


Medals: 506
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2013-06-03 02:02:26 »

Just thought of a neat solution: when a client disconnects, if there are ID's created after it, add that client's number to an ArrayList in sorted order. If the ArrayList is not empty, grab and remove the first number to hand out. Otherwise, assign the next value in order.

Offline GabrielBailey74
« Reply #5 - Posted 2013-06-03 02:10:21 »

That's what I was thinking too. You could remove a client and then all of the other id's would automatically drop one.
Wow thanks haha, I didn't think it would be self sustaining. (As in automatically decreasing the "ID")

-Implementing / Testing it now.

Offline GabrielBailey74
« Reply #6 - Posted 2013-06-03 02:11:47 »

ArrayList<Client> clients = new ArrayList<Client>(100); //initial capacity of 100 but I prefer 0 and just let it increase and decrease capacity as necessary
Thanks mate, never knew you could set the initial capacity of a List ^_^

Offline GabrielBailey74
« Reply #7 - Posted 2013-06-03 02:22:01 »

Great, thanks I got it guys.

The pseudo-code I posted above earlier worked ^_^
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   /* Method implemented for calling other functions besides disconnected */
   void clientDisconnected2(ClientModel clientModel) {
      clientDisconnected(clientModel); // abstract method
      System.out.println("Client " + clientModel.test_name + " disconnected");
      for (ClientModel c : getClients()) {
         if (c.getID() > clientModel.getID()) {
            c.setID(c.getID() - 1);
            c.sendMessage("A Client has dc'd your new ID is " + c.getID() + ".");
         }
      }
      getClients().remove(clientModel);
   }
I didn't think it would be that easy 0_0!

Offline 65K
« Reply #8 - Posted 2013-06-03 05:16:32 »

It might work, but switching object identifiers in a running program is not a good idea. Bad for consistency, understanding, debugging, etc.
Uniqe identifiers and list indexes are two things and should not be mixed up.

Lethal Running - a RPG about a deadly game show held in a futuristic dysoptian society.
Offline Z-Man
« Reply #9 - Posted 2013-06-03 05:51:37 »

It might work, but switching object identifiers in a running program is not a good idea. Bad for consistency, understanding, debugging, etc.
Uniqe identifiers and list indexes are two things and should not be mixed up.

This. If you want unique IDs then don't change them in the middle of execution Tongue I'd go with ra4king suggestion of assigning each client an ID on connecting (0, 1, 2, 3, etc.) then when someone leaves their ID gets added to a sorted list that you can pull from the next time somebody connects.
Pages: [1]
  ignore  |  Print  
 
 

 
xxMrPHDxx (18 views)
2017-11-21 16:21:00

xxMrPHDxx (11 views)
2017-11-21 16:14:31

xxMrPHDxx (14 views)
2017-11-21 16:10:57

Ecumene (114 views)
2017-09-30 02:57:34

theagentd (148 views)
2017-09-26 18:23:31

cybrmynd (248 views)
2017-08-02 12:28:51

cybrmynd (248 views)
2017-08-02 12:19:43

cybrmynd (244 views)
2017-08-02 12:18:09

Sralse (258 views)
2017-07-25 17:13:48

Archive (878 views)
2017-04-27 17:45:51
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!