OpenWAM
TCGestorWAM.cpp
1 /*--------------------------------------------------------------------------------*\
2 |==========================|
3  |\\ /\ /\ // O pen | OpenWAM: The Open Source 1D Gas-Dynamic Code
4  | \\ | X | // W ave |
5  | \\ \/_\/ // A ction | CMT-Motores Termicos / Universidad Politecnica Valencia
6  | \\/ \// M odel |
7  ----------------------------------------------------------------------------------
8  | License
9  |
10  | This file is part of OpenWAM.
11  |
12  | OpenWAM is free software: you can redistribute it and/or modify
13  | it under the terms of the GNU General Public License as published by
14  | the Free Software Foundation, either version 3 of the License, or
15  | (at your option) any later version.
16  |
17  | OpenWAM is distributed in the hope that it will be useful,
18  | but WITHOUT ANY WARRANTY; without even the implied warranty of
19  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  | GNU General Public License for more details.
21  |
22  | You should have received a copy of the GNU General Public License
23  | along with OpenWAM. If not, see <http://www.gnu.org/licenses/>.
24  |
25  \*--------------------------------------------------------------------------------*/
26 
27 //---------------------------------------------------------------------------
28 #pragma hdrstop
29 
30 #include "TCGestorWAM.h"
31 
32 //---------------------------------------------------------------------------
33 
34 #pragma package(smart_init)
35 
36 //---------------------------------------------------------------------------
37 __fastcall TCGestorWAM::TCGestorWAM() {
38 }
39 
40 void TCGestorWAM::Init() {
41  BOOL fSuccess;
42  //std::string lpszPipeName;
43  string lpszPipeName;
44  LPTSTR lpszPipeNameResp;
45  DWORD dwMode;
46  stringstream process_id(stringstream::in | stringstream::out);
47 
48  process_id << (float) GetCurrentProcessId();
49 
50  //lpszPipeName = "\\\\.\\pipe\\pipe" + FloatToStr((float)GetCurrentProcessId());
51 
52  lpszPipeName = "\\\\.\\pipe\\pipe" + process_id.str();
53 
54  //lpszPipeName = TEXT("\\\\.\\pipe\\pipe");
55 
56  lpszPipeNameResp = TEXT("\\\\.\\pipe\\pipeResp");
57 
58  //Espera hasta que se puede conectar a la tuberia
59 
60  //Sleep(5000); //Espero 5 segundos
61 
62  long num_pasadas = 0;
63 
64  while(num_pasadas <= 500000) {
65  hPipe = CreateFile(lpszPipeName.c_str(), GENERIC_WRITE, 0, NULL,
66  OPEN_EXISTING, 0, NULL);
67 
68  if(hPipe != INVALID_HANDLE_VALUE)
69  break;
70 
71  num_pasadas++;
72  }
73 
74  //Si se ha llegado al final y no se ha conectado no
75  //hacer nada mas
76  if(num_pasadas <= 500000) {
77  if(GetLastError() != 0)
78  throw "Error al abrir tuberia";
79 
80  dwMode = PIPE_READMODE_MESSAGE;
81 
82  fSuccess = SetNamedPipeHandleState(hPipe, &dwMode, NULL, NULL);
83 
84  if(!fSuccess)
85  throw "No se puede cambiar de modo la tuberia";
86  } else {
87  //Coloco hPipe en NULL para no hacer nada al enviar datos
88  hPipe = NULL;
89  }
90 
91  /*while (1)
92  {
93  hPipeResp = CreateFile(lpszPipeNameResp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
94 
95  if (hPipeResp != INVALID_HANDLE_VALUE)
96  break;
97  }
98 
99  if (GetLastError()!=0) throw "Error al abrir tuberia respuesta";
100  */
101 }
102 
103 void TCGestorWAM::Enviar(char *msg) {
104  DWORD cbWritten;
105  BOOL fSuccess;
106  LPTSTR mensaje;
107 
108  if(hPipe != NULL) {
109  mensaje = TEXT(msg);
110 
111  fSuccess = WriteFile(hPipe, mensaje, (strlen(mensaje) + 1) * sizeof(TCHAR), &cbWritten, NULL);
112 
113  if(!fSuccess)
114  throw "No se puede escribir en tuberia";
115  }
116 
117 }
118 
119 void TCGestorWAM::Enviar(float valor) {
120  DWORD cbWritten;
121  BOOL fSuccess;
122 
123  if(hPipe != NULL) {
124  fSuccess = WriteFile(hPipe, &valor, sizeof(float), &cbWritten, NULL);
125 
126  if(!fSuccess)
127  throw "No se puede escribir en tuberia";
128  }
129 }
130 
131 void TCGestorWAM::EsperarRespuesta() {
132  DWORD cbRead;
133  BOOL fSuccess;
134  TCHAR chBuf[BUFSIZE];
135 
136  fSuccess = ReadFile(hPipeResp, chBuf, BUFSIZE * sizeof(TCHAR), &cbRead,
137  NULL);
138 }
139 
140 void TCGestorWAM::NuevoMensaje(char *msg) {
141  Enviar("NUEVOMENSAJE");
142 
143  Enviar(msg);
144 
145  //EsperarRespuesta();
146 }
147 
148 void TCGestorWAM::ProcesoTranscurrido(float valor) {
149  Enviar("PROCESOTRANSCURRIDO");
150 
151  Enviar(valor);
152 
153  //EsperarRespuesta();
154 }
155 
156 void TCGestorWAM::CabeceraResInstantActualizada() {
157  Enviar("CABECERARESINSTANTACTUALIZADA");
158  //EsperarRespuesta();
159 }
160 
161 void TCGestorWAM::CabeceraResMediosActualizada() {
162  Enviar("CABECERARESMEDIOSACTUALIZADA");
163  //EsperarRespuesta();
164 }
165 
166 void TCGestorWAM::FichResMediosActualizado() {
167  Enviar("FICHRESMEDIOSACTUALIZADO");
168  //EsperarRespuesta();
169 }
170 
171 void TCGestorWAM::FichResInstantActualizado() {
172  Enviar("FICHRESINSTANTACTUALIZADO");
173  //EsperarRespuesta();
174 }
175 
176 void TCGestorWAM::Terminar() {
177  Enviar("TERMINAR");
178 }
179