1#include <cstdlib> 2#include <cstring> 3#include <cstdio> 4#include <iostream> 5 6#include <AppKit.h> 7#include <Debug.h> 8#include <KernelKit.h> 9#include <NetworkKit.h> 10 11 12class TestSyncListener : public BUrlProtocolListener { 13public: 14 void ConnectionOpened(BUrlProtocol* caller) { 15 printf("Thread<#%5d> ", find_thread(NULL)); 16 printf("TestSyncListener::ConnectionOpened(%p)\n", caller); 17 } 18}; 19 20 21class TestAsyncListener : public BUrlProtocolAsynchronousListener { 22public: 23 TestAsyncListener(bool transparent) 24 : 25 BUrlProtocolAsynchronousListener(transparent) 26 { } 27 28 void ConnectionOpened(BUrlProtocol* caller) { 29 printf("Thread<#%5d> ", find_thread(NULL)); 30 printf("TestAsyncListener::ConnectionOpened(%p)\n", caller); 31 } 32}; 33 34 35class Test { 36 // Synchronous listener 37 TestSyncListener s; 38 39 // Asynchronous listener with dispatcher not embedded 40 TestAsyncListener a; 41 BUrlProtocolDispatchingListener a_sync; 42 43 // Asynchronous listener with embedded dispatcher 44 TestAsyncListener a_transparent; 45 46public: 47 Test() 48 : 49 a(false), 50 a_sync(&a), 51 a_transparent(true) 52 { } 53 54 void testListener(BUrlProtocolListener* listener) 55 { 56 listener->ConnectionOpened((BUrlProtocol*)0xdeadbeef); 57 } 58 59 60 void DoTest() { 61 // Tests 62 printf("Launching test from thread #%5ld\n", find_thread(NULL)); 63 testListener(&s); 64 testListener(&a_sync); 65 testListener(a_transparent.SynchronousListener()); 66 } 67}; 68 69 70class TestThread { 71 Test t; 72 thread_id fThreadId; 73 74public: 75 thread_id Run() { 76 fThreadId = spawn_thread(&TestThread::_ThreadEntry, "TestThread", 77 B_NORMAL_PRIORITY, this); 78 79 if (fThreadId < B_OK) 80 return fThreadId; 81 82 status_t launchErr = resume_thread(fThreadId); 83 84 if (launchErr < B_OK) 85 return launchErr; 86 87 return fThreadId; 88 } 89 90 static status_t _ThreadEntry(void* ptr) { 91 TestThread* parent = (TestThread*)ptr; 92 93 parent->t.DoTest(); 94 return B_OK; 95 } 96}; 97 98 99class TestApp : public BApplication { 100 Test t; 101 TestThread t2; 102 103public: 104 TestApp() 105 : 106 BApplication("application/x-vnd.urlprotocollistener-test") 107 { } 108 109 void ReadyToRun() { 110 t2.Run(); 111 t.DoTest(); 112 SetPulseRate(1000000); 113 } 114 115 void Pulse() { 116 static int count = 0; 117 count++; 118 119 if (count == 1) { 120 Quit(); 121 } 122 } 123}; 124 125 126int 127main(int, char**) 128{ 129 new TestApp(); 130 131 // Let the async calls be handled 132 be_app->Run(); 133 134 delete be_app; 135 return EXIT_SUCCESS; 136} 137