#include #include #include "sparse_mask.h" #include "../server/coeff.h" #include "../server/combine.h" #include "tcpstream.h" //--------------- GLOBAL V A R I A B L E ----------------// combine * C; tcp_Stream * client; // ----------------------------------------------------- // u s e r _ r e q ue s t // obtain user request and perform validitily check. // Must make sure that input will not corrupt the server //--------------------------------------------------------- const int VIEW_NEW_IMAGE = 1; const int GET_MORE_DATA = 2; const int CLOSE_CONNECTION = 3; const int GET_LIST = 4; class image_descriptor { public: int width, height; char *description; char *fn; } *desc; class user_request { public: user_request () { filename =0 ;} ~user_request () { if (!filename) {delete [] filename; }} int request_type; char * filename; int level; int st_col; int en_col; int st_row; int en_row; }; user_request * USER_DECISION() { user_request * decision = new user_request (); // cout << " ================ WAITING FOR REQUEST =============" << endl; if (!client -> read_int ( &decision->request_type ) ) { decision -> request_type = CLOSE_CONNECTION; }; if (decision->request_type == VIEW_NEW_IMAGE ) { // cout << "VIEW NEW IMAGE" << endl; //------- read version int id; client -> read_int ( &id ); if (id != 0) client -> read_int( &id); // if (dummy!=0) { cout << "WRONG VERION " << endl;} //------- read compression type client -> read_int ( &client -> COMPRESSION_TYPE ); //------- read_string not yet implement // char tmp [50]; // client -> read_string ( tmp, 50 ); //---- dummy --- // char* tmp = "/spunky.a/misc/visual/usr/yentj/image/test/t"; // char* tmp = "/spunky.a/misc/visproj/tmp/eechien/image/test/t"; // char* tmp = "/tmp/eechien/test/t"; //in mrslate and dino decision -> filename = desc[id].fn; cout << "Using file : " << desc[id].fn << endl; } else if (decision ->request_type== GET_MORE_DATA) { client -> read_int ( & decision -> level ); client -> read_int ( & decision -> st_row ); client -> read_int ( & decision -> en_row ); client -> read_int ( & decision -> st_col ); client -> read_int ( & decision -> en_col ); // cout << "read an request " << decision -> level << " " // << decision -> st_row<< " " // << decision -> en_row<< " " // << decision -> st_col<< " " // << decision -> en_col<< " " << endl; // Request validation // to - be - completed. } else if (decision ->request_type!= GET_LIST) { decision -> request_type == CLOSE_CONNECTION; } return decision; } void send_average_across() { unsigned char * tmp; int ave_size = C -> getaverage( &tmp); cout << "sending the average... " << endl; client->total_pixel_write += ave_size/(C -> Byte_Per_Pixel); cout << "sending " << ave_size<< " byte" << endl; int total = client -> write_byte (tmp, ave_size ); delete []tmp; cout << "============== " << total << " bytes=======" << endl; } void send_header_across() { cout << "sending header acrosss " << endl; client->write_int ( C -> Image_Width ); client->write_int ( C -> Image_Height ); client->write_int ( C -> Byte_Per_Pixel ); client->write_int ( C -> Nos_Level ); } void serve_a_client ( int client_stream_id) { int getDB(image_descriptor *&); cout << "============== START SERVER MAIN LOOP =====================" << endl; //------------------------------------------- // form the io stream. //------------------------------------------- client = new tcp_Stream (client_stream_id); C = 0; //------------------------------------------- // m a i n l o o p //------------------------------------------- int flag = 1; int num = getDB(desc); while (flag) { //------------------------------------------- // get-request from user //------------------------------------------- user_request * choice = USER_DECISION (); if ( choice -> request_type == VIEW_NEW_IMAGE) { //------------------------------------------- // build data-structure ... sparse matrix etc for image. //------------------------------------------- // cout << "rebuild data-structure " // << choice -> filename << endl; if (!C ) delete C; C = new combine ( choice -> filename ); // cout << "data-structure build" << endl; //------------------------------------------- // send the header infomation across. //------------------------------------------- send_header_across (); //------------------------------------------- // send the average across //------------------------------------------- send_average_across(); } else if ( choice -> request_type == GET_MORE_DATA ) { cout << " get more data " << endl; //------------------------------------------- // formating reply //------------------------------------------- unsigned char * tmp; int total; // C->request will perform bound check. // return 0 if fail. // In this program, it will just break connection and halt. tmp = C -> request ( &total, choice->level, choice->st_row, choice->en_row, choice->st_col, choice->en_col ); if (tmp == 0 ) { cout << "CORRUPTED DATA: " << " request :: level " << choice->level << endl << " st_row " << choice->st_row << endl << " en_row " << choice->en_row << endl << " st_col " << choice->st_col << endl << " en_col " << choice->en_col << endl; cout << " BAD CLIENT..... trying to corrupt me... " << endl; exit (0); } client->total_pixel_write += total/(C->Byte_Per_Pixel); //------------------------------------------- // send-reply //------------------------------------------- if (total >0 ) { int tt = client->write_byte ( tmp , total/4 ); tt += client->write_byte (&tmp[ total/4], total/4 ); tt += client->write_byte (&tmp[2 * total/4], total/4 ); tt += client->write_byte (&tmp[3 * total/4], total/4 ); // int tt = client->write_byte ( tmp , total ); delete [] tmp; cout << " ======= byte send in this req "< total_byte_write << endl; } } else if ( choice -> request_type == GET_LIST) { client->write_int(num); int back_compress = client->COMPRESSION_TYPE; client->COMPRESSION_TYPE = client->COMPRESS_NON; for (int i = 0 ; i < num ; i ++) { unsigned char *ptr = (unsigned char *)(desc[i].description); int len = strlen(desc[i].description); client->write_int(len+1); client->write_byte(ptr, (long unsigned)(len + 1)); client->write_int(desc[i].width); client->write_int(desc[i].height); } client->COMPRESSION_TYPE = back_compress; } else if ( choice -> request_type == CLOSE_CONNECTION ) { flag = 0; cout << " ===== coeff*byte_per_coeff (compressed) " << client -> total_compressed_coeff_write << endl; cout << " ===== total overhead (byte) " << client -> total_byte_write - client -> total_compressed_coeff_write << endl; cout << " ===== total byte send in this session " << client -> total_byte_write << endl; cout << " ===== total coeff*byte_per_coeff " << client -> total_pixel_write * C->Byte_Per_Pixel << endl; } delete choice; } // while cout << " Break contact " << endl; } const int lineSize = 1024; int getDB(image_descriptor *&db) { ifstream F; int size; char inBuf[lineSize]; F.open("desc", ios::in); F >> size; F.getline(inBuf, lineSize); db = new image_descriptor[size]; for (int i = 0 ; i < size ; i ++) { F.getline(inBuf, lineSize); //Get filename int len = F.gcount(); db[i].fn = new char[len + 1]; strcpy(db[i].fn, inBuf); F.getline(inBuf, lineSize); // Get one-line description len = F.gcount(); db[i].description = new char[len + 1]; strcpy(db[i].description, inBuf); F >> db[i].width >> db[i].height; // Get image size F.getline(inBuf, lineSize); } F.close(); return size; }