corrected to accept hostname and connect to network
[csql.git] / src / sqlnetwork / SqlNwConnection.cxx
blobd4570f501e8019894814015c5e3ddae9b61d9347
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <SqlNwConnection.h>
21 #include <Network.h>
22 #include<os.h>
23 #include <CSql.h>
25 DbRetVal SqlNwConnection::connect (char *user, char * pass)
27 DbRetVal rv = OK;
28 if (isConnOpen) {
29 printError(ErrAlready, "Connection is already open");
30 return ErrAlready;
32 nwClient = new TCPClient();
33 int bufsize = 2 * IDENTIFIER_LENGTH;
34 // char hostName[IDENTIFIER_LENGTH];
35 // memset(hostName, 0, IDENTIFIER_LENGTH);
36 //os::gethostname(hostName, IDENTIFIER_LENGTH);
37 nwClient->setHost(hostname, port, 123);
38 rv = nwClient->connect();
39 if (rv != OK) {
40 printError(rv, "Connection failed");
41 return rv;
43 SqlPacketConnect *pkt = new SqlPacketConnect();
44 pkt->setConnParam(user, pass);
45 pkt->setBufferSize(bufsize);
46 char * buffer = (char *) malloc(bufsize);
47 pkt->setBuffer(buffer);
48 pkt->marshall();
49 rv = send(SQL_NW_PKT_CONNECT, buffer, bufsize);
50 if (rv != OK) {
51 printError(rv, "Data could not be sent");
52 return rv;
54 int response = 0;
55 rv = nwClient->receive();
56 ResponsePacket *rpkt = (ResponsePacket *) ((TCPClient *)nwClient)->respPkt;
57 char *ptr = (char *) &rpkt->retVal;
58 if (*ptr != 1) {
59 printError(ErrPeerResponse, "%s", rpkt->errorString);
60 nwClient->disconnect();
61 delete nwClient;
62 delete pkt;
63 return ErrPeerResponse;
65 isConnOpen = true;
66 delete pkt;
67 return rv;
69 DbRetVal SqlNwConnection::disconnect()
71 DbRetVal rv = OK;
72 if (! isConnOpen) {
73 printError(ErrNoConnection, "No connection present");
74 return ErrNoConnection;
76 rv = nwClient->disconnect();
77 isConnOpen=false;
78 delete nwClient;
79 return rv;
82 DbRetVal SqlNwConnection::beginTrans(IsolationLevel isoLevel, TransSyncMode mode)
84 if (! isConnOpen) {
85 printError(ErrNoConnection, "No connection present");
86 return ErrNoConnection;
88 return OK;
91 DbRetVal SqlNwConnection::commit()
93 if (! isConnOpen) {
94 printError(ErrNoConnection, "No connection present");
95 return ErrNoConnection;
97 DbRetVal rv = OK;
98 rv = nwClient->send(SQL_NW_PKT_COMMIT, NULL, 0);
99 int response = 0;
100 return nwClient->receive();
103 DbRetVal SqlNwConnection::rollback()
105 if (! isConnOpen) {
106 printError(ErrNoConnection, "No connection present");
107 return ErrNoConnection;
109 DbRetVal rv = OK;
110 rv = nwClient->send(SQL_NW_PKT_ROLLBACK, NULL, 0);
111 int response = 0;
112 return nwClient->receive();