The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CommThread Class Reference

#include <commthread.h>

Collaboration diagram for CommThread:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 CommThread ()
void PutData (int *vec, int n)
 ~CommThread ()

Static Public Member Functions

void CommLoop (void *)

Private Member Functions

void GetClientIP (char *c_ip, int &c_p)
void Process ()
void WriteData (MPIO *writer)

Private Attributes

bool m_Available
std::vector< int > m_Data
MUTEX_TYPE m_DataMutex
IniConfig m_Ini
MPSocketIOm_SocketWriter
MUTEX_TYPE m_ThreadHandle
bool m_ThreadRunning
WSADATA ws

Constructor & Destructor Documentation

CommThread  ) 
 

Definition at line 26 of file commthread.cpp.

References CommLoop(), MPSocketIO::connectServer(), CreateMutex(), GetClientIP(), MPConfiguration::intval(), m_DataMutex, m_SocketWriter, m_ThreadHandle, m_ThreadRunning, sMUTEX::mutex, and ws.

00027 : m_Available(false),
00028         m_Ini()
00029 {
00030 
00031         m_ThreadRunning = true;
00032         CreateMutex(m_DataMutex);
00033 /*#ifdef WIN32
00034         m_DataMutex = CreateMutex (NULL, false, NULL);
00035 #else
00036         pthread_mutex_init(&m_DataMutex,NULL);
00037 #endif*/        
00038 
00039         if (MPConfigure::intval("socketio")) {
00040                 WSAStartup(0x101,&ws);  /* This is line is very important for initializing the socket */
00041                 char *client_ip = new char[255];
00042                 int port_num;
00043                 GetClientIP(client_ip, port_num);
00044                 m_SocketWriter = new MPSocketIO(client_ip, port_num);
00045                 delete [] client_ip;
00046                 m_SocketWriter->connectServer();
00047         }
00048 
00049         m_ThreadHandle.mutex = (HANDLE) (_beginthread( CommLoop, 0, this ));
00050         SetThreadPriority(m_ThreadHandle.mutex, THREAD_PRIORITY_NORMAL-1);
00051 }

Here is the call graph for this function:

~CommThread  ) 
 

Definition at line 136 of file commthread.cpp.

References MPSocketIO::disconnectServer(), MPConfiguration::intval(), LockMutex(), m_SocketWriter, m_ThreadHandle, and m_ThreadRunning.

00137 {
00138         m_ThreadRunning = false;
00139 
00140         LockMutex(m_ThreadHandle);
00141         //WaitForSingleObject(m_ThreadHandle, INFINITE);
00142 
00143         if (MPConfigure::intval("socketio")) {
00144                 m_SocketWriter->disconnectServer();
00145                 delete m_SocketWriter;
00146         }
00147 }

Here is the call graph for this function:


Member Function Documentation

void CommLoop void *   )  [static]
 

Definition at line 55 of file commthread.cpp.

References m_ThreadRunning, and Process().

Referenced by CommThread().

00056 {       
00057         CommThread *CT = static_cast<CommThread *>(ThisCommThread);
00058         while (CT->m_ThreadRunning)
00059                 CT->Process();
00060         _endthread();
00061 }

Here is the call graph for this function:

void GetClientIP char *  c_ip,
int &  c_p
[private]
 

Definition at line 124 of file commthread.cpp.

References MPConfiguration::intval(), string, and MPConfiguration::stringval().

Referenced by CommThread().

00125 {
00126         string ip = "127.0.0.1";
00127         int port = 12000;
00128 
00129         ip = MPConfigure::stringval("ip");
00130         memcpy(c_ip, ip.c_str(), sizeof(ip.c_str())*sizeof(string));
00131         c_p = MPConfigure::intval("port");
00132 }

Here is the call graph for this function:

void Process  )  [private]
 

Definition at line 65 of file commthread.cpp.

References MPSocketIO::connectServer(), MPSocketIO::disconnectServer(), MPConfiguration::intval(), MPSocketIO::isConnect(), LockMutex(), m_Available, m_DataMutex, m_SocketWriter, ReleaseMutex(), WAIT_EXTERNAL, and WriteData().

Referenced by CommLoop().

00066 {
00067         LockMutex(m_DataMutex);
00068         if (m_Available) {
00069                 m_Available = false;
00070 
00071                 if (MPConfigure::intval("socketio")) {
00072                         if(m_SocketWriter->isConnect())
00073                                 WriteData(m_SocketWriter);
00074                         else {
00075                                 m_SocketWriter->disconnectServer();
00076                                 m_SocketWriter->connectServer();
00077                         }
00078                 }
00079         } 
00080         ReleaseMutex( m_DataMutex);
00081         Sleep(WAIT_EXTERNAL);
00082 }

Here is the call graph for this function:

void PutData int *  vec,
int  n
 

Definition at line 87 of file commthread.cpp.

References DWORD, i, m_Available, m_Data, m_DataMutex, ReleaseMutex(), TryLockMutex(), WAIT_ABANDONED, WAIT_EXTERNAL, WAIT_FAILED, WAIT_OBJECT_0, and WAIT_TIMEOUT.

00088 {
00089         DWORD waitResult = TryLockMutex(m_DataMutex, WAIT_EXTERNAL );
00090 
00091         switch( waitResult ) {
00092         case WAIT_OBJECT_0:  /* The state of the specified object is signaled. */
00093                 if (!m_Available) {
00094                         m_Data.clear();
00095                         for (int i = 0; i < n; ++i)
00096                                 m_Data.push_back(vec[i]);
00097                         m_Available = true;
00098                 }
00099                 ReleaseMutex( m_DataMutex);
00100                 break;
00101         case WAIT_TIMEOUT: /* The time-out interval elapsed, and the object's state is nonsignaled. */
00102         case WAIT_ABANDONED: /* The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. */
00103         case WAIT_FAILED:
00104         default:
00105                 break;
00106         }
00107 }

Here is the call graph for this function:

void WriteData MPIO writer  )  [private]
 

Definition at line 111 of file commthread.cpp.

References i, m_Data, and MPIO::write().

Referenced by Process().

00112 {
00113         const char startSig = '+';
00114         const char endSig = '-';
00115         writer->write(startSig);
00116 
00117         for (vector<int>::iterator i = m_Data.begin(); i != m_Data.end(); ++i)
00118                 writer->write(*i);
00119         writer->write(endSig);
00120 }

Here is the call graph for this function:


Member Data Documentation

bool m_Available [private]
 

Definition at line 35 of file commthread.h.

Referenced by Process(), and PutData().

std::vector<int> m_Data [private]
 

Definition at line 34 of file commthread.h.

Referenced by PutData(), and WriteData().

MUTEX_TYPE m_DataMutex [private]
 

Definition at line 30 of file commthread.h.

Referenced by CommThread(), Process(), and PutData().

IniConfig m_Ini [private]
 

Definition at line 36 of file commthread.h.

MPSocketIO* m_SocketWriter [private]
 

Definition at line 33 of file commthread.h.

Referenced by CommThread(), Process(), and ~CommThread().

MUTEX_TYPE m_ThreadHandle [private]
 

Definition at line 30 of file commthread.h.

Referenced by CommThread(), and ~CommThread().

bool m_ThreadRunning [private]
 

Definition at line 31 of file commthread.h.

Referenced by CommLoop(), CommThread(), and ~CommThread().

WSADATA ws [private]
 

Definition at line 32 of file commthread.h.

Referenced by CommThread().


The documentation for this class was generated from the following files:
Generated on Mon Nov 8 17:08:30 2004 for MPT by  doxygen 1.3.9.1