// Listing of server program  

/*
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
 
*/

// filename : image.cpp
#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;
// for the images
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,px,count,status='c',zero=0;
//term signal handler
struct sigaction sa;
memset(&sa,0,sizeof(sa));
sa.sa_handler = &shutdown;
sigaction(SIGINT,&sa,NULL);
// the video object
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;
// get the parameters
videoObject->ReadParams("/root/megad-75.ini");
// open the video Object
ret = videoObject->Open(0);
// if errors exist in opening then quit
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);
}

// run in single thread
Aria::init(Aria::SIGHANDLE_THREAD);

// open the server socket
if(server.open(9905,ArSocket::TCP))
printf("Opened the server port\n");
else
{
printf("Failed to open the server port: %s\n",
server.getErrorStr().c_str());
return(-1);
}
// wait for the client to join
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());    
// start the image collection 
videoObject->Start();
videoObject->SetColor(true);

// set the values
videoObject->exposure=100;
videoObject->gain=99;
videoObject->blue=20;
videoObject->red=20;
videoObject->SetDigitization();

while (1) 
{
char data[50];
char ack[50];
// get the image
imageObject = videoObject->GetImage(400);
    
if (imageObject == NULL)   
{  printf("No image, timed out...\n"); continue; }
// set initial values
k=0;count=0;acount=0,ck=0;
// collect the color and gray images
memcpy(color_image,imageObject->color,4*W*H);
memcpy(left_image,imageObject->left,W*H);

//first values
prev=left_image[0];
pr=color_image[0];
pg=color_image[1];
pb=color_image[2];
	
// run length encoding program
for(int i=0;i<H;i++)
{
for(int j=0;j<W;j++)
{
if ((abs(left_image[k]-prev)>8)||(k>=H*W-1))
{		
sprintf(data,"%c%c%c%c",pr,pg,pb,count);
					
// write the RGB and count 
client.write(data,4);
count=0;
prev = left_image[k];
pr=color_image[ck];
pg=color_image[ck+1];
pb=color_image[ck+2];	
}		  
count++;
k++;
ck=ck+4;
}
sprintf(data,"%c%c%c%c",char(0),char(0),char(0),char(0));
client.write(data,4);
}
}//while
  
// close all
client.close();
server.close();
videoObject->Stop();
videoObject->Close();  
Aria::shutdown();
}//main


/*
For Compiling and linking, the following commands are used:

g++ -o image -I$ARIA/include L$ARIA/lib -I$SVS/src -L$SVS/bin lAria ldl -lcap -lsvs -lm -pthread  image.cpp
*/

