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