Ginga  0.13.6
 All Classes Namespaces Functions Variables
Process.h
1 /******************************************************************************
2 Este arquivo eh parte da implementacao do ambiente declarativo do middleware
3 Ginga (Ginga-NCL).
4 
5 Direitos Autorais Reservados (c) 1989-2007 PUC-Rio/Laboratorio TeleMidia
6 
7 Este programa eh software livre; voce pode redistribui-lo e/ou modificah-lo sob
8 os termos da Licenca Publica Geral GNU versao 2 conforme publicada pela Free
9 Software Foundation.
10 
11 Este programa eh distribuido na expectativa de que seja util, porem, SEM
12 NENHUMA GARANTIA; nem mesmo a garantia implicita de COMERCIABILIDADE OU
13 ADEQUACAO A UMA FINALIDADE ESPECIFICA. Consulte a Licenca Publica Geral do
14 GNU versao 2 para mais detalhes.
15 
16 Voce deve ter recebido uma copia da Licenca Publica Geral do GNU versao 2 junto
17 com este programa; se nao, escreva para a Free Software Foundation, Inc., no
18 endereco 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
19 
20 Para maiores informacoes:
21 ncl @ telemidia.puc-rio.br
22 http://www.ncl.org.br
23 http://www.ginga.org.br
24 http://www.telemidia.puc-rio.br
25 ******************************************************************************
26 This file is part of the declarative environment of middleware Ginga (Ginga-NCL)
27 
28 Copyright: 1989-2007 PUC-RIO/LABORATORIO TELEMIDIA, All Rights Reserved.
29 
30 This program is free software; you can redistribute it and/or modify it under
31 the terms of the GNU General Public License version 2 as published by
32 the Free Software Foundation.
33 
34 This program is distributed in the hope that it will be useful, but WITHOUT ANY
35 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
36 PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
37 details.
38 
39 You should have received a copy of the GNU General Public License version 2
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 
43 For further information contact:
44 ncl @ telemidia.puc-rio.br
45 http://www.ncl.org.br
46 http://www.ginga.org.br
47 http://www.telemidia.puc-rio.br
48 *******************************************************************************/
49 
50 #ifndef __Process_h__
51 #define __Process_h__
52 
53 #include "IProcessListener.h"
54 
55 #include "system/compat/SystemCompat.h"
56 using namespace ::br::pucrio::telemidia::ginga::core::system::compat;
57 
58 #include "system/thread/Thread.h"
59 using namespace ::br::pucrio::telemidia::ginga::core::system::thread;
60 
61 #include <sys/mman.h>
62 #include <sys/stat.h>
63 #include <stdio.h>
64 #include <spawn.h>
65 #include <signal.h>
66 #include <sys/wait.h>
67 #include <sys/types.h>
68 
69 #include <errno.h>
70 #include <string.h>
71 
72 #include <iostream>
73 #include <string>
74 using namespace std;
75 
76 namespace br {
77 namespace pucrio {
78 namespace telemidia {
79 namespace ginga {
80 namespace core {
81 namespace system {
82 namespace process {
83  class Process {
84  private:
85  static const short PST_NULL = 0;
86  static const short PST_RUNNING = 1;
87  static const short PST_SDONE = 2;
88  static const short PST_UDONE = 3;
89 
90  static const int SHM_SIZE = 65536;
91 
92  pid_t pid;
93  int comDesc;
94  short processStatus;
95  char** argv;
96  char** envp;
97  string processUri;
98  posix_spawnattr_t spawnAttr;
99  posix_spawn_file_actions_t fileActions;
100 
101  int shmDesc;
102 
103  string rCom;
104  string wCom;
105  FILE* wFd;
106  FILE* rFd;
107 
108  protected:
109  string objName;
110  bool reader;
111 
112  private:
113  bool isSpawnedReady;
114  bool hasCom;
115 
116  bool isCheckingCom;
117  pthread_mutex_t comMutex;
118  pthread_cond_t comCond;
119 
120  protected:
121  IProcessListener* sigListener;
122 
123  public:
124  Process(char** argv);
125  virtual ~Process();
126 
127  protected:
128  void release();
129 
130  public:
131  void setProcessInfo(string processUri, string objName);
132  void checkCom();
133 
134  private:
135  void tryCom();
136 
137  public:
138  bool sendMsg(string msg);
139  static bool sendMsg(FILE* fd, string msg);
140  virtual void messageReceived(string msg);
141  static string receiveMsg(FILE* fd);
142  static FILE* openW(string wName);
143  static FILE* openR(string rName);
144 
145  void setProcessListener(IProcessListener* listener);
146  void run();
147  void forceKill();
148 
149  private:
150  void spawnedReady(bool ready);
151  static void* createFiles(void* ptr);
152  static void* detachWait(void* ptr);
153  static void* detachReceive(void* ptr);
154  };
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162 
163 #endif //__Process_h__