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.cpp

Go to the documentation of this file.
00001 /*
00002  * commthread.cpp
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind
00010  */
00011 #ifdef WIN32
00012 #pragma warning(disable:4786)
00013 #endif
00014 
00015 #include "commthread.h"
00016 #include "common.h"
00017 #include <process.h> /* for NT threading */
00018 
00019 using namespace std;
00020 
00021 /* Alternative to non-blocking or infinite mutex waits */
00022 const int WAIT_EXTERNAL=16;
00023 
00024 /* ================================================================ */
00025         
00026 CommThread::CommThread() 
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 }
00052 
00053 /* ================================================================ */
00054 
00055 void CommThread::CommLoop(void *ThisCommThread)
00056 {       
00057         CommThread *CT = static_cast<CommThread *>(ThisCommThread);
00058         while (CT->m_ThreadRunning)
00059                 CT->Process();
00060         _endthread();
00061 }
00062 
00063 /* ================================================================ */
00064 
00065 void CommThread::Process()
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 }
00083 
00084 
00085 /* ================================================================ */
00086 
00087 void CommThread::PutData(int *vec, int n)
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 }
00108 
00109 /* ================================================================ */
00110 
00111 void CommThread::WriteData(MPIO *writer)
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 }
00121 
00122 /* ================================================================ */
00123 
00124 void CommThread::GetClientIP(char*c_ip, int &c_p)
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 }
00133 
00134 /* ================================================================ */
00135 
00136 CommThread::~CommThread()
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 }
00148 
00149 /* ================================================================ */
00150 
00151 /*
00152  * 
00153  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00154  * 
00155  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00156  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00157  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00158  * 
00159  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00160  * 
00161  */
00162 

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1