1/******************************************************************************
2
3	File:	BufferStreamManager.h
4
5	Copyright 1995-97, Be Incorporated
6
7******************************************************************************/
8#ifndef _BUFFER_STREAM_MANAGER_H
9#define _BUFFER_STREAM_MANAGER_H
10
11
12#include <OS.h>
13#include <SupportDefs.h>
14#include <Locker.h>
15
16#include "OldBufferStream.h"
17
18
19/* ================
20   Local Declarations
21   ================ */
22
23#define B_DEFAULT_BSTREAM_COUNT			3
24#define	B_DEFAULT_BSTREAM_SIZE			B_PAGE_SIZE
25#define B_DEFAULT_BSTREAM_DELAY			10000
26#define	B_DEFAULT_BSTREAM_TIMEOUT		5000000
27
28
29enum stream_state {
30  B_IDLE = 100,		/* stream is shut down */
31  B_RUNNING,		/* stream is running */
32  B_STOPPING		/* waiting for final buffer to return */
33  };
34
35
36/* ================
37   Class definition for BBufferStreamManager
38   ================ */
39
40
41class BBufferStreamManager {
42
43public:
44
45					BBufferStreamManager(char* name);
46	virtual			~BBufferStreamManager();
47
48
49	char			*Name() const;
50	BBufferStream	*Stream() const;
51
52	int32			BufferCount() const;
53	void			SetBufferCount(int32 count);
54
55	int32			BufferSize() const;
56	void			SetBufferSize(int32 bytes);
57
58  /* Get or set the minimum delay between sending out successive buffers.
59   * Although the StreamManager automatically shuts down when there
60   * are no more subscribers, setting the minimum delay can prevent
61   * prevent runaway streams.  A zero or negative value means no
62   * delay.
63   */
64	bigtime_t		BufferDelay() const;
65	void			SetBufferDelay(bigtime_t usecs);
66
67  /* If no Buffers return to the StreamManager within a period of time, the
68   * StreamManager will decide that one of the subscribers is broken and
69   * will go hunting for it.  When it finds the offending subscriber,
70   * it will be removed from the chain with impunity.
71   *
72   * The default is B_DEFAULT_TIMEOUT.  Setting the timeout to 0 or a
73   * negative number will disable this.
74   */
75	bigtime_t		Timeout() const;
76	void			SetTimeout(bigtime_t usecs);
77
78  /****************************************************************
79   * Control the running of the stream.
80   *
81   */
82
83  /* Set the pending state to B_RUNNING and, if required, start up
84   * the processing thread.  The processing thread will start
85   * emitting buffers to the stream.
86   */
87	stream_state	Start();
88
89  /* Set the pending state to B_STOPPING.  The processing thread will
90   * stop emitting new buffers to the stream, and when all buffers
91   * are accounted for, will automatically set the desired state
92   * to B_IDLE.
93   */
94	stream_state	Stop();
95
96  /* Set the desired state to B_IDLE.  The processing thread will
97   * stop immediately and all buffers will be "reclaimed" back
98   * to the StreamManager.
99   */
100	stream_state	Abort();
101
102  /* Return the current state of the stream (B_RUNNING, B_STOPPING, or B_IDLE).
103   */
104	stream_state	State() const;
105
106  /* When NotificationPort is set, the receiver will get a message
107   * whenever the state of the StreamManager changes.  The msg_id of the
108   * message will be the new state of the StreamManager.
109   */
110	port_id			NotificationPort() const;
111	void			SetNotificationPort(port_id port);
112
113  /* Lock the data structures associated with this StreamManager
114   */
115	bool			Lock();
116	void			Unlock();
117
118  /****************************************************************
119   * Subscribe functions
120   */
121
122	status_t		Subscribe(BBufferStream *stream);
123	status_t		Unsubscribe();
124	subscriber_id	ID() const;
125
126
127/* ================
128   Protected member functions.
129   ================ */
130
131protected:
132
133  /****************************************************************
134   *
135   * The processing thread.  This thread waits to acquire a Buffer
136   * (or for the timeout to expire) and takes appropriate action.
137   */
138	virtual void			StartProcessing();
139	virtual void			StopProcessing();
140	static status_t			_ProcessingThread(void *arg);
141	virtual void			ProcessingThread();
142
143  /* Set the state of the stream.  If newState is the same as the
144   * current state, this is a no-op.  Otherwise, this method will
145   * notify anyone listening on the notification port about the
146   * changed state and will send a StateChange buffer through the
147   * stream.
148   */
149	virtual void			SetState(stream_state newState);
150
151  /* Snooze until the desired time arrives.  Returns the
152   * current time upon returning.
153   */
154	bigtime_t				SnoozeUntil(bigtime_t sys_time);
155
156/* ================
157   Private data.
158   ================ */
159
160private:
161
162virtual	void		_ReservedBufferStreamManager1();
163virtual	void		_ReservedBufferStreamManager2();
164virtual	void		_ReservedBufferStreamManager3();
165
166  /****************************************************************
167   *
168   * Private fields.
169   *
170   */
171
172  BBufferStream	*fStream;			/* a BBufferStream object */
173  stream_state	fState;				/* running, stopping, etc. */
174  sem_id		fSem;
175
176  int32			fBufferCount;		/* desired # of buffers */
177  int32			fBufferSize;		/* desired size of each buffer */
178  bigtime_t		fBufferDelay;		/* minimum time between sends */
179  bigtime_t		fTimeout;			/* watchdog timer */
180
181  port_id		fNotifyPort;		/* when set, send change of state msgs */
182  thread_id		fProcessingThread;	/* thread to dispatch buffers */
183  subscriber_id	fManagerID;			/* StreamManager's subID in fStream */
184
185  BLocker		fLock;
186  char*			fName;
187  uint32		_reserved[4];
188};
189
190#endif			// #ifdef _BUFFER_STREAM_MANAGER_H
191