2 * Copyright 2002-2007, Marcus Overhagen. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include <DataExchange.h>
12 #include <Messenger.h>
16 #include <MediaMisc.h>
19 #define TIMEOUT 15000000 // 15 seconds timeout!
24 namespace dataexchange
{
27 static BMessenger sMediaServerMessenger
;
28 static BMessenger sMediaRosterMessenger
;
29 static port_id sMediaServerPort
;
30 static port_id sMediaAddonServerPort
;
32 static void find_media_server_port();
33 static void find_media_addon_server_port();
37 find_media_server_port()
39 sMediaServerPort
= find_port(MEDIA_SERVER_PORT_NAME
);
40 if (sMediaServerPort
< 0) {
41 TRACE("couldn't find sMediaServerPort\n");
42 sMediaServerPort
= BAD_MEDIA_SERVER_PORT
; // make this a unique number
48 find_media_addon_server_port()
50 sMediaAddonServerPort
= find_port(MEDIA_ADDON_SERVER_PORT_NAME
);
51 if (sMediaAddonServerPort
< 0) {
52 TRACE("couldn't find sMediaAddonServerPort\n");
53 sMediaAddonServerPort
= BAD_MEDIA_ADDON_SERVER_PORT
; // make this a unique number
62 InitServerDataExchange()
64 sMediaServerMessenger
= BMessenger(B_MEDIA_SERVER_SIGNATURE
);
65 find_media_server_port();
66 find_media_addon_server_port();
71 InitRosterDataExchange(const BMessenger
& rosterMessenger
)
73 sMediaRosterMessenger
= rosterMessenger
;
77 //! BMessage based data exchange with the local BMediaRoster
79 SendToRoster(BMessage
* msg
)
81 status_t status
= sMediaRosterMessenger
.SendMessage(msg
,
82 static_cast<BHandler
*>(NULL
), TIMEOUT
);
84 ERROR("SendToRoster: SendMessage failed: %s\n", strerror(status
));
85 DEBUG_ONLY(msg
->PrintToStream());
91 //! BMessage based data exchange with the media_server
93 SendToServer(BMessage
* msg
)
95 status_t status
= sMediaServerMessenger
.SendMessage(msg
,
96 static_cast<BHandler
*>(NULL
), TIMEOUT
);
98 ERROR("SendToServer: SendMessage failed: %s\n", strerror(status
));
99 DEBUG_ONLY(msg
->PrintToStream());
106 QueryServer(BMessage
& request
, BMessage
& reply
)
108 status_t status
= sMediaServerMessenger
.SendMessage(&request
, &reply
,
110 if (status
!= B_OK
) {
111 ERROR("QueryServer: SendMessage failed: %s\n", strerror(status
));
112 DEBUG_ONLY(request
.PrintToStream());
113 DEBUG_ONLY(reply
.PrintToStream());
119 //! Raw data based data exchange with the media_server
121 SendToServer(int32 msgCode
, command_data
* msg
, size_t size
)
123 return SendToPort(sMediaServerPort
, msgCode
, msg
, size
);
127 QueryServer(int32 msgCode
, request_data
* request
, size_t requestSize
,
128 reply_data
* reply
, size_t replySize
)
130 return QueryPort(sMediaServerPort
, msgCode
, request
, requestSize
, reply
,
135 //! Raw data based data exchange with the media_addon_server
137 SendToAddOnServer(int32 msgCode
, command_data
* msg
, size_t size
)
139 return SendToPort(sMediaAddonServerPort
, msgCode
, msg
, size
);
144 QueryAddOnServer(int32 msgCode
, request_data
* request
, size_t requestSize
,
145 reply_data
* reply
, size_t replySize
)
147 return QueryPort(sMediaAddonServerPort
, msgCode
, request
, requestSize
,
152 //! Raw data based data exchange with the media_server
154 SendToPort(port_id sendPort
, int32 msgCode
, command_data
* msg
, size_t size
)
156 status_t status
= write_port_etc(sendPort
, msgCode
, msg
, size
,
157 B_RELATIVE_TIMEOUT
, TIMEOUT
);
158 if (status
!= B_OK
) {
159 ERROR("SendToPort: write_port failed, msgcode 0x%" B_PRIx32
", port %"
160 B_PRId32
": %s\n", msgCode
, sendPort
, strerror(status
));
161 if (status
== B_BAD_PORT_ID
&& sendPort
== sMediaServerPort
) {
162 find_media_server_port();
163 sendPort
= sMediaServerPort
;
164 } else if (status
== B_BAD_PORT_ID
165 && sendPort
== sMediaAddonServerPort
) {
166 find_media_addon_server_port();
167 sendPort
= sMediaAddonServerPort
;
171 status
= write_port_etc(sendPort
, msgCode
, msg
, size
,
172 B_RELATIVE_TIMEOUT
, TIMEOUT
);
173 if (status
!= B_OK
) {
174 ERROR("SendToPort: retrying write_port failed, msgCode 0x%" B_PRIx32
175 ", port %" B_PRId32
": %s\n", msgCode
, sendPort
,
185 QueryPort(port_id requestPort
, int32 msgCode
, request_data
* request
,
186 size_t requestSize
, reply_data
* reply
, size_t replySize
)
188 status_t status
= write_port_etc(requestPort
, msgCode
, request
, requestSize
,
189 B_RELATIVE_TIMEOUT
, TIMEOUT
);
190 if (status
!= B_OK
) {
191 ERROR("QueryPort: write_port failed, msgcode 0x%" B_PRIx32
", port %"
192 B_PRId32
": %s\n", msgCode
, requestPort
, strerror(status
));
194 if (status
== B_BAD_PORT_ID
&& requestPort
== sMediaServerPort
) {
195 find_media_server_port();
196 requestPort
= sMediaServerPort
;
197 } else if (status
== B_BAD_PORT_ID
198 && requestPort
== sMediaAddonServerPort
) {
199 find_media_addon_server_port();
200 requestPort
= sMediaAddonServerPort
;
204 status
= write_port_etc(requestPort
, msgCode
, request
, requestSize
,
205 B_RELATIVE_TIMEOUT
, TIMEOUT
);
206 if (status
!= B_OK
) {
207 ERROR("QueryPort: retrying write_port failed, msgcode 0x%" B_PRIx32
208 ", port %" B_PRId32
": %s\n", msgCode
, requestPort
,
215 status
= read_port_etc(request
->reply_port
, &code
, reply
, replySize
,
216 B_RELATIVE_TIMEOUT
, TIMEOUT
);
218 ERROR("QueryPort: read_port failed, msgcode 0x%" B_PRIx32
", port %"
219 B_PRId32
": %s\n", msgCode
, request
->reply_port
, strerror(status
));
222 return status
< B_OK
? status
: reply
->result
;