/***************************************************************************
                          netclient.cpp  -  description
                             -------------------
    begin                : Mit Okt 13 2004
    copyright            : (C) 2004 by Harald Krippel
    email                : neuro.harald@surfeu.at
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "netclient.h"

#include <stdio.h>
#include <qapplication.h>

// PLIB Net
#include <plib/net.h>
#include <plib/ul.h>

netclient::netclient()
{
  netInit () ;
  ssock = NULL;
  rsock = NULL;
  rmsg[0]=0 ;
  rlen = 0 ;
  smsg[0]=0 ;
  slen = 0 ;
  maxlen = 256 ;
  
   // Users
  user=0;
// netAddress netuser[MAXUSER];
 userid[0]=0;
 status[0]=0;
 zone[0]=0;
 nickname[0][0]=0;
 version[0][0]=0;
}
netclient::~netclient()
{

}

int netclient::logon(const char *host,int rport)
{
 char shost[256] = "localhost" ;	// send to host
 int sport = 5501 ;			// on port
 char rhost[256] = "localhost" ;    // i recive from host

 strncpy(shost,host,255);
 strncpy(rhost,host,255);
 
 qWarning ( "Talking to host '%s' on port %d\n", shost, sport ) ;

 /*** init send ***/
  ssock = new netSocket () ;

  if ( ! ssock -> open ( false ) )    // open a UDP socket
  {
    qWarning( "error opening socket\n" );
    return -1 ;
  }

  ssock -> setBlocking ( false ) ;

  if ( ssock -> connect( shost, sport ) == -1 )
  {
    qWarning ( "error connecting to %s:%d\n", shost, sport ) ;
    return -1 ;
  }

  /*** init recive ***/

  rsock = new netSocket () ;

  if ( ! rsock -> open ( false ) )    // open a UDP socket
  {
    qWarning ( "error opening socket\n" ) ;
    return -1 ;
  }

  rsock -> setBlocking ( false ) ;

  if ( rsock -> bind( rhost, rport ) == -1 )
  {
    qWarning ( "error connecting to %s:%d\n", rhost, rport ) ;
    return -1 ;
  }

  return 0; 
}

void netclient::logoff()
{
     ssock -> close();
     rsock -> close();
     delete ssock;
     delete rsock;
     ssock = NULL;
     rsock = NULL;
}

void netclient::send(char *msg)
{
  int len=0;

  if(ssock != NULL)
  {
    len = strlen ( msg ) ;
    ssock -> send( msg, len, 0 );
    qWarning("msg sent = %s\n", msg);
    
  }
}

void netclient::update()
{
   if(rsock != NULL)
   {
//    qWarning("rsock ok\n");
    if ( (rlen = rsock -> recv(rmsg, maxlen, 0)) >= 0 )
    {
      rmsg[rlen] = '\0' ;
      qWarning ( "msg received = %s\n", rmsg ) ;
      message(rmsg);

    }
   }
   else
   {
      qWarning("rsock false\n");
   }
}

void netclient::message(char * smsg)
{
   int tmpuserid;
   int tmpeventid;
   int tmpstatus;
//   long tmpuserport;
//   char tmpnick[256];
   char tmpversion[256];
//   char usermsg[256];

      sscanf(smsg,"%d %d",&tmpeventid,&tmpuserid);

      switch(tmpeventid)
      {
        case 1:             // user logon
                if(tmpuserid != 0)
                {
                  // read values
                  sscanf(smsg,"%d %d %d %s"
                      ,&tmpeventid
                      ,&tmpuserid
                      ,&tmpstatus
                      ,tmpversion);
                  // validate and copy
                  userid[user]= tmpuserid;
                  status[user] = tmpstatus;
                  zone[user]= 1;
                  strcpy(&version[user][0],tmpversion);
                  // printout
                  qWarning("logon: id= %d status=%d  version=%s\n"
                      ,userid[user]
                      ,status[user]
                      ,&version[user][0]);
                }
            break;
        case 2:             // user logff
                if(tmpuserid != 0)
                {
                  // read values
                  sscanf(smsg,"%d %d %d"
                      ,&tmpeventid
                      ,&tmpuserid
                      ,&tmpstatus);
                  // validate and copy
                  userid[user]= 0;
                  status[user] = tmpstatus;
                  // printout
                  qWarning("logoff: id= %d status=%d\n"
                      ,userid[user]
                      ,status[user]);
                }

            break;
        case 100:           // user player
            break;
        case 101:           // user body
            break;
        case 102:           // user barrier
            break;
      }
}
