1303947Ssephe/*
2303947Ssephe * Copyright (c) 1999-2000, Eric Moon.
3303947Ssephe * All rights reserved.
4303947Ssephe *
5303947Ssephe * Redistribution and use in source and binary forms, with or without
6303947Ssephe * modification, are permitted provided that the following conditions
7303947Ssephe * are met:
8303947Ssephe *
9303947Ssephe * 1. Redistributions of source code must retain the above copyright
10303947Ssephe *    notice, this list of conditions, and the following disclaimer.
11303947Ssephe *
12303947Ssephe * 2. Redistributions in binary form must reproduce the above copyright
13303947Ssephe *    notice, this list of conditions, and the following disclaimer in the
14303947Ssephe *    documentation and/or other materials provided with the distribution.
15303947Ssephe *
16303947Ssephe * 3. The name of the author may not be used to endorse or promote products
17303947Ssephe *    derived from this software without specific prior written permission.
18303947Ssephe *
19303947Ssephe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20303947Ssephe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21303947Ssephe * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22303947Ssephe * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23303947Ssephe * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24303947Ssephe * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25303947Ssephe * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26303947Ssephe * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27303947Ssephe * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28303947Ssephe * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29303947Ssephe */
30303947Ssephe
31303947Ssephe
32303947Ssephe// NodeSyncThread.h [rewrite 14oct99]
33303947Ssephe// * PURPOSE
34303947Ssephe//   Provide continuous synchronization notices on
35303947Ssephe//   a particular BMediaNode.  Notification is sent
36303947Ssephe//   to a provided BMessenger.
37303947Ssephe//
38303947Ssephe//   As long as a NodeSyncThread object exists its thread
39307475Ssephe//   is running.  The thread blocks indefinitely, waiting
40307475Ssephe//   for a message to show up on an internal port.
41303947Ssephe//
42303947Ssephe//   Sync-notice requests (via the +++++ sync() operation)
43303947Ssephe//   trigger a message sent to that port.  The thread wakes
44303947Ssephe//   up, then calls BMediaRoster::SyncToNode() to wait
45303947Ssephe//   until a particular performace time arrives for that node.
46303947Ssephe//
47303947Ssephe//   If SyncToNode() times out, an M_TIMED_OUT message is sent;
48307475Ssephe//   otherwise, an M_SYNC_COMPLETE message is sent.
49307475Ssephe//
50303947Ssephe// * HISTORY
51303947Ssephe//   e.moon		14oct99		Rewrite begun.
52303947Ssephe
53303947Ssephe#ifndef __NodeSyncThread_H__
54311356Ssephe#define __NodeSyncThread_H__
55311356Ssephe
56303947Ssephe#include <MediaNode.h>
57303947Ssephe#include <Messenger.h>
58307475Ssephe#include <OS.h>
59307475Ssephe
60303947Ssephe#include "cortex_defs.h"
61303947Ssephe__BEGIN_CORTEX_NAMESPACE
62
63class NodeSyncThread {
64public:													// *** messages
65	enum message_t {
66		// 'nodeID' (int32)         media_node_id value
67		// 'perfTime' (int64)				the performance time
68		// 'position' (int64)       corresponding (caller-provided) position
69		M_SYNC_COMPLETE							=NodeSyncThread_message_base,
70
71		// 'nodeID' (int32)         media_node_id value
72		// 'error' (int32)					status_t value from SyncToNode()
73		// 'perfTime' (int64)				the performance time
74		// 'position' (int64)       corresponding (caller-provided) position
75		M_SYNC_FAILED
76	};
77
78public:													// *** dtor/ctors
79	virtual ~NodeSyncThread();
80
81	NodeSyncThread(
82		const media_node&						node,
83		BMessenger*									messenger);
84
85public:													// *** operations
86	// trigger a sync operation: when 'perfTime' arrives
87	// for the node, a M_SYNC_COMPLETE message with the given
88	// position value will be sent,  unless the sync operation
89	// times out, in which case M_TIMED_OUT will be sent.
90	status_t sync(
91		bigtime_t										perfTime,
92		bigtime_t										position,
93		bigtime_t										timeout);
94
95private:
96
97	// the node to watch
98	media_node										m_node;
99
100	// the messenger to inform
101	BMessenger*										m_messenger;
102
103	// if the thread is running, it has exclusive write access
104	// to this flag.
105	volatile bool									m_syncInProgress;
106
107	// thread guts
108	thread_id											m_thread;
109	port_id												m_port;
110	char*													m_portBuffer;
111	ssize_t												m_portBufferSize;
112
113	enum _op_codes {
114		M_TRIGGER
115	};
116
117	struct _sync_op {
118		bigtime_t 	targetTime;
119		bigtime_t		position;
120		bigtime_t		timeout;
121	};
122
123	static status_t _Sync(
124		void*												cookie);
125	void _sync();
126};
127
128__END_CORTEX_NAMESPACE
129#endif /*__NodeSyncThread_H__*/
130