00001 /** 00002 Licensed to the Apache Software Foundation (ASF) under one 00003 or more contributor license agreements. See the NOTICE file 00004 distributed with this work for additional information 00005 regarding copyright ownership. The ASF licenses this file 00006 to you under the Apache License, Version 2.0 (the 00007 "License"); you may not use this file except in compliance 00008 with the License. You may obtain a copy of the License at 00009 00010 http://www.apache.org/licenses/LICENSE-2.0 00011 00012 Unless required by applicable law or agreed to in writing, software 00013 distributed under the License is distributed on an "AS IS" BASIS, 00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 See the License for the specific language governing permissions and 00016 limitations under the License. 00017 */ 00018 00019 /** 00020 * @file AsyncTimer.h 00021 */ 00022 00023 #pragma once 00024 #ifndef ATSCPPAPI_ASYNCTIMER_H_ 00025 #define ATSCPPAPI_ASYNCTIMER_H_ 00026 00027 #include <string> 00028 #include <atscppapi/shared_ptr.h> 00029 #include <atscppapi/Async.h> 00030 #include <atscppapi/Request.h> 00031 #include <atscppapi/Response.h> 00032 00033 namespace atscppapi { 00034 00035 // forward declarations 00036 struct AsyncTimerState; 00037 00038 /** 00039 * @brief This class provides an implementation of AsyncProvider that 00040 * acts as a timer. It sends events at the set frequency. Calling the 00041 * destructor will stop the events. A one-off timer just sends one 00042 * event. Calling the destructor before this event will cancel the timer. 00043 * 00044 * For either type, user must delete the timer. 00045 * 00046 * See example async_timer for sample usage. 00047 */ 00048 class AsyncTimer : public AsyncProvider { 00049 public: 00050 00051 enum Type { TYPE_ONE_OFF = 0, TYPE_PERIODIC }; 00052 00053 /** 00054 * Constructor. 00055 * 00056 * @param type A one-off timer fires only once and a periodic timer fires periodically. 00057 * @param period_in_ms The receiver will receive an event every this many milliseconds. 00058 * @param initial_period_in_ms The first event will arrive after this many milliseconds. Subsequent 00059 * events will have "regular" cadence. This is useful if the timer is 00060 * set for a long period of time (1hr etc.), but an initial event is 00061 * required. Value of 0 (default) indicates no initial event is desired. 00062 */ 00063 AsyncTimer(Type type, int period_in_ms, int initial_period_in_ms = 0); 00064 00065 virtual ~AsyncTimer(); 00066 00067 /** 00068 * Starts the timer. 00069 */ 00070 void run(); 00071 00072 void cancel(); 00073 00074 private: 00075 AsyncTimerState *state_; 00076 }; 00077 00078 } /* atscppapi */ 00079 00080 #endif /* ATSCPPAPI_ASYNCTIMER_H_ */