2 * Copyright (c) 1999-2000, Eric Moon.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "NodeSyncThread.h"
39 #include <MediaRoster.h>
41 __USE_CORTEX_NAMESPACE
43 // -------------------------------------------------------- //
45 // -------------------------------------------------------- //
47 NodeSyncThread::~NodeSyncThread() {
52 err
= kill_thread(m_thread
);
55 "! ~NodeSyncThread(): kill_thread(%" B_PRId32
"):\n"
60 // +++++ is a wait_for_thread() necessary?
64 err
= delete_port(m_port
);
67 "! ~NodeSyncThread(): delete_port(%" B_PRId32
"):\n"
74 delete [] m_portBuffer
;
79 NodeSyncThread::NodeSyncThread(
80 const media_node
& node
,
81 BMessenger
* messenger
) :
84 m_messenger(messenger
),
85 m_syncInProgress(false),
89 m_portBufferSize(sizeof(_sync_op
)) {
93 m_portBuffer
= new char[m_portBufferSize
];
96 m_portBufferSize
* 16,
97 "NodeSyncThread___port");
98 ASSERT(m_port
>= B_OK
);
100 m_thread
= spawn_thread(
105 ASSERT(m_thread
>= B_OK
);
106 resume_thread(m_thread
);
109 // -------------------------------------------------------- //
111 // -------------------------------------------------------- //
113 // trigger a sync operation: when 'perfTime' arrives
114 // for the node, a M_SYNC_COMPLETE message with the given
115 // position value will be sent, unless the sync operation
116 // times out, in which case M_TIMED_OUT will be sent.
118 status_t
NodeSyncThread::sync(
126 return B_NOT_ALLOWED
;
128 _sync_op op
= {perfTime
, position
, timeout
};
138 // -------------------------------------------------------- //
140 // -------------------------------------------------------- //
143 status_t
NodeSyncThread::_Sync(
145 ((NodeSyncThread
*)cookie
)->_sync();
151 void NodeSyncThread::_sync() {
152 ASSERT(m_port
>= B_OK
);
158 // WAIT FOR A REQUEST
160 ssize_t readCount
= read_port(
166 if(readCount
< B_OK
) {
168 "! NodeSyncThread::_sync(): read_port():\n"
170 strerror(readCount
)));
174 if(code
!= M_TRIGGER
) {
176 "! NodeSyncThread::sync(): unknown message code %" B_PRId32
181 // SERVICE THE REQUEST
182 const _sync_op
& op
= *(_sync_op
*)m_portBuffer
;
184 // pre-fill the message
185 BMessage
m(M_SYNC_COMPLETE
);
186 m
.AddInt32("nodeID", m_node
.node
);
187 m
.AddInt64("perfTime", op
.targetTime
);
188 m
.AddInt64("position", op
.position
);
191 status_t err
= BMediaRoster::Roster()->SyncToNode(
198 m
.what
= M_SYNC_FAILED
;
199 m
.AddInt32("error", err
);
202 err
= m_messenger
->SendMessage(&m
);
205 "! NodeSyncThread::_sync(): m_messenger->SendMessage():\n"
213 // END -- NodeSyncThread.cpp --