xref: /haiku/src/servers/app/TestServerLoopAdapter.cpp (revision 5e7964b0a929555415798dea3373db9ac4611caa)
1 /*
2  * Copyright 2001-2015, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		DarkWyrm <bpmagic@columbus.rr.com>
7  *		Axel Dörfler, axeld@pinc-software.de
8  *		Stephan Aßmus <superstippi@gmx.de>
9  * 		Christian Packmann
10  *		Julian Harnath <julian.harnath@rwth-aachen.de>
11  */
12 #include "TestServerLoopAdapter.h"
13 
14 #include "Desktop.h"
15 #include "ServerConfig.h"
16 #include "ServerProtocol.h"
17 
18 #include <PortLink.h>
19 
20 #include <stdio.h>
21 
22 //#define DEBUG_SERVER
23 #ifdef DEBUG_SERVER
24 #	include <stdio.h>
25 #	define STRACE(x) printf x
26 #else
27 #	define STRACE(x) ;
28 #endif
29 
30 
31 TestServerLoopAdapter::TestServerLoopAdapter(const char* signature,
32 	const char*, port_id, bool, status_t* outError)
33 	:
34 	MessageLooper("test-app_server"),
35 	fMessagePort(_CreatePort())
36 {
37 	fLink.SetReceiverPort(fMessagePort);
38 	*outError = B_OK;
39 }
40 
41 
42 TestServerLoopAdapter::~TestServerLoopAdapter()
43 {
44 }
45 
46 
47 status_t
48 TestServerLoopAdapter::Run()
49 {
50  	rename_thread(find_thread(NULL), "picasso");
51 	_message_thread((void*)this);
52 	return B_OK;
53 }
54 
55 
56 void
57 TestServerLoopAdapter::_DispatchMessage(int32 code,
58 	BPrivate::LinkReceiver& link)
59 {
60 	switch (code) {
61 		case AS_GET_DESKTOP:
62 		{
63 			port_id replyPort = 0;
64 			link.Read<port_id>(&replyPort);
65 
66 			int32 userID = -1;
67 			link.Read<int32>(&userID);
68 
69 			char* targetScreen = NULL;
70 			link.ReadString(&targetScreen);
71 
72 			int32 version = -1;
73 			link.Read<int32>(&version);
74 
75  			BMessage message(AS_GET_DESKTOP);
76 			message.AddInt32("user", userID);
77 			message.AddInt32("version", version);
78 			message.AddString("target", targetScreen);
79  			MessageReceived(&message);
80 
81  			// AppServer will try to send a reply, we just let that fail
82  			// since we can find out the port by getting the desktop instance
83  			// ourselves
84 
85 			Desktop* desktop = _FindDesktop(userID, targetScreen);
86 			free(targetScreen);
87 
88 			BPrivate::LinkSender reply(replyPort);
89 			if (desktop != NULL) {
90 				reply.StartMessage(B_OK);
91 				reply.Attach<port_id>(desktop->MessagePort());
92 			} else
93 			reply.StartMessage(B_ERROR);
94 
95 			reply.Flush();
96 
97 			break;
98 		}
99 
100 		case B_QUIT_REQUESTED:
101 		{
102 			QuitRequested();
103 			break;
104 		}
105 
106 		default:
107 			STRACE(("Server::MainLoop received unexpected code %" B_PRId32 " "
108 				"(offset %" B_PRId32 ")\n", code, code - SERVER_TRUE));
109 			break;
110 	}
111 }
112 
113 
114 port_id
115 TestServerLoopAdapter::_CreatePort()
116 {
117 	port_id port = create_port(DEFAULT_MONITOR_PORT_SIZE, SERVER_PORT_NAME);
118 	if (port < B_OK)
119 		debugger("test-app_server could not create message port");
120 	return port;
121 }
122