/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * COPYING NOTES
 *
 * sclient.cpp -- client function body
 * 
 * Copyright (C) 2002 Roberto A. Foglietta <robang@libero.it>
 * Copyright (C) 2002 GEA-Automotive <fogliettar@gea-automotive.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * REVISION NOTES:
 * released 21-10-2002 by Roberto A. Foglietta
 */

#include "../ClientSocket.h"
#include "../SocketException.h"
#include <iostream>
#include <string>

extern "C"
{
#include "sclient.h"

int
my_socket_client (char *phost, int port, void *refresh)
{
	int k = 300;
	std::string host;
	std::string reply;

	host = "";
	if (phost != NULL) {
		host = (std::string) phost;
		if (port <= 0)
			port = _PORT;
	} else {
		host = (std::string) _HOST;
		port = _PORT;
	}
	std::cout << "Trying to connect at " << host << " port " << port << "\n";

	try {
		ClientSocket client_socket (host, port);
		while (k) {
			try {
				std::cin >> reply;
				client_socket << reply;
				client_socket >> reply;
			}
			catch ( SocketException & e ) {
				std::cout << "(2) Exception was caught:" << e.description () << "\n";
				exit(-3);
			}
			std::cout << "\nWe received this response from the server:\n\"" << reply << "\"\n";;
						
			if (!strncasecmp (reply.c_str (), _STOP, strlen (_STOP)))
				return 0;
			else if ( strncasecmp (reply.c_str (), _FAIL, strlen (_FAIL)) )
				((REFRESH_FNC) refresh) (reply.c_str ());
			k--;
		}

	}
	catch (SocketException & e) {
		std::cout << "(2) Exception was caught:" << e.description () << "\n";
		exit(1);
	}

	return 0;
}}