0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
* Filename: CXClient.cpp
|
|
16 |
* Author: Sanjeet Matharu
|
|
17 |
* This is the client side of the protocol which takes name/value pairs and does stuff
|
|
18 |
* System Includes
|
|
19 |
*
|
|
20 |
*/
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
#include <stdio.h>
|
|
25 |
#include <stdlib.h>
|
|
26 |
#include <ctype.h>
|
|
27 |
#include <memory.h>
|
|
28 |
#include <string.h>
|
|
29 |
#include <assert.h>
|
|
30 |
|
|
31 |
#ifdef WIN32
|
|
32 |
#include <winsock2.h>
|
|
33 |
#include <windows.h>
|
|
34 |
#else
|
|
35 |
#include <sys/types.h>
|
|
36 |
#include <sys/socket.h>
|
|
37 |
#endif
|
|
38 |
|
|
39 |
/*******************************************************************************
|
|
40 |
*
|
|
41 |
* Local Includes
|
|
42 |
*
|
|
43 |
******************************************************************************/
|
|
44 |
#include "cxclient.h"
|
|
45 |
|
|
46 |
#define MAXRETRIES 5
|
|
47 |
|
|
48 |
//------------------------------------------------------------------------------
|
|
49 |
//constructor
|
|
50 |
CXClient::CXClient()
|
|
51 |
{
|
|
52 |
iClientStatus = SENDING_HELLO;
|
|
53 |
|
|
54 |
sprintf(hellobuffer, "%s", "HELLO");
|
|
55 |
sprintf(goodbyebuffer, "%s", "GOODBYE");
|
|
56 |
}
|
|
57 |
|
|
58 |
//------------------------------------------------------------------------------
|
|
59 |
//destructor
|
|
60 |
CXClient::~CXClient()
|
|
61 |
{
|
|
62 |
|
|
63 |
}
|
|
64 |
|
|
65 |
//------------------------------------------------------------------------------
|
|
66 |
//this function adds the specified value into the list of name value pairs
|
|
67 |
int CXClient::OnExecute(int handle, char* cValue, int dim1, int dim2)
|
|
68 |
{
|
|
69 |
int error = 0;
|
|
70 |
int localsocket = handle;
|
|
71 |
int counter = 0;
|
|
72 |
|
|
73 |
if(localsocket != 0)
|
|
74 |
{
|
|
75 |
error = SendData(localsocket, hellobuffer);
|
|
76 |
//send the hello to the server side first
|
|
77 |
if( error != OK_DATATRANSFER)
|
|
78 |
{
|
|
79 |
//give up and return a message
|
|
80 |
printf("There was a problem saying hello...\n");
|
|
81 |
return -1;
|
|
82 |
}
|
|
83 |
}
|
|
84 |
else
|
|
85 |
{
|
|
86 |
printf("There was a problem with the socket...\n");
|
|
87 |
return -1;
|
|
88 |
}
|
|
89 |
|
|
90 |
//go through the valuebuffer and send the data
|
|
91 |
for(counter = 0; counter < dim1; counter++)
|
|
92 |
{
|
|
93 |
error = SendData(localsocket, &cValue[counter*dim2]);
|
|
94 |
if( error != OK_DATATRANSFER)
|
|
95 |
{
|
|
96 |
//give up and return a message
|
|
97 |
printf("There was a problem sending the data...\n");
|
|
98 |
return -1;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
//send the goodbye to the server side
|
|
103 |
if(localsocket != 0)
|
|
104 |
{
|
|
105 |
error = SendData(localsocket, goodbyebuffer);
|
|
106 |
if( error != OK_DATATRANSFER )
|
|
107 |
{
|
|
108 |
//give up and return a message
|
|
109 |
printf("There was a problem when saying goodbye...\n");
|
|
110 |
return -1;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
else
|
|
114 |
{
|
|
115 |
printf("There was a problem with the socket...\n");
|
|
116 |
return -1;
|
|
117 |
}
|
|
118 |
|
|
119 |
return 0;
|
|
120 |
}
|
|
121 |
|
|
122 |
//------------------------------------------------------------------------------
|
|
123 |
//this sends the hello command to the client over the specified socket to initiate comms
|
|
124 |
int CXClient::SendData(int handle, char* aValue)
|
|
125 |
{
|
|
126 |
char c; //this is the buffer that is used to receive the data from across the link
|
|
127 |
char receivebuffer[MAXBUFFERLEN];
|
|
128 |
|
|
129 |
int counter = 0;
|
|
130 |
int bufferlength = 0;
|
|
131 |
int sendcounter = 0;
|
|
132 |
int err = -1;
|
|
133 |
|
|
134 |
bool success = false;
|
|
135 |
memset(receivebuffer, 0, sizeof(receivebuffer)); //initialise
|
|
136 |
|
|
137 |
bufferlength = strlen(aValue);
|
|
138 |
|
|
139 |
//send the data
|
|
140 |
while( sendcounter < MAXRETRIES )
|
|
141 |
{
|
|
142 |
//write to the socket
|
|
143 |
err = WriteClient( handle, HT_SOCKET, aValue, bufferlength );
|
|
144 |
|
|
145 |
//if unable to send for some reason (verify that the error code is the length of one char)
|
|
146 |
if( err != bufferlength )
|
|
147 |
{
|
|
148 |
printf("Data %s sending failed %d...\n", aValue, err);
|
|
149 |
sendcounter++;
|
|
150 |
}
|
|
151 |
else //print ok
|
|
152 |
{
|
|
153 |
printf("Data %s sending ok %d chars sent...\n", aValue, bufferlength);
|
|
154 |
sendcounter = MAXRETRIES;
|
|
155 |
//send an end of string character so the server can evaluate what has been received
|
|
156 |
WriteClient( handle, HT_SOCKET, "", 1 );
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
//receive the reply ONLY if send has succeeded (hence inside the maxretries limit)
|
|
161 |
if( ( sendcounter <= MAXRETRIES ) )
|
|
162 |
{
|
|
163 |
while( 1 )
|
|
164 |
{
|
|
165 |
err = ReadClient( handle, HT_SOCKET, &c );
|
|
166 |
|
|
167 |
if( strcmp( &c, "" ) == 0)
|
|
168 |
{
|
|
169 |
//if there was a problem with the read (i.e. invalid command received from server, knackered link)
|
|
170 |
if( ( err <= 0 ) || ( strcmp(receivebuffer, aValue) != 0 ) )
|
|
171 |
{
|
|
172 |
printf("Data read failed ( device returned %s and receive code was %d )...\n", receivebuffer, err);
|
|
173 |
memset(receivebuffer, 0, sizeof(receivebuffer)); //reset
|
|
174 |
return ERROR_DATATRANSFER;
|
|
175 |
}
|
|
176 |
else
|
|
177 |
{
|
|
178 |
printf("Data read succeeded ( device returned %s )...\n", receivebuffer);
|
|
179 |
memset(receivebuffer, 0, sizeof(receivebuffer)); //reset
|
|
180 |
counter = 0; //reset
|
|
181 |
break;
|
|
182 |
}
|
|
183 |
} //if
|
|
184 |
|
|
185 |
receivebuffer[counter++] = c;
|
|
186 |
} //while
|
|
187 |
}
|
|
188 |
|
|
189 |
return OK_DATATRANSFER;
|
|
190 |
}
|
|
191 |
//------------------------------------------------------------------------------
|
|
192 |
int CXClient::ReadClient(int handle, int handletype, char* c)
|
|
193 |
{
|
|
194 |
int err;
|
|
195 |
|
|
196 |
assert( c != NULL );
|
|
197 |
|
|
198 |
// socket handles
|
|
199 |
if( handletype == HT_SOCKET )
|
|
200 |
{
|
|
201 |
err = recv( handle, c, 1, 0 );
|
|
202 |
if( err != 1 ) //if the single char has not been received
|
|
203 |
{
|
|
204 |
err = GetLastError();
|
|
205 |
return err;
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
// success
|
|
210 |
return err;
|
|
211 |
}
|
|
212 |
|
|
213 |
//------------------------------------------------------------------------------
|
|
214 |
int CXClient::WriteClient(int handle, int handletype, char *buff, int bufflen )
|
|
215 |
{
|
|
216 |
int err = 0;
|
|
217 |
unsigned long bytes_written = 0;
|
|
218 |
|
|
219 |
assert( buff != NULL );
|
|
220 |
|
|
221 |
// socket handles
|
|
222 |
if( handletype == HT_SOCKET )
|
|
223 |
{
|
|
224 |
err = send( handle, buff, bufflen, 0 );
|
|
225 |
if( err != bufflen )
|
|
226 |
{
|
|
227 |
err = GetLastError();
|
|
228 |
return err;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
return err;
|
|
233 |
}
|
|
234 |
|
|
235 |
//------------------------------------------------------------------------------
|