Ik ben een applicatie schrijven die aan de hand van XML communicatie verzorgt met bepaalde apparaten, dat werkt allemaal geweldig. Ook is er een mogelijkheid om een bestand als een apparaat te zien, dus de inhoud van die file is een output van een apparaat. Dit was de korte intro.
Ik heb een thread met de volgende sequence:
- laad html-template uit een bestand voor rapportage
- ga voor elk apparaat in de lijst de xml parsen.
- sla het bewerkte html-template (rapport dus) op.
Dit gaat perfect als de apparaten alleen maar echte apparaten zijn en dus geen file I/O hoeven te doen. Zodra ik dus een stuk XML heb dat bepaald dat ik uit een bestand data moet lezen kan ik opeens stap 3 niet meer doen, haal ik stap 3 naar voren, dus voor stap 2, dan werkt het opslaan opeens wel weer. Er zit dus duidelijk iets fout in stap 2. ik heb gechecked op handles die gesloten worden, maar ze worden allemaal netjes afgesloten.
Nu even de I/O-flow van mijn applicatie:
stap 1 (std::ios_base::in):
- std::fstream > open("html/report.temlate");
- std::fstream > read();
- std::fstream > close();
stap 2 (std::ios_base::in):
Voor elke apparaat dat de data in een bestand uitleest:
- std::fstream > open("volledig pad naar bestand");
- std::fstream > read();
- std::fstream > close();
stap 3 (std::ios_base::out | std::ios_base::trunc)
- std::fstream > open("reports/temp.html");
en daar gaat het dan fout er wordt geen bestand aangemaakt. Nu komt het vreemde, als ik dus geen bestanden inlees in stap 2 dus alleen directe I/O op de andere apparaten werkt stap 3 wel opeens. Alle handles worden echt gesloten. Om een en ander uit te sluiten zal ik hier de code neergooien van de klasse die ik gebruik om met de files te interfacen:
Ik word er een beetje gek van
edit: Ohja, als ik de C-library gebruik (fopen() etc) bij stap 3 dan krijg ik hetzelfde bij std::fstream
Ik heb een thread met de volgende sequence:
- laad html-template uit een bestand voor rapportage
- ga voor elk apparaat in de lijst de xml parsen.
- sla het bewerkte html-template (rapport dus) op.
Dit gaat perfect als de apparaten alleen maar echte apparaten zijn en dus geen file I/O hoeven te doen. Zodra ik dus een stuk XML heb dat bepaald dat ik uit een bestand data moet lezen kan ik opeens stap 3 niet meer doen, haal ik stap 3 naar voren, dus voor stap 2, dan werkt het opslaan opeens wel weer. Er zit dus duidelijk iets fout in stap 2. ik heb gechecked op handles die gesloten worden, maar ze worden allemaal netjes afgesloten.
Nu even de I/O-flow van mijn applicatie:
stap 1 (std::ios_base::in):
- std::fstream > open("html/report.temlate");
- std::fstream > read();
- std::fstream > close();
stap 2 (std::ios_base::in):
Voor elke apparaat dat de data in een bestand uitleest:
- std::fstream > open("volledig pad naar bestand");
- std::fstream > read();
- std::fstream > close();
stap 3 (std::ios_base::out | std::ios_base::trunc)
- std::fstream > open("reports/temp.html");
en daar gaat het dan fout er wordt geen bestand aangemaakt. Nu komt het vreemde, als ik dus geen bestanden inlees in stap 2 dus alleen directe I/O op de andere apparaten werkt stap 3 wel opeens. Alle handles worden echt gesloten. Om een en ander uit te sluiten zal ik hier de code neergooien van de klasse die ik gebruik om met de files te interfacen:
C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| typedef struct __FileConfig { char* fileName; bool isWriteable; bool isReadable; } FileConfig; class Connector { // public access data public: // Constructors/Deconstructors: Connector(); virtual ~Connector(); //Member functions: // Init & Termination virtual bool connect(void* config) = 0; virtual bool disconnect() = 0; // RW-OPS virtual bool read(char* buffer, size_t length) = 0; virtual bool write(char* buffer, size_t length) = 0; bool rxFlag; bool rxEndFlag; virtual void clearBuffer() = 0; virtual void waitForCompletion() = 0; // private data private: // protected data protected: bool connected; }; class FileConnector : public Connector { // public access data public: // Constructors/Deconstructors: FileConnector(); ~FileConnector(); //Member functions: // Init & Termination bool connect(void* config); bool disconnect(); void clearBuffer(); void waitForCompletion(); // RW-OPS bool read(char* buffer, size_t length); bool write(char* buffer, size_t length); // private data private: std::fstream* fileHandle; FileConfig* configuration; // protected data protected: }; bool FileConnector::connect(void* config) { DEBUG("FileConnector::connect()"); // check if we are already connected if(!this->connected) { // set config pointer this->configuration = (FileConfig*)config; /* debug */ DEBUG("BEGIN DEVICE CONFIG"); DEBUG("fileName: " << this->configuration->fileName); DEBUG("isWriteable: " << this->configuration->isWriteable); DEBUG("isReadable: " << this->configuration->isReadable); DEBUG("END DEVICE CONFIG"); /* create handle with right read/write flags */ if(this->configuration->isWriteable && !this->configuration->isReadable) this->fileHandle = new std::fstream(this->configuration->fileName, std::ios_base::out | std::ios_base::trunc); else if (this->configuration->isReadable && !this->configuration->isWriteable) this->fileHandle = new std::fstream(this->configuration->fileName, std::ios_base::in); else this->fileHandle = new std::fstream(this->configuration->fileName, std::ios_base::out | std::ios_base::in | std::ios_base::trunc); /* check if file was opened correctly */ if(!this->fileHandle->is_open()) { return false; } /* we are connected */ this->connected = true; } else return false; return true; } /* Function: disconnect * Description: Disconnects the device * * Param(s): none * * Returns: state of function call */ bool FileConnector::disconnect() { DEBUG("FileConnector::disconnect()"); /* check */ if(this->connected) { /* close stream */ this->fileHandle->close(); /* free the pointer */ delete this->fileHandle; this->connected = false; } else return false; return true; } /* Function: read * Description: read a number of bytes from a device * * Param(s): * * - buffer: a pointer to a buffer to write the data to * - length: the number of bytes to read * * Returns: state of function call */ bool FileConnector::read(char* buffer, size_t length) { DEBUG("FileConnector::read(null, " << length << ")"); if(this->connected && this->configuration->isReadable) { this->fileHandle->read(buffer, static_cast<std::streamsize>(length)); this->rxFlag = false; this->rxEndFlag = false; } else return false; return true; } /* Function: write * Description: write a number of bytes to a device * * Param(s): * * - buffer: a pointer to a buffer to write the data to * - length: the number of bytes to read * * Returns: state of function call */ bool FileConnector::write(char* buffer, size_t length) { DEBUG("FileConnector::write(" << buffer << ", " << length << ")"); if(this->connected && this->configuration->isWriteable) { this->fileHandle->write(buffer, static_cast<std::streamsize>(length)); } else return false; return true; } |
Ik word er een beetje gek van
edit: Ohja, als ik de C-library gebruik (fopen() etc) bij stap 3 dan krijg ik hetzelfde bij std::fstream