void Run() { NPT_Debug("Thread3::Run - start\n"); NPT_Thread::SetCurrentThreadPriority(NPT_THREAD_PRIORITY_ABOVE_NORMAL); // sleep a while NPT_TimeInterval duration(3.1f); NPT_System::Sleep(duration); NPT_Debug("Thread3::Run - setting shared var to 1\n"); m_SharedVariable->SetValue(1); // sleep a while NPT_System::Sleep(duration); NPT_Debug("Thread3::Run - setting shared var to 2\n"); m_SharedVariable->SetValue(2); // sleep a while NPT_System::Sleep(duration); NPT_Debug("Thread3::Run - setting shared var to 3\n"); m_SharedVariable->SetValue(3); NPT_Debug("Thread3::Run - end\n"); }
/*---------------------------------------------------------------------- | TestHttp +---------------------------------------------------------------------*/ static NPT_Result TestHttp() { NPT_HttpServer server; NPT_InputStreamReference input; NPT_OutputStreamReference output; NPT_SocketInfo client_info; NPT_HttpStaticRequestHandler* static_handler = new NPT_HttpStaticRequestHandler("<HTML><H1>Hello World</H1></HTML>", "text/html"); server.AddRequestHandler(static_handler, "/test", false); TestHandler* test_handler = new TestHandler(); server.AddRequestHandler(test_handler, "/test2", false); NPT_HttpFileRequestHandler* file_handler = new NPT_HttpFileRequestHandler("/temp", "c:\\Temp"); server.AddRequestHandler(file_handler, "/temp", true); NPT_Result result = server.WaitForNewClient(input, output, client_info); NPT_Debug("WaitForNewClient returned %d\n", result); if (NPT_FAILED(result)) return result; result = server.RespondToClient(input, output, client_info); NPT_Debug("ResponToClient returned %d\n", result); delete static_handler; delete file_handler; return result; }
/*---------------------------------------------------------------------- | UdpServerLoop +---------------------------------------------------------------------*/ static void UdpServerLoop(int port) { NPT_UdpSocket listener; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", port); } NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port)); if (NPT_FAILED(result)) { NPT_Debug("ERROR: Bind() failed (%d)\n", result); } // packet loop NPT_DataBuffer packet(32768); NPT_SocketAddress address; do { result = listener.Receive(packet, &address); if (NPT_SUCCEEDED(result)) { if (Options.verbose) { NPT_String ip = address.GetIpAddress().ToString(); NPT_Debug("Received %d bytes from %s:%d\n", packet.GetDataSize(), ip.GetChars(), address.GetPort()); } listener.Send(packet, &address); } } while (NPT_SUCCEEDED(result)); }
void Run() { NPT_Debug("*** T1 running ***\n"); NPT_TimeInterval duration(1.0f); NPT_Debug("*** T1 sleeping ***\n"); NPT_System::Sleep(duration); NPT_Debug("*** T1 done ***\n"); }
void Run() { NPT_Debug("Thread1::Run - start\n"); // sleep a while NPT_TimeInterval duration(1.2f); NPT_System::Sleep(duration); NPT_Debug("Thread1::Run - end\n"); }
/*---------------------------------------------------------------------- | GetEndPointUdpSocket +---------------------------------------------------------------------*/ static NPT_Result GetEndPointUdpSocket(EndPoint* endpoint, NPT_UdpSocket*& udp_socket) { // default return values udp_socket = NULL; switch (endpoint->type) { case ENDPOINT_TYPE_UDP_SERVER: { udp_socket = new NPT_UdpSocket(); // info if (Options.verbose) { NPT_Debug("listening on port %d", endpoint->info.udp_server.port); } // listen on port, any addr return udp_socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.udp_server.port)); } break; case ENDPOINT_TYPE_MULTICAST_SERVER: { NPT_UdpMulticastSocket* udp_multicast_socket = new NPT_UdpMulticastSocket(); udp_socket = udp_multicast_socket; // info if (Options.verbose) { NPT_Debug("listening on port %d\n", endpoint->info.multicast_server.port); } // listen on port, any addr NPT_CHECK(udp_socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, endpoint->info.multicast_server.port))); // info if (Options.verbose) { NPT_Debug("joining multicast group %s\n", endpoint->info.multicast_server.groupname); } // resolve name NPT_IpAddress address; NPT_CHECK(address.ResolveName(endpoint->info.multicast_server.groupname)); // join the group NPT_CHECK(udp_multicast_socket->JoinGroup(address)); return NPT_SUCCESS; } break; default: return NPT_FAILURE; } return NPT_SUCCESS; }
void Run() { NPT_Debug("Thread4::Run - start\n"); // change the prio NPT_Thread::SetCurrentThreadPriority(NPT_THREAD_PRIORITY_BELOW_NORMAL); // sleep a while NPT_TimeInterval duration(4.3f); NPT_System::Sleep(duration); NPT_Debug("Thread4::Run - end\n"); }
void Run() { for (int i=0; i<10;) { NPT_Debug("CBR2: processing [%d]\n", i); NPT_Result result = m_Slot.ReceiveCallback(*this); if (result == NPT_ERROR_CALLBACK_NOTHING_PENDING) { } else { i++; } NPT_Debug("CBR2: sleeping\n"); NPT_System::Sleep(NPT_TimeInterval(0.2f)); } NPT_Debug("CBR2: shutting down\n"); m_Slot.Shutdown(); }
void Run() { NPT_Debug("Thread2::Run - start\n"); // sleep a while NPT_TimeInterval duration(2.1f); NPT_System::Sleep(duration); NPT_Debug("Thread2::Run - waiting for variable == 3\n"); m_SharedVariable->WaitUntilEquals(3); NPT_Debug("Thread2::Run - end\n"); NPT_Debug("Thread2::Run - deleting myself\n"); //FIXME: This causes a crash //delete this; }
/*---------------------------------------------------------------------- | TestUrlParser +---------------------------------------------------------------------*/ static void TestUrlParser(const char* url) { NPT_HttpUrl url_object(url); NPT_Debug("Parsing URL: '%s'\n", url ? url : "null"); if (url_object.IsValid()) { NPT_Debug(" --> host=%s, port=%d, path=%s\n", (const char*)url_object.GetHost(), url_object.GetPort(), (const char*)url_object.GetPath()); } else { NPT_Debug(" --> Invalid URL\n"); } }
/*---------------------------------------------------------------------- | NPT_GenericQueue::CreateInstance +---------------------------------------------------------------------*/ NPT_GenericQueue* NPT_GenericQueue::CreateInstance(NPT_Cardinal max_items) { NPT_Debug(":: NPT_GenericQueue::CreateInstance - queue max_items = %ld\n", max_items); return new NPT_PSPQueue(max_items); }
/*---------------------------------------------------------------------- | TestCanonicalizer +---------------------------------------------------------------------*/ static void TestCanonicalizer() { extern const char* xml_cano_1[]; NPT_XmlParser parser(true); // do not ignore whitespaces NPT_XmlNode* root; for (unsigned int i=0; xml_cano_1[i]; i+=2) { const char* xml_in = xml_cano_1[i]; const char* xml_out = xml_cano_1[i+1]; CHECK(NPT_SUCCEEDED(parser.Parse(xml_in, root))); CHECK(root); NPT_XmlCanonicalizer canonicalizer; NPT_MemoryStream buffer1; NPT_Result result = canonicalizer.Serialize(*root, buffer1); NPT_String str((const char*)buffer1.GetData(), buffer1.GetDataSize()); NPT_Debug("%s", str.GetChars()); CHECK(str == xml_out); delete root; CHECK(NPT_SUCCEEDED(parser.Parse(str, root))); CHECK(root); NPT_MemoryStream buffer2; result = canonicalizer.Serialize(*root, buffer2); CHECK(buffer1.GetBuffer() == buffer2.GetBuffer()); delete root; } }
/*---------------------------------------------------------------------- | NPT_PSPQueue::NPT_PSPQueue +---------------------------------------------------------------------*/ NPT_PSPQueue::NPT_PSPQueue(NPT_Cardinal max_items) : m_MaxItems(max_items) { NPT_Debug(":: NPT_PSPQueue::NPT_PSPQueue\n"); //pthread_cond_init(&m_CanPushOrPopCondition, NULL); }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { NPT_COMPILER_UNUSED(argc); NPT_COMPILER_UNUSED(argv); #if defined(WIN32) && defined(_DEBUG) _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetAllocHook(AllocHook); #endif TestSharedVariables(); TestPrio(); Test3(100000, 0.0f, 0.0f); Test3(300, 0.1f, 0.0f); Test3(100, 0.5f, 0.4f); Test4(); Test1(); Test2(); NPT_Debug("- program done -\n"); return 0; }
void OnCallback(void* args) { _CB_T* t_args = (_CB_T*)args; CHECK(*t_args->var == -1); (*t_args->var)+= t_args->var_i; _count_CBR[m_Var]++; m_FlipFlop = true; if (m_Sleep != 0.0f) { NPT_Debug(".CBR [%d] - on callback (%d)\n", m_Var, _count_CBR[m_Var]); } }
static void TestPrio() { PrioThread p1(NPT_THREAD_PRIORITY_NORMAL); PrioThread p2(NPT_THREAD_PRIORITY_BELOW_NORMAL); PrioThread p3(NPT_THREAD_PRIORITY_ABOVE_NORMAL); NPT_Thread t1(p1); NPT_Thread t2(p2); NPT_Thread t3(p3); t1.Start(); t2.Start(); t3.Start(); t1.Wait(); t2.Wait(); t3.Wait(); NPT_Debug("### Prio NORMAL -> %lld iterations\n", p1.m_Counter); NPT_Debug("### Prio BELOW NORMAL -> %lld iterations\n", p2.m_Counter); NPT_Debug("### Prio ABOVE NORMAL -> %lld iterations\n", p3.m_Counter); }
void Run() { volatile int res = -1; for (int i=0; i<m_Cycles; i++) { _CB_T args = {&res, m_Var+1}; if (m_Sleep != 0.0f) { NPT_Debug("@CBR [%d] - calling back\n", m_Var); } NPT_Result result = m_Slot.SendCallback(&args); if (result == NPT_ERROR_CALLBACK_HANDLER_SHUTDOWN) { NPT_Debug("SHUTDOWN\n"); return; } CHECK(res == m_Var); res -= (m_Var+1); if (m_Sleep != 0.0f) { NPT_Debug("@CBR [%d] - sleeping\n", m_Var); } if (m_Sleep != 0.0f) NPT_System::Sleep(NPT_TimeInterval(m_Sleep)); } }
NPT_Result SetupResponse(NPT_HttpRequest& request, NPT_HttpResponse& response, NPT_SocketInfo& /*info*/) { NPT_String msg = "<HTML>"; msg += "PATH="; msg += request.GetUrl().GetPath(); msg += " <P><UL>"; if (request.GetUrl().HasQuery()) { NPT_HttpUrlQuery query(request.GetUrl().GetQuery()); for (NPT_List<NPT_HttpUrlQuery::Field>::Iterator it = query.GetFields().GetFirstItem(); it; ++it) { NPT_HttpUrlQuery::Field& field = *it; msg += "<LI>"; msg += field.m_Name; msg += " = "; msg += field.m_Value; msg += " </LI>"; } } msg += "</UL></HTML>"; if (request.GetMethod() == NPT_HTTP_METHOD_POST) { NPT_DataBuffer request_body; request.GetEntity()->Load(request_body); NPT_Debug("REQUEST: body = %d bytes\n", request_body.GetDataSize()); NPT_Debug("REQUEST: content type = %s\n", request.GetEntity()->GetContentType().GetChars()); if (request.GetEntity()->GetContentType().StartsWith("text") || request.GetEntity()->GetContentType() == "application/x-www-form-urlencoded") { NPT_String body_string; body_string.Assign((char*)request_body.GetData(), request_body.GetDataSize()); NPT_Debug("%s", body_string.GetChars()); } } NPT_HttpEntity* entity = response.GetEntity(); entity->SetContentType("text/html"); entity->SetInputStream(msg); return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | NPT_PSPThread::EntryPoint +---------------------------------------------------------------------*/ int NPT_PSPThread::EntryPoint(SceSize /* argument_size */, void* argument) { NPT_PSPThread** pthread = (NPT_PSPThread**)argument; NPT_PSPThread* thread = *pthread; //NPT_PSPThread* thread = reinterpret_cast<NPT_PSPThread*>(*argument); NPT_Debug(NPT_LOG_LEVEL_1, ":: NPT_PSPThread::EntryPoint - in =======================\n"); // run the thread thread->Run(); NPT_Debug(NPT_LOG_LEVEL_1, ":: NPT_PSPThread::EntryPoint - out ======================\n"); // we're done with the thread object thread->Terminate(); // done return 0; }
/*---------------------------------------------------------------------- | TestHttpTimeouts +---------------------------------------------------------------------*/ static void TestHttpTimeouts(const char* arg) { NPT_HttpUrl url(arg); NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET); NPT_HttpClient client; NPT_HttpResponse* response; NPT_Debug("### TIMEOUTS START ###\n"); client.SetTimeouts(10000, 10000, 10000); NPT_TimeStamp before, after; NPT_System::GetCurrentTimeStamp(before); NPT_Result result = client.SendRequest(request, response); NPT_Debug("SendRequest returned %d (%s)\n", result, NPT_ResultText(result)); NPT_System::GetCurrentTimeStamp(after); NPT_Debug("time elapsed: %d ms\n", (after-before).ToMillis()); client.SetTimeouts(5000, 5000, 5000); NPT_System::GetCurrentTimeStamp(before); result = client.SendRequest(request, response); NPT_Debug("SendRequest returned %d (%s)\n", result, NPT_ResultText(result)); NPT_System::GetCurrentTimeStamp(after); NPT_Debug("time elapsed: %d ms\n", (after-before).ToMillis()); NPT_Debug("--- TIMEOUTS END ---\n"); delete response; }
/*---------------------------------------------------------------------- | TestFile +---------------------------------------------------------------------*/ static void TestFile(const char* filename) { NPT_File* input; NPT_InputStreamReference stream; NPT_Result result; // open the input file input = new NPT_File(filename); result = input->Open(NPT_FILE_OPEN_MODE_READ); if (NPT_FAILED(result)) { NPT_Debug("XmtTest1:: cannot open input (%d)\n", result); return; } result = input->GetInputStream(stream); // parse the buffer NPT_XmlParser parser; NPT_XmlNode* tree; result = parser.Parse(*stream, tree); if (NPT_FAILED(result)) { NPT_Debug("XmlTest1:: cannot parse input (%d)\n", result); return; } // dump the tree NPT_XmlWriter writer(2); NPT_File output(NPT_FILE_STANDARD_OUTPUT); output.Open(NPT_FILE_OPEN_MODE_WRITE); NPT_OutputStreamReference output_stream_ref; output.GetOutputStream(output_stream_ref); writer.Serialize(*tree, *output_stream_ref); // delete the tree delete tree; // delete the input delete input; }
void Run() { for (int i=0; i<m_Cycles;) { if (m_Sleep != 0.0f) { NPT_Debug(".CBR [%d] - processing\n", m_Var); } NPT_Result result = m_Slot.ReceiveCallback(*this); if (result == NPT_ERROR_CALLBACK_NOTHING_PENDING) { if (m_Sleep != 0.0f) { NPT_Debug(".CBR [%d] - nothing pending\n", m_Var); } } else { CHECK(result == NPT_SUCCESS); CHECK(m_FlipFlop == true); m_FlipFlop = false; i++; } if (m_Sleep != 0.0f) { NPT_Debug(".CBR [%d] - sleeping\n", m_Var); } if (m_Sleep != 0.0f) NPT_System::Sleep(NPT_TimeInterval(m_Sleep)); } }
/*---------------------------------------------------------------------- | Test2 +---------------------------------------------------------------------*/ static void Test2() { NPT_Debug("--- Test2 Start ---\n"); NPT_SharedVariable shv1(0); NPT_Thread* thread1 = new Thread1(); Thread2 t2(&shv1); NPT_Thread* thread2 = new NPT_Thread(t2, true); NPT_Thread* thread3 = new Thread3(&shv1); Thread4 t4; NPT_Thread* thread4 = new NPT_Thread(t4, false); NPT_Debug("starting thread1...\n"); thread1->Start(); NPT_Debug("starting thread2...\n"); thread2->Start(); NPT_Debug("starting thread3\n"); thread3->Start(); NPT_Debug("releasing thread3\n"); delete thread3; NPT_Debug("starting thread4\n"); thread4->Start(); NPT_Debug("deleting thread4\n"); delete thread4; NPT_Debug("deleting thread1...\n"); delete thread1; NPT_Debug("...done\n"); // sleep a while NPT_TimeInterval duration(15.0); NPT_System::Sleep(duration); NPT_Debug("--- Test2 End ---\n"); }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { // parse args if (argc != 2) { NPT_Debug("HttpClientTest2: missing URL argument\n"); return 1; } TestHttpGet(argv[1]); return 0; }
/*---------------------------------------------------------------------- | NPT_PSPThread::Start +---------------------------------------------------------------------*/ NPT_Result NPT_PSPThread::Start() { if (m_ThreadId > 0) { NPT_Debug(NPT_LOG_LEVEL_1, ":: NPT_PSPThread::Start already started !\n"); return NPT_FAILURE; } NPT_Debug(NPT_LOG_LEVEL_1, ":: NPT_PSPThread::Start - creating thread\n"); char thread_name[32]; sprintf(thread_name, "thread_%d", (int)NPT_System::GetSystem()->GetRandomInteger()); // create the native thread m_ThreadId = (SceUID) sceKernelCreateThread( thread_name, EntryPoint, SCE_KERNEL_USER_LOWEST_PRIORITY, 1024 * 16, 0, NULL); if (m_ThreadId <= 0) { // failed return NPT_FAILURE; } NPT_PSPThread* thread = this; int ret = sceKernelStartThread( m_ThreadId, sizeof(thread), &thread); if (ret != SCE_KERNEL_ERROR_OK) { return NPT_FAILURE; } return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | Test3 +---------------------------------------------------------------------*/ static void Test3(int cycles, float r_sleep, float w_sleep) { NPT_Debug("--- Test3 Start ---\n"); NPT_ThreadCallbackSlot slot; CBR cbr0(slot, 0, cycles, r_sleep); CBR cbr1(slot, 1, cycles, r_sleep); CBR cbr2(slot, 2, cycles, r_sleep); NPT_Thread* rt1 = new NPT_Thread(cbr0); NPT_Thread* rt2 = new NPT_Thread(cbr1); NPT_Thread* rt3 = new NPT_Thread(cbr2); CBW cbw0(slot, 0, cycles, w_sleep); CBW cbw1(slot, 1, cycles, w_sleep); CBW cbw2(slot, 2, cycles, w_sleep); NPT_Thread* wt1 = new NPT_Thread(cbw0); NPT_Thread* wt2 = new NPT_Thread(cbw1); NPT_Thread* wt3 = new NPT_Thread(cbw2); rt1->Start(); rt2->Start(); rt3->Start(); wt1->Start(); wt2->Start(); wt3->Start(); delete rt1; delete rt2; delete rt3; delete wt1; delete wt2; delete wt3; NPT_Debug("--- Test3: %d %d %d\n", _count_CBR[0], _count_CBR[1], _count_CBR[2]); NPT_Debug("--- Test3 End ---\n"); }
/*---------------------------------------------------------------------- | TcpServerLoop +---------------------------------------------------------------------*/ static void TcpServerLoop(int port) { NPT_TcpServerSocket listener; NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port)); if (NPT_FAILED(result)) { NPT_Debug("ERROR: Bind() failed (%d)\n", result); } NPT_Socket* client; for (;;) { NPT_Debug("waiting for client on port %d\n", port); NPT_Result result = listener.WaitForNewClient(client); NPT_SocketInfo socket_info; client->GetInfo(socket_info); NPT_Debug("client connected from %s:%d\n", socket_info.remote_address.GetIpAddress().ToString().GetChars(), socket_info.remote_address.GetPort()); NPT_InputStreamReference input; client->GetInputStream(input); NPT_OutputStreamReference output; client->GetOutputStream(output); do { char buffer[1024]; NPT_Size bytes_read; result = input->Read(buffer, sizeof(buffer), &bytes_read); if (NPT_SUCCEEDED(result)) { NPT_Debug("read %ld bytes\n", bytes_read); output->Write(buffer, bytes_read); } } while (NPT_SUCCEEDED(result)); delete client; } }
/*---------------------------------------------------------------------- | Test4 +---------------------------------------------------------------------*/ static void Test4() { NPT_Debug("--- Test4 Start ---\n"); NPT_ThreadCallbackSlot slot; CBR2 cbr(slot); NPT_Thread* t = new NPT_Thread(cbr); t->Start(); for (int i=0; i<20; i++) { NPT_Debug("Test4: calling back [%d]\n", i); NPT_Result result = slot.SendCallback(NULL); if (NPT_FAILED(result)) { CHECK(result == NPT_ERROR_CALLBACK_HANDLER_SHUTDOWN); CHECK(i >= 10); NPT_Debug("Test4: slot shutdown\n"); } } delete t; NPT_Debug("--- Test4 End ---\n"); }
/*---------------------------------------------------------------------- | TestHttpGet +---------------------------------------------------------------------*/ static void TestHttpGet(const char* arg, ShowMode mode) { NPT_HttpUrl url(arg); NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET); NPT_HttpClient client; NPT_HttpResponse* response; NPT_Result result = client.SendRequest(request, response); NPT_Debug("SendRequest returned %d\n", result); if (NPT_FAILED(result)) return; ShowResponse(response, mode); delete response; }
/*---------------------------------------------------------------------- | TestHttpGetWithProxy +---------------------------------------------------------------------*/ static void TestHttpGetWithProxy(const char* arg) { NPT_HttpUrl url(arg); NPT_HttpRequest request(url, NPT_HTTP_METHOD_GET); NPT_HttpClient client; NPT_HttpResponse* response; client.SetProxy("proxy", 8080); NPT_Result result = client.SendRequest(request, response); NPT_Debug("SendRequest returned %d\n", result); if (NPT_FAILED(result)) return; ShowResponse(response); delete response; }