lister.cpp

00001 
00002 /***************************************************************************
00003  *  shmem_lister.cpp - BlackBoard shared memory lister
00004  *
00005  *  Created: Fri Oct 20 11:50:03 2006
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <blackboard/shmem/lister.h>
00025 #include <utils/system/console_colors.h>
00026 #include <utils/ipc/shm.h>
00027 
00028 #include <iostream>
00029 #include <cstdio>
00030 
00031 using namespace std;
00032 namespace fawkes {
00033 
00034 /** @class BlackBoardSharedMemoryLister <blackboard/shmem/lister.h>
00035  * BlackBoard shared memory lister.
00036  * Lister that can be used to print infos about BlackBoard shared memory
00037  * segments.
00038  * @author Tim Niemueller
00039  */
00040 
00041 /** Constructor */
00042 BlackBoardSharedMemoryLister::BlackBoardSharedMemoryLister()
00043 {
00044   num = 0;
00045 }
00046 
00047 
00048 /** Destructor */
00049 BlackBoardSharedMemoryLister::~BlackBoardSharedMemoryLister()
00050 {
00051 }
00052 
00053 
00054 /** Print header of the table.
00055  * This should fit on the terminal and thus have a width of at most
00056  * 79 columns.
00057  */
00058 void
00059 BlackBoardSharedMemoryLister::print_header()
00060 {
00061   cout << endl << cblue << "Fawkes BlackBoard Shared Memory Segments" << cnormal << endl
00062        << "========================================================================" << endl
00063        << cdarkgray;
00064   printf ("%-3s %-10s %-11s %-16s %-12s %s\n",
00065           "#", "ShmID", "Semaphore", "Bytes", "# attached", "State");
00066   cout << cnormal
00067        << "------------------------------------------------------------------------" << endl;
00068   num = 0;
00069 }
00070 
00071 
00072 /** Print footer of the table.
00073  * This should fit on the terminal and thus have a width of at most
00074  * 79 columns.
00075  */
00076 void
00077 BlackBoardSharedMemoryLister::print_footer()
00078 {
00079   cout << "========================================================================" << endl;
00080 }
00081 
00082 
00083 /** Print this if no matching segment was found.
00084  * Called by SharedMemory if no matching segment could be found.
00085  */
00086 void
00087 BlackBoardSharedMemoryLister::print_no_segments()
00088 {
00089   cout << "No Fawkes BlackBoard shared memory segments found" << endl;
00090 }
00091 
00092 
00093 /** Print this if no matching orphaned segment was found.
00094  * Called by SharedMemory::erase_orphaned() if no matching segment
00095  * could be found.
00096  */
00097 void
00098 BlackBoardSharedMemoryLister::print_no_orphaned_segments()
00099 {
00100   cout << "No " << cdarkgray << "orphaned" << cnormal
00101        << " Fawkes BlackBoard shared memory segments found" << endl;
00102 }
00103 
00104 
00105 /** Print info about segment.
00106  * This method is called for every matching shared memory segment.
00107  * You should print a line of information (maybe more than one line
00108  * if needed) about the segment.
00109  * @param header The data-specific header
00110  * @param shm_id The id of the shared memory segment
00111  * @param semaphore semaphore assigned to the shared memory segment
00112  * @param mem_size the total memory size
00113  * @param memptr pointer to the data segment.
00114  */
00115 void
00116 BlackBoardSharedMemoryLister::print_info(const SharedMemoryHeader *header,
00117                                          int shm_id, int semaphore,
00118                                          unsigned int mem_size,
00119                                          const void *memptr)
00120 {
00121   unsigned int nattch = SharedMemory::num_attached(shm_id);
00122   bool swapable = SharedMemory::is_swapable(shm_id);
00123   bool destroyed = SharedMemory::is_destroyed(shm_id);
00124 
00125   printf ("%-3u %-10d 0x%08x  %-16u %-12u %s%s%s%s%s\n",
00126           ++num, shm_id, semaphore, mem_size, nattch,
00127           ((nattch > 1) ? "active" : "orphaned"),
00128           ((swapable || destroyed) ? " (" : ""),
00129           (swapable ? "S" : ""),
00130           (destroyed ? "D" : ""),
00131           ((swapable || destroyed) ? ")" : "")
00132           );
00133 }
00134 
00135 } // end namespace fawkes