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