xref: /haiku/src/tests/kits/app/messaging/first_light/main.cpp (revision 13ab4decd944a753514a5205e96b02ae553641e8)
1 //------------------------------------------------------------------------------
2 //	main.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <Application.h>
11 #include <Looper.h>
12 #include <Message.h>
13 #include <OS.h>
14 
15 // Project Includes ------------------------------------------------------------
16 
17 // Local Includes --------------------------------------------------------------
18 
19 // Local Defines ---------------------------------------------------------------
20 #ifdef USE_BAPP
21 	#define DO_EXIT	be_app_messenger.SendMessage(B_QUIT_REQUESTED)
22 #else
23 	#define DO_EXIT release_sem(gThreadLock)
24 #endif
25 
26 // Globals ---------------------------------------------------------------------
27 #ifndef USE_BAPP
28 sem_id gThreadLock = create_sem(0, "gThreadLock");
29 #endif
30 
31 class TLooper1 : public BLooper
32 {
33 	public:
TLooper1()34 		TLooper1() : BLooper() {;}
MessageReceived(BMessage * msg)35 		void MessageReceived(BMessage* msg)
36 		{
37 			switch (msg->what)
38 			{
39 				case '2345':
40 					printf("Got message '2345' in %s\n", __PRETTY_FUNCTION__);
41 					DO_EXIT;
42 					break;
43 				default:
44 					BLooper::MessageReceived(msg);
45 			}
46 		}
47 };
48 
49 class TLooper2 : public BLooper
50 {
51 	public:
TLooper2(BMessenger target)52 		TLooper2(BMessenger target) : BLooper(), fTarget(target) {;}
MessageReceived(BMessage * msg)53 		void MessageReceived(BMessage* msg)
54 		{
55 			switch (msg->what)
56 			{
57 				case '1234':
58 					printf("Got message '1234' in %s\n", __PRETTY_FUNCTION__);
59 					fTarget.SendMessage('2345');
60 					break;
61 				default:
62 					BLooper::MessageReceived(msg);
63 					break;
64 			}
65 		}
66 
67 	private:
68 		BMessenger fTarget;
69 };
70 
main()71 int main()
72 {
73 	BLooper*	fLooper1 = new TLooper1;
74 	BLooper*	fLooper2 = new TLooper2(fLooper1);
75 	fLooper1->Run();
76 	fLooper2->Run();
77 	printf("Sending message '1234' in %s\n", __PRETTY_FUNCTION__);
78 	BMessenger(fLooper2).SendMessage('1234');
79 
80 	// Wait for loopers to finish
81 	acquire_sem(gThreadLock);
82 
83 	return 0;
84 }
85 
86 #if 0
87 class TestApp : public BApplication
88 {
89 	public:
90 		TestApp() : BApplication("application/x-vnd.FirstMessageTestApp")
91 		{
92 			fLooper1 = new TLooper1;
93 			fLooper2 = new TLooper2(fLooper1);
94 			fLooper1->Run();
95 			fLooper2->Run();
96 		}
97 		void ReadyToRun()
98 		{
99 			printf("Sending message '1234' in %s\n", __PRETTY_FUNCTION__);
100 			BMessenger(fLooper2).SendMessage('1234');
101 		}
102 
103 	private:
104 		BLooper*	fLooper1;
105 		BLooper*	fLooper2;
106 };
107 
108 int main()
109 {
110 	new TestApp;
111 	be_app->Run();
112 	delete be_app;
113 	return 0;
114 }
115 #endif
116 
117 /*
118  * $Log $
119  *
120  * $Id  $
121  *
122  */
123 
124