3 #include "Rwho_DB_Manager.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_time.h"
7 #include "ace/OS_NS_fcntl.h"
9 // Change to the RWHO directory to speed up and simplify later
10 // processing. This requires opening the directory for reading with
11 // the directory iterator abstraction and then skipping the first two
12 // files in the directory, which are assumed to be "." and ".." (this
13 // function needs to be changed if this assumption does not hold!)
15 RWho_DB_Manager::RWho_DB_Manager ()
16 : number_of_users (0),
18 WHOD_HEADER_SIZE (sizeof host_data
- sizeof host_data
.wd_we
),
19 rwho_dir_name (RWHODIR
)
21 if (ACE_OS::getcwd (this->original_pathname
, MAXPATHLEN
+ 1) == 0)
24 Options::program_name
,
27 if (ACE_OS::chdir (this->rwho_dir_name
) < 0)
33 this->rwho_dir
.open (this->rwho_dir_name
);
37 this->rwho_dir
.read ();
38 this->rwho_dir
.read ();
42 // The destructor cleans up the RWHOD_DIR handle.
44 RWho_DB_Manager::~RWho_DB_Manager ()
46 if (ACE_OS::chdir (this->original_pathname
) < 0)
49 Options::program_name
,
52 if (Options::get_opt (Options::DEBUGGING
))
54 "disposing the RWho_DB_Manager\n"));
57 // This procedure looks through the rwhod directory until it finds the next
60 // The requirements for user files are:
61 // 1) The file is at least MIN_HOST_DATA_SIZE bytes long
62 // 2) It was received within the last MAX_HOST_TIMEOUT seconds
64 // Are there any more hosts? */
67 RWho_DB_Manager::get_next_host ()
71 ACE_OS::time (¤t_time
);
73 // Go through each file in the directory looking for valid entries.
75 for (dirent
*dir_ptr
= this->rwho_dir
.read ();
77 dir_ptr
= this->rwho_dir
.read ())
79 ACE_HANDLE user_file
=
80 ACE_OS::open (dir_ptr
->d_name
, O_RDONLY
);
85 int host_data_length
=
86 ACE_OS::read (user_file
,
87 (char *) &this->host_data
,
88 sizeof this->host_data
);
90 if (host_data_length
> WHOD_HEADER_SIZE
91 && current_time
- this->host_data
.wd_recvtime
< MAX_HOST_TIMEOUT
)
93 this->current_user
= 0;
94 this->number_of_users
= (host_data_length
- WHOD_HEADER_SIZE
) / sizeof *this->host_data
.wd_we
;
95 ACE_OS::close (user_file
);
96 return 1; // We found a good host, so return it.
99 ACE_OS::close (user_file
);
102 // There are no more hosts, so return False.
106 // Returns the next user's information. Note that for efficiency only
107 // pointers are copied, i.e., this info must be used before we call
108 // this function again.
111 RWho_DB_Manager::get_next_user (Protocol_Record
&protocol_record
)
113 // Get the next host file if necessary
114 if (this->current_user
>= this->number_of_users
115 && this->get_next_host () == 0)
118 protocol_record
.set_login (this->host_data
.wd_we
[current_user
].we_utmp
.out_name
);
119 Drwho_Node
*current_node
= protocol_record
.get_drwho_list ();
120 current_node
->set_host_name (this->host_data
.wd_hostname
);
121 current_node
->set_idle_time (this->host_data
.wd_we
[current_user
].we_idle
);
122 this->current_user
++;