#include "wx/defs.h"
#include <stdio.h>
#include "wx/string.h"
#include "wx/file.h"
#include "wx/app.h"
#include "wx/log.h"
#include "wxsbclient.h"
#include "wx/utils.h"
#include "wx/socket.h"
#include "wx/protocol/protocol.h"
#include "wx/protocol/http.h"
#include "wx/thread.h"
#include "wx/datetime.h"
Go to the source code of this file.
#define wxArgv argv |
Referenced by main().
int main | ( | int | argc, | |
char ** | argv | |||
) |
Create files "tessdata/inttemp" and "tessdata/pffmtable" from feature samples in training pageS.
Definition at line 608 of file console_socket.cpp.
References check_legal_image_size(), TessBaseAPI::End(), ERRCODE::error(), EXIT, IMAGE::get_bpp(), IMAGE::get_buffer(), IMAGE::get_xsize(), IMAGE::get_ysize(), TessBaseAPI::Init(), MEMORY_OUT, NULL, IMAGE::read(), IMAGE::read_header(), READFAILED, TessBaseAPI::TesseractRect(), TestEnvironment(), TestSocketClient(), tprintf(), USAGE, and wxArgv.
00609 { 00610 #if wxUSE_UNICODE 00611 wxChar **wxArgv = new wxChar *[argc + 1]; 00612 00613 { 00614 int n; 00615 00616 for (n = 0; n < argc; n++ ) 00617 { 00618 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]); 00619 wxArgv[n] = wxStrdup(warg); 00620 } 00621 00622 wxArgv[n] = NULL; 00623 } 00624 #else // !wxUSE_UNICODE 00625 #define wxArgv argv 00626 #endif // wxUSE_UNICODE/!wxUSE_UNICODE 00627 00628 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program"); 00629 00630 wxInitializer initializer; 00631 if ( !initializer ) 00632 { 00633 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); 00634 00635 return -1; 00636 } 00637 00638 00639 TestEnvironment(); 00640 00641 00642 // TestSocketServer(); 00643 TestSocketClient(); 00644 00645 /* 00646 int nCPUs = wxThread::GetCPUCount(); 00647 wxPrintf(_T("This system has %d CPUs\n"), nCPUs); 00648 if ( nCPUs != -1 ) 00649 wxThread::SetConcurrency(nCPUs); 00650 00651 TestJoinableThreads(); 00652 00653 TestJoinableThreads(); 00654 TestDetachedThreads(); 00655 TestThreadSuspend(); 00656 TestThreadDelete(); 00657 TestThreadConditions(); 00658 TestThreadExec(); 00659 TestSemaphore(); 00660 */ 00661 #if wxUSE_UNICODE 00662 { 00663 for ( int n = 0; n < argc; n++ ) 00664 free(wxArgv[n]); 00665 00666 delete [] wxArgv; 00667 } 00668 #endif // wxUSE_UNICODE 00669 00670 wxUnusedVar(argc); 00671 wxUnusedVar(argv); 00672 return 0; 00673 }
static wxString MakePrintable | ( | const wxChar * | s | ) | [static] |
Definition at line 16 of file console_socket.cpp.
Referenced by TestSocketClient().
00017 { 00018 wxString str(s); 00019 (void)str.Replace(_T("\t"), _T("\\t")); 00020 (void)str.Replace(_T("\n"), _T("\\n")); 00021 (void)str.Replace(_T("\r"), _T("\\r")); 00022 00023 return str; 00024 }
static wxString MyGetEnv | ( | const wxString & | var | ) | [static] |
Definition at line 32 of file console_socket.cpp.
Referenced by TestEnvironment().
00033 { 00034 wxString val; 00035 if ( !wxGetEnv(var, &val) ) 00036 val = _T("<empty>"); 00037 else 00038 val = wxString(_T('\'')) + val + _T('\''); 00039 00040 return val; 00041 }
static void TestDetachedThreads | ( | ) | [static] |
Definition at line 281 of file console_socket.cpp.
References gs_cond.
00282 { 00283 wxPuts(_T("\n*** Testing detached threads ***")); 00284 00285 static const size_t nThreads = 3; 00286 MyDetachedThread *threads[nThreads]; 00287 size_t n; 00288 for ( n = 0; n < nThreads; n++ ) 00289 { 00290 threads[n] = new MyDetachedThread(10, 'A' + n); 00291 } 00292 00293 threads[0]->SetPriority(WXTHREAD_MIN_PRIORITY); 00294 threads[1]->SetPriority(WXTHREAD_MAX_PRIORITY); 00295 00296 for ( n = 0; n < nThreads; n++ ) 00297 { 00298 threads[n]->Run(); 00299 } 00300 00301 // wait until all threads terminate 00302 gs_cond.Wait(); 00303 00304 wxPuts(wxEmptyString); 00305 }
static void TestEnvironment | ( | ) | [static] |
Definition at line 43 of file console_socket.cpp.
References MyGetEnv().
Referenced by main().
00044 { 00045 const wxChar *var = _T("wxTestVar"); 00046 00047 wxPuts(_T("*** testing environment access functions ***")); 00048 00049 wxPrintf(_T("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); 00050 wxSetEnv(var, _T("value for wxTestVar")); 00051 wxPrintf(_T("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); 00052 wxSetEnv(var, _T("another value")); 00053 wxPrintf(_T("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); 00054 wxUnsetEnv(var); 00055 wxPrintf(_T("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str()); 00056 wxPrintf(_T("PATH = %s\n"), MyGetEnv(_T("PATH")).c_str()); 00057 }
static void TestJoinableThreads | ( | ) | [static] |
Definition at line 307 of file console_socket.cpp.
00308 { 00309 wxPuts(_T("\n*** Testing a joinable thread (a loooong calculation...) ***")); 00310 00311 // calc 10! in the background 00312 MyJoinableThread thread(10); 00313 thread.Run(); 00314 00315 wxPrintf(_T("\nThread terminated with exit code %lu.\n"), 00316 (unsigned long)thread.Wait()); 00317 }
static void TestSemaphore | ( | ) | [static] |
Definition at line 586 of file console_socket.cpp.
00587 { 00588 wxPuts(_T("*** Testing wxSemaphore class. ***")); 00589 00590 static const int SEM_LIMIT = 3; 00591 00592 wxSemaphore sem(SEM_LIMIT, SEM_LIMIT); 00593 ArrayThreads threads; 00594 00595 for ( int i = 0; i < 3*SEM_LIMIT; i++ ) 00596 { 00597 threads.Add(new MySemaphoreThread(i, &sem)); 00598 threads.Last()->Run(); 00599 } 00600 00601 for ( size_t n = 0; n < threads.GetCount(); n++ ) 00602 { 00603 threads[n]->Wait(); 00604 delete threads[n]; 00605 } 00606 }
static void TestSocketClient | ( | ) | [static] |
Definition at line 143 of file console_socket.cpp.
References ARC, and MakePrintable().
Referenced by main().
00144 { 00145 wxPuts(_T("*** Testing wxSocketClient ***\n")); 00146 00147 static const wxChar *hostname = _T("localhost"); 00148 00149 wxIPV4address addr; 00150 addr.Hostname(hostname); 00151 addr.Service(3000); 00152 00153 wxPrintf(_T("Connecting to %s:3000...\n"), hostname); 00154 00155 wxSocketClient client; 00156 if ( !client.Connect(addr) ) 00157 { 00158 wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); 00159 } 00160 else 00161 { 00162 wxPrintf(_T("--- Connected to %s:%u...\n"), 00163 addr.Hostname().c_str(), addr.Service()); 00164 00165 wxChar buf[8192]; 00166 00167 char send_cmd[1024]; 00168 char cmd_args[1000]; 00169 char cmd[20]; 00170 int n; 00171 00172 n = sprintf(cmd, "F%02d:",ARC); 00173 n = sprintf(cmd_args, "%ld:%ld:%f:%f|",42,84,3.1415,2.784); 00174 n = sprintf(send_cmd, "%s%s",cmd,cmd_args); 00175 00176 // could use simply "GET" here I suppose 00177 wxString cmdGet = 00178 wxString::Format(_T("%s"), send_cmd); 00179 client.Write(cmdGet, cmdGet.length()); 00180 wxPrintf(_T("Sent command '%s'\n"), MakePrintable(cmdGet).c_str()); 00181 // client.Read(buf, WXSIZEOF(buf)); 00182 // wxPrintf(_T("--- Server replied:\n%s"), buf); 00183 } 00184 }
static void TestSocketServer | ( | ) | [static] |
Definition at line 68 of file console_socket.cpp.
00069 { 00070 wxPuts(_T("*** Testing wxSocketServer ***\n")); 00071 00072 static const int PORT = 3000; 00073 00074 wxIPV4address addr; 00075 addr.Service(PORT); 00076 00077 wxSocketServer *server = new wxSocketServer(addr); 00078 if ( !server->Ok() ) 00079 { 00080 wxPuts(_T("ERROR: failed to bind")); 00081 00082 return; 00083 } 00084 00085 bool quit = false; 00086 while ( !quit ) { 00087 wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT); 00088 00089 wxSocketBase *socket = server->Accept(); 00090 if ( !socket ) { 00091 wxPuts(_T("ERROR: wxSocketServer::Accept() failed.")); 00092 break; 00093 } 00094 00095 wxPuts(_T("Server: got a client.")); 00096 00097 server->SetTimeout(60); // 1 min 00098 00099 bool close = false; 00100 while ( !close && socket->IsConnected() ) { 00101 wxString s; 00102 wxChar ch = _T('\0'); 00103 for ( ;; ) { 00104 if ( socket->Read(&ch, sizeof(ch)).Error() ) { 00105 // don't log error if the client just close the connection 00106 if ( socket->IsConnected() ) { 00107 wxPuts(_T("ERROR: in wxSocket::Read.")); 00108 } 00109 break; 00110 } 00111 if ( ch == '\r' ) 00112 continue; 00113 if ( ch == '\n' ) 00114 break; 00115 s += ch; 00116 }//for 00117 if ( ch != '\n' ) { 00118 break; 00119 }//if not \n 00120 wxPrintf(_T("Server: got '%s'.\n"), s.c_str()); 00121 if ( s == _T("close") ) { 00122 wxPuts(_T("Closing connection")); 00123 close = true; 00124 } else if ( s == _T("quit") ) { 00125 close = quit = true; 00126 wxPuts(_T("Shutting down the server")); 00127 } else { // not a special command 00128 socket->Write(s.MakeUpper().c_str(), s.length()); 00129 socket->Write("\r\n", 2); 00130 wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str()); 00131 }//if command = close 00132 }//while not closed & connected 00133 if ( !close ) { 00134 wxPuts(_T("Server: lost a client unexpectedly.")); 00135 }//if not closed 00136 socket->Destroy(); 00137 }//while not quit 00138 00139 // same as "delete server" but is consistent with GUI programs 00140 server->Destroy(); 00141 }
static void TestThreadConditions | ( | ) | [static] |
Definition at line 450 of file console_socket.cpp.
References gs_cond.
00451 { 00452 wxMutex mutex; 00453 wxCondition condition(mutex); 00454 00455 // otherwise its difficult to understand which log messages pertain to 00456 // which condition 00457 //wxLogTrace(_T("thread"), _T("Local condition var is %08x, gs_cond = %08x"), 00458 // condition.GetId(), gs_cond.GetId()); 00459 00460 // create and launch threads 00461 MyWaitingThread *threads[10]; 00462 00463 size_t n; 00464 for ( n = 0; n < WXSIZEOF(threads); n++ ) 00465 { 00466 threads[n] = new MyWaitingThread( &mutex, &condition ); 00467 } 00468 00469 for ( n = 0; n < WXSIZEOF(threads); n++ ) 00470 { 00471 threads[n]->Run(); 00472 } 00473 00474 // wait until all threads run 00475 wxPuts(_T("Main thread is waiting for the other threads to start")); 00476 fflush(stdout); 00477 00478 size_t nRunning = 0; 00479 while ( nRunning < WXSIZEOF(threads) ) 00480 { 00481 gs_cond.Wait(); 00482 00483 nRunning++; 00484 00485 wxPrintf(_T("Main thread: %u already running\n"), nRunning); 00486 fflush(stdout); 00487 } 00488 00489 wxPuts(_T("Main thread: all threads started up.")); 00490 fflush(stdout); 00491 00492 wxThread::Sleep(500); 00493 00494 #if 1 00495 // now wake one of them up 00496 wxPrintf(_T("Main thread: about to signal the condition.\n")); 00497 fflush(stdout); 00498 condition.Signal(); 00499 #endif 00500 00501 wxThread::Sleep(200); 00502 00503 // wake all the (remaining) threads up, so that they can exit 00504 wxPrintf(_T("Main thread: about to broadcast the condition.\n")); 00505 fflush(stdout); 00506 condition.Broadcast(); 00507 00508 // give them time to terminate (dirty!) 00509 wxThread::Sleep(500); 00510 }
static void TestThreadDelete | ( | ) | [static] |
Definition at line 358 of file console_socket.cpp.
00359 { 00360 // As above, using Sleep() is only for testing here - we must use some 00361 // synchronisation object instead to ensure that the thread is still 00362 // running when we delete it - deleting a detached thread which already 00363 // terminated will lead to a crash! 00364 00365 wxPuts(_T("\n*** Testing thread delete function ***")); 00366 00367 MyDetachedThread *thread0 = new MyDetachedThread(30, 'W'); 00368 00369 thread0->Delete(); 00370 00371 wxPuts(_T("\nDeleted a thread which didn't start to run yet.")); 00372 00373 MyDetachedThread *thread1 = new MyDetachedThread(30, 'Y'); 00374 00375 thread1->Run(); 00376 00377 wxThread::Sleep(300); 00378 00379 thread1->Delete(); 00380 00381 wxPuts(_T("\nDeleted a running thread.")); 00382 00383 MyDetachedThread *thread2 = new MyDetachedThread(30, 'Z'); 00384 00385 thread2->Run(); 00386 00387 wxThread::Sleep(300); 00388 00389 thread2->Pause(); 00390 00391 thread2->Delete(); 00392 00393 wxPuts(_T("\nDeleted a sleeping thread.")); 00394 00395 MyJoinableThread thread3(20); 00396 thread3.Run(); 00397 00398 thread3.Delete(); 00399 00400 wxPuts(_T("\nDeleted a joinable thread.")); 00401 00402 MyJoinableThread thread4(2); 00403 thread4.Run(); 00404 00405 wxThread::Sleep(300); 00406 00407 thread4.Delete(); 00408 00409 wxPuts(_T("\nDeleted a joinable thread which already terminated.")); 00410 00411 wxPuts(wxEmptyString); 00412 }
static void TestThreadExec | ( | ) | [static] |
Definition at line 532 of file console_socket.cpp.
00533 { 00534 wxPuts(_T("*** Testing wxExecute interaction with threads ***\n")); 00535 00536 MyExecThread thread(_T("true")); 00537 thread.Run(); 00538 00539 wxPrintf(_T("Main program exit code: %ld.\n"), 00540 wxExecute(_T("false"), wxEXEC_SYNC)); 00541 00542 wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait()); 00543 }
static void TestThreadSuspend | ( | ) | [static] |
Definition at line 319 of file console_socket.cpp.
References gs_cond.
00320 { 00321 wxPuts(_T("\n*** Testing thread suspend/resume functions ***")); 00322 00323 MyDetachedThread *thread = new MyDetachedThread(15, 'X'); 00324 00325 thread->Run(); 00326 00327 // this is for this demo only, in a real life program we'd use another 00328 // condition variable which would be signaled from wxThread::Entry() to 00329 // tell us that the thread really started running - but here just wait a 00330 // bit and hope that it will be enough (the problem is, of course, that 00331 // the thread might still not run when we call Pause() which will result 00332 // in an error) 00333 wxThread::Sleep(300); 00334 00335 for ( size_t n = 0; n < 3; n++ ) 00336 { 00337 thread->Pause(); 00338 00339 wxPuts(_T("\nThread suspended")); 00340 if ( n > 0 ) 00341 { 00342 // don't sleep but resume immediately the first time 00343 wxThread::Sleep(300); 00344 } 00345 wxPuts(_T("Going to resume the thread")); 00346 00347 thread->Resume(); 00348 } 00349 00350 wxPuts(_T("Waiting until it terminates now")); 00351 00352 // wait until the thread terminates 00353 gs_cond.Wait(); 00354 00355 wxPuts(wxEmptyString); 00356 }
WX_DEFINE_ARRAY_PTR | ( | wxThread * | , | |
ArrayThreads | ||||
) |
wxSemaphore gs_cond [static] |
Definition at line 190 of file console_socket.cpp.
Referenced by MyWaitingThread::Entry(), MyDetachedThread::OnExit(), TestDetachedThreads(), TestThreadConditions(), and TestThreadSuspend().
size_t gs_counter = (size_t)-1 [static] |
Definition at line 188 of file console_socket.cpp.
Referenced by MyDetachedThread::Entry(), and MyDetachedThread::OnExit().
wxCriticalSection gs_critsect [static] |
Definition at line 189 of file console_socket.cpp.
Referenced by MyDetachedThread::Entry(), and MyDetachedThread::OnExit().