Cleanup ACE_HAS_PTHREAD_SIGMASK_PROTOTYPE, all platforms support it so far as I can...
[ACE_TAO.git] / ACE / apps / drwho / BS_Server.cpp
blobb72d20fb46fc9394e7e04de5ed1d0f12c5456254
1 #include "BS_Server.h"
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')
35 continue;
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
43 // friend names.
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. */
53 Protocol_Record *
54 BS_Server::insert (const char *key_name, int max_len)
56 static char last_lookup[MAXHOSTNAMELEN];
57 static int mid = 0;
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)
64 if (result == 0)
65 return 0;
67 else
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;
73 int lo = 0;
74 int cmp = 0;
76 while (lo < hi)
78 mid = (hi + lo + 1) / 2;
80 cmp = ACE_OS::strncmp (key_name,
81 buffer[mid]->get_login (),
82 max_len);
83 if (cmp < 0)
84 hi = mid - 1;
85 else
86 lo = mid;
89 // This line is very subtle... ;-)
90 if (!(cmp == 0
91 || ACE_OS::strncmp (key_name, buffer[--mid]->get_login (), max_len) == 0))
93 result = 0;
94 return 0;
98 // If we get here we've located a friend.
100 result = 1;
101 return buffer[mid];
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. */
108 Protocol_Record *
109 BS_Server::get_next_entry ()
111 for (Protocol_Record *prp = Binary_Search::get_next_entry ();
112 prp != 0;
113 prp = Binary_Search::get_next_entry ())
114 if (prp->get_drwho_list () != 0)
115 return prp;
117 return 0;