reSIProcate/stack  9694
testAppTimer.cxx
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "resip/stack/TransactionMessage.hxx"
00003 #include "resip/stack/TimerQueue.hxx"
00004 #include "resip/stack/TuSelector.hxx"
00005 #include "rutil/Fifo.hxx"
00006 #include "rutil/Logger.hxx"
00007 #ifdef WIN32
00008 #include <io.h>
00009 #else
00010 #include <unistd.h>
00011 #endif
00012 
00013 #ifdef WIN32
00014 #define usleep(x) Sleep(x/1000)
00015 #define sleep(x) Sleep(x*1000)
00016 #endif
00017 
00018 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
00019 
00020 using namespace resip;
00021 using namespace std;
00022 
00023 bool
00024 isNear(int value, int reference, int epsilon=250)
00025 {
00026    int diff = ::abs(value-reference);
00027    return (diff < epsilon);
00028 }
00029 
00030 class AppMessage : public Message
00031 {
00032    public:
00033       AppMessage(const Data& text)
00034          : mText(text)
00035       {}
00036 
00037       virtual EncodeStream& encode(EncodeStream& s) const {s << mText; return s;}
00038       virtual EncodeStream& encodeBrief(EncodeStream& s) const {return s << mText;}
00039 
00040       const Data& getText() const
00041       {
00042          return mText;
00043       }
00044       
00045       virtual Message* clone() const
00046       {
00047          return new AppMessage(mText);
00048       }
00049 
00050    private:
00051       const Data mText;
00052 };
00053 
00054 int
00055 main(int argc, char** argv)
00056 {
00057    Log::initialize(Log::Cout, Log::Debug, argv[0]);
00058 
00059    TimeLimitFifo<Message> f(0, 0);
00060    TimeLimitTimerQueue timer(f);
00061 
00062    cerr << "Before Fifo size: " << f.size() << endl;
00063    assert(f.size() == 0);
00064    cerr << "next timer = " << timer.msTillNextTimer() << endl;
00065    assert(timer.msTillNextTimer() == INT_MAX);
00066 
00067    timer.add(1000, new AppMessage("first"));
00068    cerr << timer;
00069    assert(f.size() == 0);
00070    timer.process();
00071    cerr << "Immediately after Fifo size: " << f.size() << endl;
00072    assert(f.size() == 0);
00073 
00074    sleep(1);
00075    timer.process();
00076    assert(f.size() == 1);
00077 
00078    Message* msg = f.getNext();
00079 
00080    cerr << *msg << endl;
00081 
00082    delete msg;
00083    return 0;
00084 }
00085 
00086 /* ====================================================================
00087  * The Vovida Software License, Version 1.0 
00088  * 
00089  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00090  * 
00091  * Redistribution and use in source and binary forms, with or without
00092  * modification, are permitted provided that the following conditions
00093  * are met:
00094  * 
00095  * 1. Redistributions of source code must retain the above copyright
00096  *    notice, this list of conditions and the following disclaimer.
00097  * 
00098  * 2. Redistributions in binary form must reproduce the above copyright
00099  *    notice, this list of conditions and the following disclaimer in
00100  *    the documentation and/or other materials provided with the
00101  *    distribution.
00102  * 
00103  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00104  *    and "Vovida Open Communication Application Library (VOCAL)" must
00105  *    not be used to endorse or promote products derived from this
00106  *    software without prior written permission. For written
00107  *    permission, please contact vocal@vovida.org.
00108  *
00109  * 4. Products derived from this software may not be called "VOCAL", nor
00110  *    may "VOCAL" appear in their name, without prior written
00111  *    permission of Vovida Networks, Inc.
00112  * 
00113  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00114  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00115  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00116  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00117  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00118  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00119  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00120  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00121  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00122  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00123  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00124  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00125  * DAMAGE.
00126  * 
00127  * ====================================================================
00128  * 
00129  * This software consists of voluntary contributions made by Vovida
00130  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00131  * Inc.  For more information on Vovida Networks, Inc., please see
00132  * <http://www.vovida.org/>.
00133  *
00134  */