tService.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. /* ====================================================================
  2.  * The Vovida Software License, Version 1.0 
  3.  * 
  4.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  * 
  18.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  19.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  20.  *    not be used to endorse or promote products derived from this
  21.  *    software without prior written permission. For written
  22.  *    permission, please contact vocal@vovida.org.
  23.  *
  24.  * 4. Products derived from this software may not be called "VOCAL", nor
  25.  *    may "VOCAL" appear in their name, without prior written
  26.  *    permission of Vovida Networks, Inc.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  31.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  32.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  33.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40.  * DAMAGE.
  41.  * 
  42.  * ====================================================================
  43.  * 
  44.  * This software consists of voluntary contributions made by Vovida
  45.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  46.  * Inc.  For more information on Vovida Networks, Inc., please see
  47.  * <http://www.vovida.org/>.
  48.  *
  49.  */
  50. static const char* const tService_cxx_Version = 
  51. "$Id$";
  52. #include "Application.hxx"
  53. #include "Event.hxx"
  54. #include "ControlEvent.hxx"
  55. #include "PollFifo.hxx"
  56. #include "Thread.hxx"
  57. #include <iostream>
  58. using Vocal::Runnable;
  59. using Vocal::ReturnCode;
  60. using Vocal::SUCCESS;
  61. using Vocal::Threads::Thread;
  62. using Vocal::Services::ControlEvent;
  63. using Vocal::Services::Event;
  64. class Module : public Runnable
  65. {
  66.     public:
  67.      Module() {}
  68.      ~Module() {}
  69.      ReturnCode   run();
  70.      bool      processMessages(int numberFdsActive);
  71.      PollFifo< Sptr<Event> >     & getFifo()
  72. {
  73.     return ( fifo_ );
  74. }
  75.     private:
  76.     
  77.      PollFifo< Sptr<Event> >     fifo_;
  78. };
  79. class tService : public Application
  80. {
  81.     public:
  82.      tService() {}
  83. ~tService() {}
  84.      ReturnCode       run();
  85.      ReturnCode       init(int, char**, char **)
  86. {
  87.     Thread::init();
  88.     return ( SUCCESS );
  89. }
  90. void          uninit()
  91. {
  92.     Thread::uninit();
  93. }
  94. };
  95. ReturnCode  
  96. Module::run()
  97.     const string    fn("Module::run");
  98.     ReturnCode     rc = SUCCESS;
  99.     bool         done = false;
  100.     while ( !done )
  101.     {
  102.      int numberFdsActive = 0;
  103.      try
  104. {
  105.             numberFdsActive = fifo_.block();
  106.          cout << fn << ": after wait. number fds active = " 
  107.       << numberFdsActive << endl;
  108. }
  109. catch ( ... )
  110. {
  111.     done = true;
  112.     rc = -1;
  113.     continue;
  114. }
  115. done = processMessages(numberFdsActive);
  116.     }
  117.     return ( rc );
  118. }
  119. bool     
  120. Module::processMessages(int numberFdsActive)
  121. {
  122.     const string    fn("Module::processMessages");
  123.     VLog         log(fn);
  124.     bool done = false;
  125.     
  126.     while ( fifo_.messageAvailable() )
  127.     {
  128.      try
  129. {
  130.     Sptr<Event> event = fifo_.getNext();
  131.          Sptr<ControlEvent> ctrlEvent;
  132.          ctrlEvent.dynamicCast(event);
  133.          if ( ctrlEvent != 0 )
  134.          {
  135. cout << fn << ": msg = " << *ctrlEvent << endl;
  136.           switch ( ctrlEvent->getType() )
  137.           {
  138.     case ControlEvent::SHUTDOWN:
  139.     {
  140.      cout << fn << ": Shutdown" << endl;
  141.      done = true;
  142.      break;
  143.     }
  144.     case ControlEvent::START:
  145.     case ControlEvent::STOP:
  146.     {
  147.      ctrlEvent->setReturnCode(SUCCESS);
  148. FifoBase< Sptr<Event> > * fifo = ctrlEvent->getReplyFifo();
  149. if ( fifo )
  150. {
  151.     cout << fn << ": Returning message." << endl;
  152.     
  153.     // Send the message back.
  154.     //
  155.     fifo->add(Sptr<Event>(ctrlEvent));
  156. }
  157. else
  158. {
  159.     cout << fn << ": Huh? no return fifo." << endl;
  160.     done = true;
  161. }
  162. break;
  163.     }
  164.     default:
  165.     {
  166.      cout << fn << ": Huh? bad type" << endl;
  167. done = true;
  168.     }
  169. }
  170.          }
  171. }
  172. catch ( ... )
  173. {
  174.     done = true;
  175. }
  176.     }
  177.     if ( numberFdsActive )
  178.     {
  179.         fifo_.processProtocols(numberFdsActive);
  180.     }
  181.     return ( done );
  182. }
  183. Application * Application::create()
  184. {
  185.     return ( new tService );
  186. }
  187. ReturnCode
  188. tService::run()
  189. {
  190.     Module             module;
  191.     Thread             moduleThread(module, "module");
  192.     PollFifo< Sptr<Event> >  poll;
  193.     
  194.     Sptr<ControlEvent>  event = new ControlEvent(ControlEvent::START, poll);
  195.     int id = event->getControlEventId();
  196.     
  197.     cout << "Sending start: " << *event << endl;
  198.     
  199.     module.getFifo().add(Sptr<Event>(event));
  200.     
  201.     bool    done = false;
  202.     int     numberFdsActive;
  203.     
  204.     while ( !done )
  205.     {
  206.         numberFdsActive = poll.block();
  207.     
  208.         if ( poll.messageAvailable() )
  209. {
  210.     Sptr<Event> newEvent = poll.getNext();
  211.          Sptr<ControlEvent> ctrlEvent;
  212.          ctrlEvent.dynamicCast(event);
  213.          if  (   ctrlEvent != 0 
  214.      &&  ctrlEvent->getControlEventId() == id
  215.      )
  216.          {
  217.      cout << "Received start: " << *ctrlEvent << endl;
  218.      done = true;
  219.     }
  220.     else
  221.     {
  222.      cout << "Received unknown event." << endl;
  223.     }
  224. }
  225. if ( numberFdsActive )
  226. {
  227.             poll.processProtocols(numberFdsActive);
  228. }
  229.     }
  230.     event = new ControlEvent(ControlEvent::STOP, poll);
  231.     
  232.     id = event->getControlEventId();
  233.     module.getFifo().add(Sptr<Event>(event));
  234.     done = false;
  235.         
  236.     while ( !done )
  237.     {
  238.         numberFdsActive = poll.block();
  239.     
  240.         if ( poll.messageAvailable() )
  241. {
  242.     Sptr<Event> newEvent = poll.getNext();
  243.          Sptr<ControlEvent> ctrlEvent;
  244.          ctrlEvent.dynamicCast(event);
  245.          if  (   ctrlEvent != 0 
  246.      &&  ctrlEvent->getControlEventId() == id
  247.      )
  248.          {
  249.      cout << "Received stop: " << *ctrlEvent << endl;
  250.      done = true;
  251. continue;
  252.     }
  253.          else
  254.     {     
  255.      cout << "Received unknown event." << endl;
  256.     }
  257. }
  258. if ( numberFdsActive )
  259. {
  260.             poll.processProtocols(numberFdsActive);
  261. }
  262.     }
  263.          
  264.     event = new ControlEvent(
  265.     ControlEvent::SHUTDOWN,
  266.     poll);
  267.     
  268.     module.getFifo().add(Sptr<Event>(event));
  269.     return ( moduleThread.join() );
  270. }
  271. int main(int argc, char ** argv, char ** arge)
  272. {
  273.     return ( Application::main(argc, argv, arge) );
  274. }