1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved.
17// Basic Usage Environment: for a simple, non-scripted, console application
18// C++ header
19
20#ifndef _BASIC_USAGE_ENVIRONMENT0_HH
21#define _BASIC_USAGE_ENVIRONMENT0_HH
22
23#ifndef _BASICUSAGEENVIRONMENT_VERSION_HH
24#include "BasicUsageEnvironment_version.hh"
25#endif
26
27#ifndef _USAGE_ENVIRONMENT_HH
28#include "UsageEnvironment.hh"
29#endif
30
31#ifndef _DELAY_QUEUE_HH
32#include "DelayQueue.hh"
33#endif
34
35#define RESULT_MSG_BUFFER_MAX 1000
36
37// An abstract base class, useful for subclassing
38// (e.g., to redefine the implementation of "operator<<")
39class BasicUsageEnvironment0: public UsageEnvironment {
40public:
41 // redefined virtual functions:
42 virtual MsgString getResultMsg() const;
43
44 virtual void setResultMsg(MsgString msg);
45 virtual void setResultMsg(MsgString msg1,
46 MsgString msg2);
47 virtual void setResultMsg(MsgString msg1,
48 MsgString msg2,
49 MsgString msg3);
50 virtual void setResultErrMsg(MsgString msg, int err = 0);
51
52 virtual void appendToResultMsg(MsgString msg);
53
54 virtual void reportBackgroundError();
55
56protected:
57 BasicUsageEnvironment0(TaskScheduler& taskScheduler);
58 virtual ~BasicUsageEnvironment0();
59
60private:
61 void reset();
62
63 char fResultMsgBuffer[RESULT_MSG_BUFFER_MAX];
64 unsigned fCurBufferSize;
65 unsigned fBufferMaxSize;
66};
67
68class HandlerSet; // forward
69
70#define MAX_NUM_EVENT_TRIGGERS 32
71
72// An abstract base class, useful for subclassing
73// (e.g., to redefine the implementation of socket event handling)
74class BasicTaskScheduler0: public TaskScheduler {
75public:
76 virtual ~BasicTaskScheduler0();
77
78 virtual void SingleStep(unsigned maxDelayTime = 0) = 0;
79 // "maxDelayTime" is in microseconds. It allows a subclass to impose a limit
80 // on how long "select()" can delay, in case it wants to also do polling.
81 // 0 (the default value) means: There's no maximum; just look at the delay queue
82
83public:
84 // Redefined virtual functions:
85 virtual TaskToken scheduleDelayedTask(int64_t microseconds, TaskFunc* proc,
86 void* clientData);
87 virtual void unscheduleDelayedTask(TaskToken& prevTask);
88
89 virtual void doEventLoop(char volatile* watchVariable);
90
91 virtual EventTriggerId createEventTrigger(TaskFunc* eventHandlerProc);
92 virtual void deleteEventTrigger(EventTriggerId eventTriggerId);
93 virtual void triggerEvent(EventTriggerId eventTriggerId, void* clientData = NULL);
94
95protected:
96 BasicTaskScheduler0();
97
98protected:
99 // To implement delayed operations:
100 DelayQueue fDelayQueue;
101
102 // To implement background reads:
103 HandlerSet* fHandlers;
104 int fLastHandledSocketNum;
105
106 // To implement event triggers:
107 EventTriggerId volatile fTriggersAwaitingHandling; // implemented as a 32-bit bitmap
108 EventTriggerId fLastUsedTriggerMask; // implemented as a 32-bit bitmap
109 TaskFunc* fTriggeredEventHandlers[MAX_NUM_EVENT_TRIGGERS];
110 void* fTriggeredEventClientDatas[MAX_NUM_EVENT_TRIGGERS];
111 unsigned fLastUsedTriggerNum; // in the range [0,MAX_NUM_EVENT_TRIGGERS)
112};
113
114#endif
115