2011-04-14 20:37:47 +02:00
|
|
|
/*
|
2011-05-10 16:42:00 +02:00
|
|
|
* $Id: imgsock_manager.c 54 2011-05-10 13:22:47Z kaori $
|
2011-04-14 20:37:47 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2011, Professor Benoit Macq
|
|
|
|
* Copyright (c) 2010-2011, Kaori Hagihara
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-05-09 20:11:40 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#else
|
|
|
|
#include <strings.h>
|
2011-04-14 20:37:47 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2011-05-10 16:42:00 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2011-04-14 20:37:47 +02:00
|
|
|
#include "imgsock_manager.h"
|
|
|
|
|
|
|
|
#define BUF_LEN 256
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
SOCKET open_listeningsocket()
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-05-10 16:42:00 +02:00
|
|
|
SOCKET listening_socket;
|
2011-04-14 20:37:47 +02:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
int sock_optval = 1;
|
|
|
|
int port = 5000;
|
2011-10-18 14:38:31 +02:00
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
listening_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if ( listening_socket == -1 ){
|
|
|
|
perror("socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
&sock_optval, sizeof(sock_optval)) == -1 ){
|
|
|
|
perror("setsockopt");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
2011-04-14 20:37:47 +02:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
if ( bind(listening_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0 ){
|
|
|
|
perror("bind");
|
2011-10-18 14:38:31 +02:00
|
|
|
close_socket(listening_socket);
|
2011-04-14 20:37:47 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( listen(listening_socket, SOMAXCONN) == -1){
|
|
|
|
perror("listen");
|
2011-10-18 14:38:31 +02:00
|
|
|
close_socket(listening_socket);
|
2011-04-14 20:37:47 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("port %d is listened\n", port);
|
|
|
|
|
|
|
|
return listening_socket;
|
|
|
|
}
|
|
|
|
|
2011-10-18 14:38:31 +02:00
|
|
|
SOCKET accept_socket( SOCKET listening_socket)
|
|
|
|
{
|
|
|
|
struct sockaddr_in peer_sin;
|
|
|
|
unsigned int addrlen = sizeof(peer_sin);
|
|
|
|
|
|
|
|
return accept( listening_socket, (struct sockaddr *)&peer_sin, &addrlen);
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
msgtype_t identify_clientmsg( SOCKET connected_socket)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-05-10 16:42:00 +02:00
|
|
|
int receive_size;
|
2011-04-14 20:37:47 +02:00
|
|
|
char buf[BUF_LEN];
|
2011-08-25 19:13:04 +02:00
|
|
|
char *magicid[] = { "JPIP-stream", "PNM request", "XML request", "TID request", "CID request", "CID destroy", "JP2 save", "QUIT"};
|
2011-05-09 20:11:40 +02:00
|
|
|
int i;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
receive_size = receive_line( connected_socket, buf);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if( receive_size == 0){
|
|
|
|
fprintf( stderr, "Error to receive the header of client message\n");
|
|
|
|
return MSGERROR;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2011-05-09 20:11:40 +02:00
|
|
|
for( i=0; i<NUM_OF_MSGTYPES; i++){
|
2011-04-14 20:37:47 +02:00
|
|
|
if( strncasecmp( magicid[i], buf, strlen(magicid[i])) == 0){
|
2011-08-25 19:13:04 +02:00
|
|
|
printf("%s\n", magicid[i]);
|
2011-04-14 20:37:47 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-05 15:35:31 +02:00
|
|
|
fprintf( stderr, "Cannot identify client message type %s\n", buf);
|
2011-05-10 16:42:00 +02:00
|
|
|
return MSGERROR;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2011-08-25 19:13:04 +02:00
|
|
|
Byte_t * receive_JPIPstream( SOCKET connected_socket, char *target, char *tid, char *cid, int *streamlen)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-07-05 01:00:21 +02:00
|
|
|
Byte_t *jpipstream=NULL, *ptr;
|
2011-08-25 19:13:04 +02:00
|
|
|
char buf[BUF_LEN], versionstring[] = "version 1.2";
|
2011-04-14 20:37:47 +02:00
|
|
|
int linelen, redlen, remlen;
|
|
|
|
|
|
|
|
target[0] = 0;
|
|
|
|
cid[0] = 0;
|
2011-09-16 16:20:00 +02:00
|
|
|
tid[0] = 0;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if((linelen = receive_line( connected_socket, buf)) == 0)
|
2011-04-14 20:37:47 +02:00
|
|
|
return NULL;
|
|
|
|
if( strncmp( versionstring, buf, strlen(versionstring))!=0){
|
|
|
|
fprintf( stderr, "Wrong format\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if((linelen = receive_line( connected_socket, buf)) == 0)
|
2011-04-14 20:37:47 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if( strstr( buf, "jp2")){
|
|
|
|
// register cid option
|
|
|
|
strcpy( target, buf);
|
2011-08-25 19:13:04 +02:00
|
|
|
|
|
|
|
if((linelen = receive_line( connected_socket, buf)) == 0)
|
|
|
|
return NULL;
|
2011-09-16 16:20:00 +02:00
|
|
|
if( strcmp( buf, "0") != 0)
|
|
|
|
strcpy( tid, buf);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if((linelen = receive_line( connected_socket, buf)) == 0)
|
2011-04-14 20:37:47 +02:00
|
|
|
return NULL;
|
2011-09-16 16:20:00 +02:00
|
|
|
if( strcmp( buf, "0") != 0)
|
|
|
|
strcpy( cid, buf);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if((linelen = receive_line( connected_socket, buf)) == 0)
|
2011-04-14 20:37:47 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*streamlen = atoi( buf);
|
2011-08-25 19:13:04 +02:00
|
|
|
fprintf( stderr, "Receive Data: %d Bytes\n", *streamlen);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-07-05 01:00:21 +02:00
|
|
|
jpipstream = (unsigned char *)malloc( (*streamlen));
|
|
|
|
ptr = jpipstream;
|
2011-04-14 20:37:47 +02:00
|
|
|
remlen = (*streamlen);
|
|
|
|
while( remlen > 0){
|
2011-05-10 16:42:00 +02:00
|
|
|
redlen = recv( connected_socket, ptr, remlen, 0);
|
2011-04-14 20:37:47 +02:00
|
|
|
if( redlen == -1){
|
2011-07-05 01:00:21 +02:00
|
|
|
fprintf( stderr, "receive JPT- JPP- stream error\n");
|
2011-04-14 20:37:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
remlen -= redlen;
|
|
|
|
ptr = ptr + redlen;
|
|
|
|
}
|
|
|
|
|
2011-07-05 01:00:21 +02:00
|
|
|
return jpipstream;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void send_stream( SOCKET connected_socket, void *stream, int length);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void send_XMLstream( SOCKET connected_socket, Byte_t *xmlstream, int length)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
|
|
|
Byte_t header[5];
|
|
|
|
|
|
|
|
header[0] = 'X';
|
|
|
|
header[1] = 'M';
|
|
|
|
header[2] = 'L';
|
|
|
|
header[3] = (length >> 8) & 0xff;
|
|
|
|
header[4] = length & 0xff;
|
|
|
|
|
|
|
|
send_stream( connected_socket, header, 5);
|
|
|
|
send_stream( connected_socket, xmlstream, length);
|
|
|
|
}
|
|
|
|
|
2011-08-25 19:13:04 +02:00
|
|
|
void send_IDstream( SOCKET connected_socket, char *id, int idlen, char *label);
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void send_CIDstream( SOCKET connected_socket, char *cid, int cidlen)
|
2011-08-25 19:13:04 +02:00
|
|
|
{
|
|
|
|
send_IDstream( connected_socket, cid, cidlen, "CID");
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_TIDstream( SOCKET connected_socket, char *tid, int tidlen)
|
|
|
|
{
|
|
|
|
send_IDstream( connected_socket, tid, tidlen, "TID");
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_IDstream( SOCKET connected_socket, char *id, int idlen, char *label)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
|
|
|
Byte_t header[4];
|
|
|
|
|
2011-08-25 19:13:04 +02:00
|
|
|
header[0] = label[0];
|
|
|
|
header[1] = label[1];
|
|
|
|
header[2] = label[2];
|
|
|
|
header[3] = idlen & 0xff;
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
send_stream( connected_socket, header, 4);
|
2011-08-25 19:13:04 +02:00
|
|
|
send_stream( connected_socket, id, idlen);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void send_PNMstream( SOCKET connected_socket, Byte_t *pnmstream, unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
|
|
|
int pnmlen = 0;
|
|
|
|
Byte_t header[7];
|
|
|
|
|
|
|
|
pnmlen = width*height*numofcomp;
|
|
|
|
|
|
|
|
header[0] = 'P';
|
|
|
|
header[1] = numofcomp==3 ? 6:5;
|
|
|
|
header[2] = (width >> 8) & 0xff;
|
|
|
|
header[3] = width & 0xff;
|
|
|
|
header[4] = (height >> 8) & 0xff;
|
|
|
|
header[5] = height & 0xff;
|
|
|
|
header[6] = maxval;
|
|
|
|
|
|
|
|
send_stream( connected_socket, header, 7);
|
|
|
|
send_stream( connected_socket, pnmstream, pnmlen);
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void send_stream( SOCKET connected_socket, void *stream, int length)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-05-09 20:11:40 +02:00
|
|
|
void *ptr = stream;
|
2011-04-14 20:37:47 +02:00
|
|
|
int remlen = length;
|
|
|
|
|
|
|
|
while( remlen > 0){
|
2011-05-10 16:42:00 +02:00
|
|
|
int sentlen = send( connected_socket, ptr, remlen, 0);
|
2011-04-14 20:37:47 +02:00
|
|
|
if( sentlen == -1){
|
|
|
|
fprintf( stderr, "sending stream error\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
remlen = remlen - sentlen;
|
|
|
|
ptr = ptr + sentlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
int receive_line(SOCKET connected_socket, char *p)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
while (1){
|
|
|
|
int ret;
|
2011-05-10 16:42:00 +02:00
|
|
|
ret = recv( connected_socket, p, 1, 0);
|
2011-04-14 20:37:47 +02:00
|
|
|
if ( ret == -1 ){
|
2011-05-10 16:42:00 +02:00
|
|
|
perror("receive");
|
2011-04-14 20:37:47 +02:00
|
|
|
exit(1);
|
|
|
|
} else if ( ret == 0 ){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( *p == '\n' )
|
|
|
|
break;
|
|
|
|
p++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if( len == 0)
|
2011-05-10 16:42:00 +02:00
|
|
|
fprintf( stderr, "Header receive error\n");
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
void response_signal( SOCKET connected_socket, bool succeed)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
|
|
|
Byte_t code;
|
|
|
|
|
|
|
|
if( succeed)
|
|
|
|
code = 1;
|
|
|
|
else
|
|
|
|
code = 0;
|
|
|
|
|
2011-05-10 16:42:00 +02:00
|
|
|
if( send( connected_socket, &code, 1, 0) != 1)
|
2011-04-14 20:37:47 +02:00
|
|
|
fprintf( stderr, "Response signalling error\n");
|
|
|
|
}
|
2011-10-18 14:38:31 +02:00
|
|
|
|
|
|
|
int close_socket( SOCKET sock)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return closesocket( sock);
|
|
|
|
#else
|
|
|
|
return close( sock);
|
|
|
|
#endif
|
|
|
|
}
|