xref: /haiku/src/tests/kits/app/bmessenger/SMRemoteTargetApp.cpp (revision 3e216965baa8d58a67bf7372e2bfa13d999f5a9d)
1 // SMRemoteTargetApp.cpp
2 
3 #include <assert.h>
4 #include <stdio.h>
5 #include <string.h>
6 
7 #include <OS.h>
8 
9 #include "SMRemoteTargetApp.h"
10 #include "SMLooper.h"
11 
12 // debugging
13 //#define DBG(x) x
14 #define DBG(x)
15 #define OUT	printf
16 
17 int
18 main(int argc, char **argv)
19 {
20 DBG(OUT("REMOTE: main()\n"));
21 	assert(argc > 1);
22 	const char *portName = argv[1];
23 	bool preferred = (argc > 2 && !strcmp(argv[2], "preferred"));
24 	// create local port
25 	port_id localPort = create_port(5, "SMRemoteTargetApp port");
26 	assert(localPort >= 0);
27 DBG(OUT("REMOTE: local port created\n"));
28 	// create looper/handler
29 	SMHandler *handler = NULL;
30 	SMLooper *looper = new SMLooper;
31 	looper->Run();
32 	if (!preferred) {
33 		handler = new SMHandler;
34 		assert(looper->Lock());
35 		looper->AddHandler(handler);
36 		looper->Unlock();
37 	}
38 DBG(OUT("REMOTE: looper/handler created\n"));
39 	// find remote port
40 DBG(OUT("REMOTE: find remote port `%s'\n", portName));
41 	port_id remotePort = find_port(portName);
42 	assert(remotePort >= 0);
43 DBG(OUT("REMOTE: found remote port\n"));
44 	// send init message
45 	smrt_init initData;
46 	initData.port = localPort;
47 	initData.messenger = BMessenger(handler, looper);
48 	assert(write_port(remotePort, SMRT_INIT, &initData, sizeof(smrt_init))
49 		   == B_OK);
50 DBG(OUT("REMOTE: init message sent\n"));
51 	// loop until quit message
52 	bool running = true;
53 	while (running) {
54 		char buffer[sizeof(smrt_get_ready)];
55 		int32 code;
56 		ssize_t readSize = read_port(localPort, &code, buffer, sizeof(buffer));
57 		if (readSize < 0)
58 			running = readSize;
59 DBG(OUT("REMOTE: read port: %.4s\n", (char*)&code));
60 		switch (code) {
61 			case SMRT_GET_READY:
62 			{
63 DBG(OUT("REMOTE: SMRT_GET_READY\n"));
64 				assert(readSize == sizeof(smrt_get_ready));
65 				smrt_get_ready &data = *(smrt_get_ready*)buffer;
66 				looper->SetReplyDelay(data.reply_delay);
67 				looper->BlockUntil(data.unblock_time);
68 DBG(OUT("REMOTE: SMRT_GET_READY done\n"));
69 				break;
70 			}
71 			case SMRT_DELIVERY_SUCCESS_REQUEST:
72 			{
73 DBG(OUT("REMOTE: SMRT_DELIVERY_SUCCESS_REQUEST\n"));
74 				assert(readSize == 0);
75 				smrt_delivery_success data;
76 				data.success = looper->DeliverySuccess();
77 				assert(write_port(remotePort, SMRT_DELIVERY_SUCCESS_REPLY,
78 								  &data, sizeof(smrt_delivery_success))
79 					   == B_OK);
80 DBG(OUT("REMOTE: SMRT_DELIVERY_SUCCESS_REQUEST done\n"));
81 				break;
82 			}
83 			case SMRT_QUIT:
84 DBG(OUT("REMOTE: QUIT\n"));
85 			default:
86 if (code != SMRT_QUIT)
87 DBG(OUT("REMOTE: UNKNOWN COMMAND!\n"));
88 				running = false;
89 				break;
90 		}
91 	}
92 	// delete looper/handler
93 	if (looper) {
94 		looper->Lock();
95 		if (handler) {
96 			looper->RemoveHandler(handler);
97 			delete handler;
98 		}
99 		looper->Quit();
100 	}
101 DBG(OUT("REMOTE: looper/handler deleted\n"));
102 	// delete local port
103 	delete_port(localPort);
104 DBG(OUT("REMOTE: main() done\n"));
105 	return 0;
106 }
107 
108 
109