1 /*****************************************************************************/
2 /* Software Testing Automation Framework (STAF)                              */
3 /* (C) Copyright IBM Corp. 2001, 2004                                        */
4 /*                                                                           */
5 /* This software is licensed under the Eclipse Public License (EPL) V1.0.    */
6 /*****************************************************************************/
7 
8 #ifndef STAF_ConnectionProviderInlImpl
9 #define STAF_ConnectionProviderInlImpl
10 
11 #include "STAF.h"
12 #include "STAFConnectionProvider.h"
13 
14 #define CHECK_FOR_CONN_IO_EXCEPTIONS(where) \
15 if (rc != kSTAFOk)\
16 {\
17     STAFString errorString = STAFString(where) + ": " +\
18                              STAFString(errorBuffer, STAFString::kShallow);\
19     STAFConnectionIOException ioError(errorString.toCurrentCodePage()->buffer(),\
20                                       rc);\
21     THROW_STAF_EXCEPTION(ioError);\
22 }
23 
24 
25 #define CHECK_FOR_CONN_EXCEPTIONS(where) \
26 if (rc != kSTAFOk)\
27 {\
28     STAFString errorString = STAFString(where) + ": " +\
29                              STAFString(errorBuffer, STAFString::kShallow);\
30     STAFConnectionException connError(errorString.toCurrentCodePage()->buffer(),\
31                                       rc);\
32     THROW_STAF_EXCEPTION(connError);\
33 }
34 
35 
36 #define CHECK_FOR_PROV_EXCEPTIONS(where) \
37 if (rc != kSTAFOk)\
38 {\
39     STAFString errorString = STAFString(where) + ": " +\
40                              STAFString(errorBuffer, STAFString::kShallow);\
41     STAFConnectionProviderException provError(\
42         errorString.toCurrentCodePage()->buffer(), rc);\
43     THROW_STAF_EXCEPTION(provError);\
44 }
45 
46 
47 #define CHECK_FOR_EXCEPTIONS(where) \
48 if (rc != kSTAFOk)\
49 {\
50     STAFString errorString = STAFString(where) + ": " +\
51                              STAFString(errorBuffer, STAFString::kShallow);\
52     STAFException error(errorString.toCurrentCodePage()->buffer(), rc);\
53     THROW_STAF_EXCEPTION(error);\
54 }
55 
56 
handleNewConnection(STAFConnectionProvider_t provider,STAFConnection_t conn,const STAFConnectionProviderFunctionTable * funcTable,void * data)57 STAF_INLINE STAFRC_t STAFConnectionProvider::handleNewConnection(
58     STAFConnectionProvider_t provider,
59     STAFConnection_t conn,
60     const STAFConnectionProviderFunctionTable *funcTable,
61     void *data)
62 {
63     STAFConnectionProvider *theProvider =
64         reinterpret_cast<STAFConnectionProvider *>(data);
65 
66     STAFConnectionPtr theConn =
67         STAFConnectionPtr(new STAFConnection(conn, &theProvider->fFuncTable),
68                           STAFConnectionPtr::INIT);
69 
70     return theProvider->fNewConnFunc(theProvider, theConn);
71 }
72 
73 
createRefPtr(const STAFString & name,const STAFString & connLib,void * constructInfo,unsigned int constructInfoLevel)74 STAF_INLINE STAFConnectionProviderPtr STAFConnectionProvider::createRefPtr(
75     const STAFString &name,
76     const STAFString &connLib,
77     void *constructInfo,
78     unsigned int constructInfoLevel)
79 {
80     STAFConnectionProvider *theConnProv = create(name, connLib, constructInfo,
81                                                  constructInfoLevel);
82 
83     return STAFConnectionProviderPtr(theConnProv,
84                                      STAFConnectionProviderPtr::INIT);
85 }
86 
87 
create(const STAFString & name,const STAFString & connLib,void * constructInfo,unsigned int constructInfoLevel)88 STAF_INLINE STAFConnectionProvider *STAFConnectionProvider::create(
89     const STAFString &name,
90     const STAFString &connLib,
91     void *constructInfo,
92     unsigned int constructInfoLevel)
93 {
94     STAFString_t errorBuffer = 0;
95     STAFDynamicLibrary_t library = 0;
96     STAFRC_t rc = STAFDynamicLibraryOpen(&library,
97                                          connLib.toCurrentCodePage()->buffer(),
98                                          &errorBuffer);
99 
100     CHECK_FOR_EXCEPTIONS("STAFDynamicLibrary");
101 
102     STAFConnectionProviderFunctionTable funcTable = { 0 };
103 
104     rc = STAFConnectionProviderLoad(library, &funcTable, &errorBuffer);
105 
106     if (rc != kSTAFOk) STAFDynamicLibraryClose(&library, 0);
107 
108     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderLoad");
109 
110     STAFConnectionProvider_t provider = 0;
111 
112     rc = funcTable.provConstruct(&provider, constructInfo, constructInfoLevel,
113                                  &errorBuffer);
114 
115     if (rc != kSTAFOk) STAFDynamicLibraryClose(&library, 0);
116 
117     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderConstruct");
118 
119     return new STAFConnectionProvider(name, connLib, library,
120                                       provider, funcTable);
121 }
122 
123 
start(NewConnectionFunc newConnFunc)124 STAF_INLINE void STAFConnectionProvider::start(
125     NewConnectionFunc newConnFunc)
126 {
127     STAFConnectionProviderStartInfoLevel1 startInfo = { 0 };
128 
129     startInfo.newConnectionFunc = handleNewConnection;
130     startInfo.data = this;
131     fNewConnFunc = newConnFunc;
132 
133     STAFString_t errorBuffer = 0;
134     STAFRC_t rc = fFuncTable.provStart(fProvider, &startInfo, 1, &errorBuffer);
135 
136     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderStart");
137 }
138 
139 
stop()140 STAF_INLINE void STAFConnectionProvider::stop()
141 {
142     STAFString_t errorBuffer = 0;
143     STAFRC_t rc = fFuncTable.provStop(fProvider, 0, 0, &errorBuffer);
144 
145     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderStop");
146 }
147 
148 
getMyNetworkIDs(STAFString & logicalIdentifier,STAFString & physicalIdentifier) const149 STAF_INLINE void STAFConnectionProvider::getMyNetworkIDs(
150     STAFString &logicalIdentifier, STAFString &physicalIdentifier) const
151 {
152     STAFStringConst_t logicalImpl = 0;
153     STAFStringConst_t physicalImpl = 0;
154     STAFString_t errorBuffer = 0;
155     STAFRC_t rc = fFuncTable.provGetMyNetworkIDs(fProvider, &logicalImpl,
156                                                  &physicalImpl, &errorBuffer);
157 
158     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderGetMyNetworkIDs");
159 
160     logicalIdentifier = logicalImpl;
161     physicalIdentifier = physicalImpl;
162 }
163 
164 
getOptions(STAFObjectPtr & options) const165 STAF_INLINE void STAFConnectionProvider::getOptions(
166     STAFObjectPtr &options) const
167 {
168     STAFObject_t optionsImpl = 0;
169     STAFString_t errorBuffer = 0;
170 
171     STAFRC_t rc = fFuncTable.provGetOptions(
172         fProvider, &optionsImpl, &errorBuffer);
173 
174     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderGetOptions");
175 
176     options = STAFObject::create(optionsImpl);
177 }
178 
179 
getProperty(STAFConnectionProviderProperty_t property) const180 STAF_INLINE STAFString STAFConnectionProvider::getProperty(
181     STAFConnectionProviderProperty_t property) const
182 {
183     STAFStringConst_t valueImpl = 0;
184     STAFString_t errorBuffer = 0;
185 
186     STAFRC_t rc = fFuncTable.provGetProperty(fProvider, property,
187                                              &valueImpl, &errorBuffer);
188 
189     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderGetProperty");
190 
191     return valueImpl;
192 }
193 
194 
getName() const195 STAF_INLINE const STAFString &STAFConnectionProvider::getName() const
196 {
197     return fName;
198 }
199 
200 
getLibrary() const201 STAF_INLINE const STAFString &STAFConnectionProvider::getLibrary() const
202 {
203     return fLibrary;
204 }
205 
206 
connect(const STAFString & endpoint) const207 STAF_INLINE STAFConnectionPtr STAFConnectionProvider::connect(
208     const STAFString &endpoint) const
209 {
210     STAFConnection_t conn = 0;
211     STAFConnectionProviderConnectInfoLevel1 connectInfo = { 0 };
212 
213     connectInfo.endpoint = endpoint.getImpl();
214 
215     STAFString_t errorBuffer = 0;
216     STAFRC_t rc = fFuncTable.provConnect(fProvider, &conn, &connectInfo,
217                                          1, &errorBuffer);
218 
219     CHECK_FOR_PROV_EXCEPTIONS("STAFConnectionProviderConnect");
220 
221     return STAFConnectionPtr(new STAFConnection(conn, &fFuncTable),
222                              STAFConnectionPtr::INIT);
223 }
224 
225 
~STAFConnectionProvider()226 STAF_INLINE STAFConnectionProvider::~STAFConnectionProvider()
227 {
228     // The try/catch is here because when running STAF on OpenSolaris x86 when
229     // built using Sun Studio CC, an exception is thrown while the stack is
230     // unwinding which forces STAFProc to call abort() and core dump
231 
232     try
233     {
234         fFuncTable.provDestruct(&fProvider, 0, 0, 0);
235         STAFDynamicLibraryClose(&fConnLib, 0);
236     }
237     catch (STAFException &se)
238     {
239         se.write("STAFConnectionProvider::~STAFConnectionProvider()", cerr);
240     }
241     catch (...)
242     {
243         cerr << "Unknown exception at STAFConnectionProvider::"
244              << "~STAFConnectionProvider()" << endl;
245     }
246 }
247 
248 
STAFConnectionProvider(const STAFString & name,const STAFString & library,STAFDynamicLibrary_t connLib,STAFConnectionProvider_t provider,const STAFConnectionProviderFunctionTable funcTable)249 STAF_INLINE STAFConnectionProvider::STAFConnectionProvider(
250     const STAFString &name,
251     const STAFString &library,
252     STAFDynamicLibrary_t connLib,
253     STAFConnectionProvider_t provider,
254     const STAFConnectionProviderFunctionTable funcTable)
255     : fName(name), fLibrary(library), fConnLib(connLib),
256       fProvider(provider), fFuncTable(funcTable)
257 {
258     // Do nothing
259 }
260 
261 
read(void * buffer,unsigned int size,bool timeout)262 STAF_INLINE void STAFConnection::read(void *buffer, unsigned int size,
263                                       bool timeout)
264 {
265     STAFString_t errorBuffer = 0;
266     STAFRC_t rc =
267         fFuncTable->connRead(fConn, buffer, size, &errorBuffer, timeout);
268 
269     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionRead");
270 }
271 
272 
readUInt(bool timeout)273 STAF_INLINE unsigned int STAFConnection::readUInt(bool timeout)
274 {
275     unsigned int theUInt = 0;
276     STAFString_t errorBuffer = 0;
277     STAFRC_t rc =
278         fFuncTable->connReadUInt(fConn, &theUInt, &errorBuffer, timeout);
279 
280     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionReadUInt");
281 
282     return theUInt;
283 }
284 
285 
readUInt(unsigned int & uint,bool timeout)286 STAF_INLINE void STAFConnection::readUInt(unsigned int &uint, bool timeout)
287 {
288     STAFString_t errorBuffer = 0;
289     STAFRC_t rc =
290         fFuncTable->connReadUInt(fConn, &uint, &errorBuffer, timeout);
291 
292     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionReadUInt");
293 }
294 
295 
readString(bool timeout)296 STAF_INLINE STAFString STAFConnection::readString(bool timeout)
297 {
298     STAFString_t theString = 0;
299     STAFString_t errorBuffer = 0;
300     STAFRC_t rc =  fFuncTable->connReadSTAFString(fConn, &theString,
301                                                   &errorBuffer, timeout);
302 
303     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionReadSTAFString");
304 
305     return STAFString(theString, STAFString::kShallow);
306 }
307 
308 
readString(STAFString & theString,bool timeout)309 STAF_INLINE void STAFConnection::readString(STAFString &theString, bool timeout)
310 {
311     STAFString_t tempString = 0;
312     STAFString_t errorBuffer = 0;
313     STAFRC_t rc =  fFuncTable->connReadSTAFString(fConn, &tempString,
314                                                   &errorBuffer, timeout);
315 
316     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionReadSTAFString");
317 
318     theString.replaceImpl(tempString);
319 }
320 
321 
write(void * buffer,unsigned int size,bool timeout)322 STAF_INLINE void STAFConnection::write(void *buffer, unsigned int size,
323                                        bool timeout)
324 {
325     STAFString_t errorBuffer = 0;
326     STAFRC_t rc =
327         fFuncTable->connWrite(fConn, buffer, size, &errorBuffer, timeout);
328 
329     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionWrite");
330 }
331 
332 
writeUInt(unsigned int uint,bool timeout)333 STAF_INLINE void STAFConnection::writeUInt(unsigned int uint, bool timeout)
334 {
335     STAFString_t errorBuffer = 0;
336     STAFRC_t rc =
337         fFuncTable->connWriteUInt(fConn, uint, &errorBuffer, timeout);
338 
339     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionWriteUInt");
340 }
341 
342 
writeString(const STAFString & theString,bool timeout)343 STAF_INLINE void STAFConnection::writeString(const STAFString &theString,
344                                              bool timeout)
345 {
346     STAFString_t errorBuffer = 0;
347     STAFRC_t rc =  fFuncTable->connWriteSTAFString(fConn, theString.getImpl(),
348                                                    &errorBuffer, timeout);
349 
350     CHECK_FOR_CONN_IO_EXCEPTIONS("STAFConnectionWriteSTAFString");
351 }
352 
353 
getPeerNetworkIDs(STAFString & logicalIdentifier,STAFString & physicalIdentifier)354 STAF_INLINE void STAFConnection::getPeerNetworkIDs(
355     STAFString &logicalIdentifier,
356     STAFString &physicalIdentifier)
357 {
358     STAFStringConst_t logicalImpl = 0;
359     STAFStringConst_t physicalImpl = 0;
360     STAFString_t errorBuffer = 0;
361     STAFRC_t rc =  fFuncTable->connGetPeerNetworkIDs(fConn, &logicalImpl,
362                                                      &physicalImpl,
363                                                      &errorBuffer);
364 
365     CHECK_FOR_CONN_EXCEPTIONS("STAFConnectionGetPeerNetworkIDs");
366 
367     logicalIdentifier = logicalImpl;
368     physicalIdentifier = physicalImpl;
369 }
370 
371 
~STAFConnection()372 STAF_INLINE STAFConnection::~STAFConnection()
373 {
374     fFuncTable->connDestruct(&fConn, 0);
375 }
376 
377 
STAFConnection(STAFConnection_t conn,const STAFConnectionProviderFunctionTable * funcTable)378 STAF_INLINE STAFConnection::STAFConnection(
379     STAFConnection_t conn,
380     const STAFConnectionProviderFunctionTable *funcTable)
381     : fConn(conn), fFuncTable(funcTable)
382 {
383     // Do nothing
384 }
385 
386 #endif
387