// Server source code

/*
Author: Srikanta Patnaik and Team 
Machine Intelligence Laboratory, University College of Engineering, Burla, India
Date of Writing: April, 2003; Version: 1.0
Book: Robot Cognition and Navigation: An Experiment with Mobile Robots
Publisher: Springer
ISBN: 978-3-540-23446-3
URL: http://www.springer.com/3-540-23446-2
*/


//imageserver.ccp
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include "Aria.h"
#include "svsclass.h"

#define H 240
#define W 320

ArSocket server,client;

svsVideoImages *videoObject; 
svsStereoImage *imageObject;
int sfd,cfd;

unsigned char color_image[4*W*H+104];
unsigned char left_image[W*H+104];

void shutdown(int signum)
{
printf("Closing video device and TCP connection ...\n");
videoObject->Stop();
videoObject->Close();
server.close();
client.close();
Aria::shutdown();
exit(0);
}

int main(int argc, char **argv)
{
long k,acount,ck;
unsigned char prev,pr,pg,pb,count,status='c',zero=0;
//term signal handler
struct sigaction sa;
memset(&sa,0,sizeof(sa));
sa.sa_handler = &shutdown;
sigaction(SIGINT,&sa,NULL);
	
videoObject = getVideoObject(); 
//size
svsImageParams *ip=videoObject->GetIP();
ip->width=W;
ip->height=H;
//sample
videoObject->decimation=2;
videoObject->binning=2;
svsHasMMX=true;
ip->linelen=ip->width;
ip->lines=ip->height;
ip->vergence=0;

bool ret;

videoObject->ReadParams("/root/megad-75.ini");

ret = videoObject->Open(0);
if (!ret) {printf("Can't open frame grabber.\n"); return 0; } 
else printf("Opened frame grabber.\n");
if (!videoObject->CheckParams()) 
{
	printf("Incorrect  Params\n");
	videoObject->Close();
	exit(1);
}

Aria::init(Aria::SIGHANDLE_THREAD);
	
if(server.open(9904,ArSocket::TCP))
printf("Opened the server port\n");
else
{
printf("Failed to open the server port: %s\n",
server.getErrorStr().c_str());
return(-1);
}

if(server.accept(&client))
printf("Client has connected\n");
else
printf("Error in accepting a connection from the client: %s\n",
	
server.getErrorStr().c_str());
//client
{
/* Handle a request from the connection.  We have our own copy of the connected socket descriptor.  */

videoObject->Start();
videoObject->SetColor(true);
videoObject->exposure=100;
videoObject->gain=100;
videoObject->blue=30;
videoObject->red=17;
videoObject->SetDigitization();
acount=0;
	
while (1) 
{
char data[50];
char ack[50];
imageObject = videoObject->GetImage(400);
        
if (imageObject == NULL)   
{ printf("No image, timed out...\n"); continue; }
  k=0;count=0;ck=0;
memcpy(color_image,imageObject->color,4*W*H);
memcpy(left_image,imageObject->left,W*H);

acount++;
if(acount%10==0)
{
for(int i=0;i<H;i++)
{
for(int j=0;j<W;j++)
{
sprintf(data,"%c%c%c%c",color_image[ck],
color_image[ck+1],color_image[ck+2],char(1));
client.write(data,4);
}		  
ck=ck+4;
}
} 
			
sprintf(data,"%c%c%c%c",char(0),char(0),char(0),char(0));
client.write(data,4);
}//while
}//client
    
client.close();
server.close();
videoObject->Stop();
videoObject->Close();
Aria::shutdown();
}//main

/*
Compilation and execution: The following text commands are used for compilation and execution of image server program

g++ -c -I$SVS/src imageserver.cpp
g++ -o image -L$SVS/bin -lcap -lsvs -lm -pthread 

*/
