2 #include "ace/OS_NS_string.h"
3 #include "ace/OS_NS_stdlib.h"
4 #include "ace/OS_Memory.h"
5 #include "ace/os_include/os_netdb.h"
7 // This constructor takes a message of sorted login names and loads up
8 // the symbol table on the server's side. It assumes that the number
9 // of friends is stored in the first MAXUSERIDNAMELEN bytes of the
10 // packet. Note that we assume that the client sends the login names
11 // in sorted order, so we don't bother sorting them!
13 BS_Server::BS_Server (const char *packet
)
15 const char *buf_ptr
= packet
+ MAXUSERIDNAMELEN
;
17 this->count_
= ACE_OS::atoi (packet
);
18 this->buffer_
= buf_ptr
;
20 ACE_NEW (this->protocol_record_
,
21 Protocol_Record
[this->count_
]);
22 ACE_NEW (this->sorted_record_
,
23 Protocol_Record
*[this->count_
]);
25 for (int i
= 0; i
< this->count_
; i
++)
27 Protocol_Record
*rec_ptr
= &this->protocol_record_
[i
];
29 this->sorted_record_
[i
] = rec_ptr
;
30 rec_ptr
->key_name1_
= buf_ptr
;
32 // Skip forward to the start of the next login name.
34 while (*buf_ptr
++ != '\0')
40 // Insert the HOST_NAME into the appropriate DRWHO_LIST slot if the
41 // KEY_NAME happens to be one of our friends. Binary search is used
42 // because the Protocol_Manager keeps a sorted representation of the
45 // Note that this binary search is tuned for unsuccessful searches,
46 // since most of the time we the KEY_NAME is *not* a friend (unless
47 // you've got *lots* of friends)!
49 // Note finally that we keep a cache of the last KEY_NAME that is
50 // looked up, as well as the result of the lookup. This speeds things
51 // up because the whod files tend to cluster userids together. */
54 BS_Server::insert (const char *key_name
, int max_len
)
56 static char last_lookup
[MAXHOSTNAMELEN
];
58 static int result
= 0;
59 Protocol_Record
**buffer
= this->sorted_record_
;
61 // First check the cache...
62 if (ACE_OS::strncmp (last_lookup
, key_name
, max_len
) == 0)
69 // Store this away in the cache for the next iteration.
70 ACE_OS::strncpy (last_lookup
, key_name
, max_len
);
72 int hi
= this->count_
- 1;
78 mid
= (hi
+ lo
+ 1) / 2;
80 cmp
= ACE_OS::strncmp (key_name
,
81 buffer
[mid
]->get_login (),
89 // This line is very subtle... ;-)
91 || ACE_OS::strncmp (key_name
, buffer
[--mid
]->get_login (), max_len
) == 0))
98 // If we get here we've located a friend.
104 // Returns the next friend in the sequence of sorted friends. Skips
105 // over the entries that don't have any hosts associated with them
106 // (because these entries weren't on the server machine. */
109 BS_Server::get_next_entry ()
111 for (Protocol_Record
*prp
= Binary_Search::get_next_entry ();
113 prp
= Binary_Search::get_next_entry ())
114 if (prp
->get_drwho_list () != 0)