ServerSiftGPU.cpp
Go to the documentation of this file.
00001 
00002 //      File:           ServerSiftGPU.cpp
00003 //      Author:         Changchang Wu
00004 //      Description :   implementation for the ServerSiftGPU class.
00005 //
00006 //      Copyright (c) 2007 University of North Carolina at Chapel Hill
00007 //      All Rights Reserved
00008 //
00009 //      Permission to use, copy, modify and distribute this software and its
00010 //      documentation for educational, research and non-profit purposes, without
00011 //      fee, and without a written agreement is hereby granted, provided that the
00012 //      above copyright notice and the following paragraph appear in all copies.
00013 //      
00014 //      The University of North Carolina at Chapel Hill make no representations
00015 //      about the suitability of this software for any purpose. It is provided
00016 //      'as is' without express or implied warranty. 
00017 //
00018 //      Please send BUG REPORTS to ccwu@cs.unc.edu
00019 //
00021 
00022 #include <iostream>
00023 #include <vector>
00024 
00025 using std::cout;
00026 using std::vector;
00027 
00028 
00029 #if defined(SERVER_SIFTGPU_ENABLED)
00030 
00031 #include "GL/glew.h"
00032 
00033 #ifdef _WIN32
00034         #include <winsock2.h>
00035         #include <process.h>
00036         #define socklen_t int
00037     #pragma comment(lib,  "ws2_32.lib")
00038 #else
00039     #include <string.h>
00040     #include <stdio.h>
00041     #include <stdlib.h>
00042         #include <sys/types.h>
00043         #include <sys/socket.h>
00044         #include <unistd.h>
00045         #include <pthread.h>
00046         #include <spawn.h>
00047         #include <netdb.h>
00048         #include <netinet/in.h>
00049         //conversion from Win32
00050         typedef int SOCKET;
00051         #define INVALID_SOCKET -1
00052         #define closesocket close
00053 #endif
00054 
00055 #include "../SiftGPU/SiftGPU.h"
00056 #include "ServerSiftGPU.h"
00057 
00058 
00059 
00060 
00061 
00062 
00063 class SocketUtil
00064 {
00065 public:
00066         static int readline(SOCKET s, char *buf, int nread)
00067         {
00068                 char c;
00069                 int  num,n=0;
00070                 for(n=1; n<nread; n++)
00071                 {
00072                         if((num=recv(s,&c,1,0))==1)
00073                         {
00074                                 if(c== '\n')    break;  
00075                                 if(c==0) *buf++=' ';
00076                                 else *buf++=c;
00077                         }else if( num==0)
00078                         {
00079                                 if(n==1)        return 0;
00080                                 else            break;
00081                         }else 
00082                         {
00083                                 return -1;
00084                         }               
00085                 }
00086                 *buf=0; 
00087                 return n;
00088         }
00089         static int readint(SOCKET s, int* data, int count = 1)
00090         {
00091                 int size = (sizeof(int) * count);
00092                 data[0] = 0;
00093                 return recv(s, (char*)data, size, 0) == size;
00094         }
00095         static int writeint(SOCKET s, int data)
00096         {
00097                 int size = sizeof(int);
00098                 return send(s, (char*) (&data), size, 0) == size;
00099         }
00100         static int writeint(SOCKET s, unsigned int data)
00101         {
00102                 int size = sizeof(unsigned int);
00103                 return send(s, (char*) (&data), size, 0) == size;
00104         }
00105         static int writedata(SOCKET s, const void* data, int count)
00106         {
00107                 return send(s, (const char*)data, count, 0) == count;
00108         }
00109         static int readdata(SOCKET s, void* data, int count)
00110         {
00111                 int count_read_sum = 0, count_read;
00112                 char * p = (char*) data;
00113                 do
00114                 {
00115                         count_read =  recv(s, p, count - count_read_sum, 0);
00116                         p+= count_read;
00117                         count_read_sum += count_read;
00118                 }while(count_read > 0 && count_read_sum < count);
00119                 return  count_read_sum == count;
00120         }
00121         static int init()
00122         {
00123  
00124 #ifdef _WIN32
00125                 WSADATA                 WsaData; 
00126                 if(WSAStartup(0x0202, &WsaData))
00127                 {
00128                         std::cout << "server: can't startup socket\n";
00129                         return 0;
00130                 }else
00131         {
00132             return 1;
00133         }
00134 #else
00135                 return 1;
00136 #endif
00137 
00138         }
00139 };
00140 
00141 
00142 ServerSiftGPU::ServerSiftGPU(int port, char* remote_server)
00143 {
00144         _port = port;
00145         _socketfd = INVALID_SOCKET;
00146     _connected = 0;
00147         strcpy(_server_name, remote_server? remote_server : "\0");
00148 }
00149 
00150 int ServerSiftGPU::InitSocket()
00151 {
00152     return SocketUtil::init();
00153 }
00154 
00155 
00156 int ServerSiftGPU::StartServerProcess(int argc, char** argv)
00157 {
00158         vector<char*> args(argc + 4);   char ports[16];
00159 
00160     args[0] = "server_siftgpu";    
00161     args[1] = "-server";
00162         sprintf(ports, "%d", _port);    
00163     args[2] = ports;
00165         for(int i = 0; i < argc; ++i)   args[i + 3] = argv[i];
00166         args[argc + 3] = 0;
00168         //make a new process
00169 #ifdef _WIN32
00170     int result = (int) _spawnv(_P_NOWAIT, "server_siftgpu.exe", &args[0]);
00171     if(result == -1) std::cerr<< "spawn returns -1 with error = " << errno << "\n";
00172         return  result != -1;
00173 #else
00174         #ifdef __APPLE__
00175         #define LIBPATH_NAME "DYLD_LIBRARY_PATH"
00176         #else
00177         #define LIBPATH_NAME "LD_LIBRARY_PATH"
00178         #endif
00179         int result;     pid_t pid; 
00180     char* oldpath = getenv(LIBPATH_NAME);
00181     if(oldpath == NULL)
00182     {
00183             result =  posix_spawn(&pid, "server_siftgpu", NULL, NULL, &args[0], NULL);
00184     }else
00185     {
00186         char newpath[1024]= LIBPATH_NAME "=";
00187         strcat(newpath, oldpath);
00188         char* envp [] = {newpath, 0};
00189         result = posix_spawn(&pid, "server_siftgpu", NULL, NULL, &args[0], envp);
00190     }
00191         if(result) std::cerr << "failed to use poxis_spawn to create the server.\n";
00192         return  result == 0;
00193 #endif
00194 }
00195 
00196 int ServerSiftGPU::ConnectServer(const char* server_name, int port)
00197 {
00198         struct hostent* hh;
00199 
00200         if((hh = gethostbyname(server_name)) == NULL)return 0;
00201         
00203         struct sockaddr_in servaddr;    
00204         memset(&servaddr, 0, sizeof(servaddr));
00205         servaddr.sin_family = AF_INET;
00206         servaddr.sin_port = htons(port);
00207         servaddr.sin_addr.s_addr=((struct in_addr*)(hh->h_addr))->s_addr;
00208 
00209         ((SOCKET&)_socketfd) = socket(AF_INET, SOCK_STREAM, 0);
00210         if(_socketfd==INVALID_SOCKET) return 0;
00211 
00212         //if can not connect to server, start again
00213         if(connect(_socketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)))
00214         {
00215                 closesocket(_socketfd);
00216                 _socketfd = INVALID_SOCKET;
00217                 return 0;
00218         }else
00219         {
00220                 std::cout<<"Connected to server " << server_name << "\n";
00221                 return 1;
00222         }
00223 }
00224 
00225 void ServerSiftGPU::Disconnect()
00226 {
00227         SocketUtil::writeint(_socketfd, _server_name[0]? COMMAND_DISCONNECT : COMMAND_EXIT);
00228         closesocket(_socketfd);
00229         _socketfd = INVALID_SOCKET;
00230         _connected = 0;
00231 }
00232 
00233 ServerSiftGPU::~ServerSiftGPU()
00234 {
00235         if(_connected) Disconnect();
00236 }
00237 
00238 
00239 inline void ServerSiftGPU::ServerLoop(int port, int argc, char** argv)
00240 {
00241         SOCKET sockfd, newsockfd;       
00242         struct  sockaddr_in     serv_addr, cli_addr;
00243         socklen_t addr_len = sizeof(sockaddr_in);
00245         memset((char*)&serv_addr, 0, sizeof(serv_addr));
00246         serv_addr.sin_family    = AF_INET;
00247         serv_addr.sin_port      = htons(port);
00248         serv_addr.sin_addr.s_addr = INADDR_ANY;
00250 
00251     if(ServerSiftGPU::InitSocket() == 0)
00252         {
00253                 return;
00254         }
00256         sockfd=socket(AF_INET,SOCK_STREAM,0);
00257         if(sockfd==INVALID_SOCKET) 
00258         {
00259                 std::cout << "server: can't open stream socket\n";
00260                 return;
00261         }else if(bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr)))
00262     {
00263             std::cout << "server: can't bind to port " <<  port <<"\n";
00264             return;
00265     }else if(listen(sockfd, 1))
00266         {
00267                 std::cout << "server: failed to listen\n";
00268                 return;
00269         }else
00270         {
00271                 std::cout << "server: listent to port "<< port << "\n";
00272         }
00273                 
00274         newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &addr_len);
00275         if(newsockfd == INVALID_SOCKET)
00276         {
00277                 std::cout << "error: accept failed\n";
00278         closesocket(sockfd);
00279                 return;
00280         }
00282         char buf[1024];
00283         int command, result;
00284         int sift_feature_count = 0;;
00285         vector<SiftGPU::SiftKeypoint> keys;
00286         vector<float> descriptors;
00287         vector<char> databuf;
00288 
00290         SiftGPU siftgpu;
00291         SiftMatchGPU matcher;
00292 
00293         if(argc > 0) siftgpu.ParseParam(argc, argv);
00294     
00296         siftgpu.SetVerbose(0);
00298 
00299     do
00300     {
00301             while(SocketUtil::readint(newsockfd, &command) && command != COMMAND_DISCONNECT)
00302             {
00303                     switch(command)
00304                     {
00305                         case COMMAND_INITIALIZE:
00306                                 {
00307                                     result = (siftgpu.CreateContextGL() == SiftGPU::SIFTGPU_FULL_SUPPORTED);
00308                                     SocketUtil::writeint(newsockfd, result);
00309                                     if(result)  break;
00310                                 }
00311             case COMMAND_EXIT:
00312                 closesocket(newsockfd);
00313                 closesocket(sockfd);
00314                 return;
00315                     case COMMAND_ALLOCATE_PYRAMID:
00316                             {
00317                                     int size[2];
00318                                     SocketUtil::readint(newsockfd, size, 2);
00319                                     if(size[0] > 0 && size[1] > 0) siftgpu.AllocatePyramid(size[0], size[1]);
00320                                     break;
00321                             }
00322                     case COMMAND_GET_KEY_VECTOR:
00323                             {
00324                                     int size = sift_feature_count * sizeof(SiftGPU::SiftKeypoint);
00325                                     SocketUtil::writedata(newsockfd, &keys[0], size);
00326                                     break;
00327                             }
00328                     case COMMAND_GET_DES_VECTOR:
00329                             {
00330                                     int size = sift_feature_count * sizeof(float) * 128;
00331                                     SocketUtil::writedata(newsockfd, &descriptors[0], size);
00332                                     break;
00333                             }
00334                     case COMMAND_RUNSIFT:
00335                             {
00336                                     result = siftgpu.RunSIFT();
00337                                     if((sift_feature_count = siftgpu.GetFeatureNum()) > 0)
00338                                     {
00339                                             keys.resize(sift_feature_count);
00340                                             descriptors.resize(sift_feature_count * 128);
00341                                             siftgpu.GetFeatureVector(&keys[0], &descriptors[0]);
00342                                                 std::cout << "RunSIFT: [-] [" << sift_feature_count << "]\n";
00343                                     }
00344                                     SocketUtil::writeint(newsockfd, result);
00345                                     break;
00346                             }
00347                     case COMMAND_RUNSIFT_FILE:
00348                             {
00349                                     SocketUtil::readline(newsockfd, buf, 1024);
00350 
00351                                     result = siftgpu.RunSIFT(buf);
00352                                     if((sift_feature_count = siftgpu.GetFeatureNum()) > 0)
00353                                     {
00354                                             keys.resize(sift_feature_count);
00355                                             descriptors.resize(sift_feature_count * 128);
00356                                             siftgpu.GetFeatureVector(&keys[0], &descriptors[0]);
00357                                     }
00358                                         std::cout << "RunSIFT: "<< buf <<" " << sift_feature_count << "\n" ;
00359                                     SocketUtil::writeint(newsockfd, result);
00360                                     break;
00361                             }
00362                     case COMMAND_SET_KEYPOINT:
00363                             {
00364                                     int keys_have_orientation;
00365                                     SocketUtil::readint(newsockfd, &sift_feature_count);
00366                                     SocketUtil::readint(newsockfd, &keys_have_orientation);
00367                                     if(sift_feature_count > 0)
00368                                     {
00369                                             keys.resize(sift_feature_count);
00370                                             descriptors.resize(sift_feature_count * 128);
00371                                                 SocketUtil::readdata(newsockfd, &keys[0], int(keys.size() * sizeof(SiftGPU::SiftKeypoint)));
00372                                             siftgpu.SetKeypointList(sift_feature_count, &keys[0], keys_have_orientation);
00373                                     }
00374                                     break;
00375                             }
00376                     case COMMAND_RUNSIFT_KEY:
00377                             {
00378                                     int keys_have_orientation;
00379                                     SocketUtil::readint(newsockfd, &sift_feature_count);
00380                                     SocketUtil::readint(newsockfd, &keys_have_orientation);
00381                                     if(sift_feature_count > 0)
00382                                     {
00383                                                 std::cout << "RunSIFT: "<< sift_feature_count << " KEYPOINTS\n" ;
00384                                             int key_data_size = sift_feature_count * sizeof(SiftGPU::SiftKeypoint);
00385                                             keys.resize(sift_feature_count);
00386                                             descriptors.resize(sift_feature_count * 128);
00387                                             SocketUtil::readdata(newsockfd, &keys[0], key_data_size);
00388                                             result = siftgpu.RunSIFT(sift_feature_count, &keys[0], keys_have_orientation);
00389                                             siftgpu.GetFeatureVector(NULL, &descriptors[0]);
00390                                     }else
00391                                     {
00392                                             result = 0;
00393                                     }
00394                                     SocketUtil::writeint(newsockfd, result);
00395                                     break;
00396                             }
00397                     case COMMAND_RUNSIFT_DATA:
00398                             {
00399                                     int data_des[4], size = 0;  
00400                                     SocketUtil::readint(newsockfd, data_des, 4);
00401                                         SocketUtil::readint(newsockfd, &size, 1);
00402                                         std::cout << "RunSIFT: [" << data_des[0] << "x" << data_des[1] << "]";
00403 
00404                                         databuf.resize(size);
00405                                         void* data_ptr = &databuf[0];
00406                                         SocketUtil::readdata(newsockfd, data_ptr, size);
00407 
00408 
00409                                     result = siftgpu.RunSIFT(data_des[0], data_des[1], data_ptr, data_des[2], data_des[3]);
00410                                     if((sift_feature_count = siftgpu.GetFeatureNum()) > 0)
00411                                     {
00412                                             keys.resize(sift_feature_count);
00413                                             descriptors.resize(sift_feature_count * 128);
00414                                             siftgpu.GetFeatureVector(&keys[0], &descriptors[0]);
00415                                     }
00416                                         std::cout << "[" << sift_feature_count << "]\n";
00417                                     SocketUtil::writeint(newsockfd, result);
00418                                     break;
00419                             }
00420                     case COMMAND_SAVE_SIFT:
00421                             {
00422                                     SocketUtil::readline(newsockfd, buf, 1024);
00423                                     siftgpu.SaveSIFT(buf);
00424                                     break;
00425                             }
00426                     case COMMAND_SET_MAX_DIMENSION:
00427                             {
00428                                     int maxd;
00429                                     if(SocketUtil::readint(newsockfd, &maxd) && maxd > 0) siftgpu.SetMaxDimension(maxd);
00430                                     break;
00431                             }
00432                     case COMMAND_SET_TIGHTPYRAMID:
00433                             {
00434                                     int tight;
00435                                     if(SocketUtil::readint(newsockfd, &tight))  siftgpu.SetTightPyramid(tight);
00436                                     break;
00437                             }
00438                     case COMMAND_GET_FEATURE_COUNT:
00439                             {
00440                                     SocketUtil::writeint(newsockfd, sift_feature_count);
00441                                     break;
00442                             }
00443                         case COMMAND_PARSE_PARAM:
00444                                 {
00445                                     SocketUtil::readline(newsockfd, buf, 1024);
00446                                         std::cout << "ParseParam [" << buf << "]\n";
00447                                         vector<char*> params;
00448                                         char* p = buf;
00449                                         while(*p)
00450                                         {
00451                                                 while(*p == ' ' || *p == '\t')*p++ = 0;
00452                                                 params.push_back(p);
00453                                                 while(*p && *p != ' ' && *p != '\t') p++;
00454                                         }
00455                                         siftgpu.ParseParam(params.size(), &params[0]);
00456                                         break;
00457                                 }
00458                         case COMMAND_MATCH_INITIALIZE:
00459                                 {
00460                                         result = matcher.CreateContextGL();
00461                                         SocketUtil::writeint(newsockfd, result);
00462                                         break;
00463                                 }
00464                         case COMMAND_MATCH_SET_LANGUAGE:
00465                                 {
00466                                         int language;
00467                                         if(SocketUtil::readint(newsockfd, &language)) matcher.SetLanguage(language);
00468                                         break;
00469                                 }
00470                         case COMMAND_MATCH_SET_DES_FLOAT:
00471                                 {
00472                                         int command[3] = {0, 0, 0};
00473                                         if(SocketUtil::readdata(newsockfd, command, sizeof(command)))
00474                                         {
00475                                                 databuf.resize(sizeof(float) * 128 * command[1]);
00476                                                 if(SocketUtil::readdata(newsockfd, &databuf[0], databuf.size()))
00477                                                 {
00478                                                         matcher.SetDescriptors(command[0], command[1], (float*) (&databuf[0]), command[2]);     
00479                                                 }
00480                                         }
00481                                         break;
00482                                 }
00483                         case COMMAND_MATCH_SET_DES_BYTE:
00484                                 {
00485                                         int command[3] = {0, 0, 0};
00486                                         if(SocketUtil::readdata(newsockfd, command, sizeof(command)))
00487                                         {
00488                                                 databuf.resize(sizeof(unsigned char) * 128 * command[1]);
00489                                                 if(SocketUtil::readdata(newsockfd, &databuf[0], databuf.size()))
00490                                                 {
00491                                                         matcher.SetDescriptors(command[0], command[1], (unsigned char*) (&databuf[0]), command[2]);     
00492                                                 }
00493                                         }
00494                                         break;
00495                                 }
00496                         case COMMAND_MATCH_GET_MATCH:
00497                                 {
00498                                         int command[2]; float fcommand[2];
00499                                         result = 0;
00500                                         if( SocketUtil::readdata(newsockfd, command, sizeof(command)) &&
00501                                                 SocketUtil::readdata(newsockfd, fcommand, sizeof(fcommand)))
00502                                         {
00503                                                 int max_match  = command[0], mbm = command[1];
00504                                                 float distmax = fcommand[0], ratiomax = fcommand[1];
00505                                                 databuf.resize(max_match * 2 * sizeof(int));
00506                                                 result = matcher.GetSiftMatch(max_match, ( int(*)[2]) (&databuf[0]), distmax, ratiomax, mbm);
00507 
00508                                         }
00509                                         SocketUtil::writeint(newsockfd, result);
00510                                         if(result > 0) SocketUtil::writedata(newsockfd, &databuf[0], sizeof(int) * 2 * result);
00511                                         std::cout << "SiftMatch: " <<  result << "\n"; 
00512                                         break;
00513                                 }
00514                         case COMMAND_MATCH_SET_MAXSIFT:
00515                                 {
00516                                         int max_sift;
00517                                         if(SocketUtil::readint(newsockfd, &max_sift)) matcher.SetMaxSift(max_sift);
00518                                         break;
00519                                 }
00520                                 break;
00521                     default:
00522                             std::cout << "unrecognized command: " << command << "\n";
00523                                 break;
00524                     }
00525             }
00526 
00527         //client disconneted
00528         closesocket(newsockfd);
00529         //wait for the next client.
00530         std::cout << "wait for new client...";
00531             newsockfd = accept(sockfd, (struct sockaddr*) &cli_addr, &addr_len);
00532             if(newsockfd == INVALID_SOCKET)
00533             {
00534                     std::cout << "error: accept failed";
00535             closesocket(sockfd);
00536                     return;
00537             }else
00538         {
00539             std::cout << "connected\n\n";
00540         }
00541    }while(1);
00542 }
00543 
00544 void ServerSiftGPU::SetParamSiftGPU(int argc, char** argv)
00545 {
00546         if(!_connected || argc <= 0) return;
00547 
00548         char buf[1025], *p = buf, cret = '\n';
00549         for(int i = 0; i < argc; ++i)
00550         {
00551                 if(argv[i])     
00552                 {
00553                         strcpy(p, argv[i]);
00554                         p += strlen(argv[i]);
00555                 }
00556                 *p++= ((i +1 < argc)? ' ' : '\0');
00557         }
00558         SocketUtil::writeint(_socketfd, COMMAND_PARSE_PARAM);
00559         SocketUtil::writedata(_socketfd, buf, (p - buf));
00560         SocketUtil::writedata(_socketfd, &cret, 1);
00561 
00562 }
00563 
00564 int ServerSiftGPU:: InitializeConnection(int argc, char** argv)
00565 {
00566         char server_name[1024]; 
00567         if(!InitSocket()) return 0 ;
00568     if(_server_name[0] == 0)
00569     {
00570             if(StartServerProcess(argc, argv) == 0) 
00571             {
00572                     std::cout<<"Unable to start local siftgpu server\n";
00573             return 0 ;
00574             }
00575         strcpy(server_name, "127.0.0.1");
00576     }else
00577     {
00578         strcpy(server_name, _server_name);
00579     }
00580         
00582         if(ConnectServer(server_name, _port) == 0)
00583         {
00584         //wait for one second
00585 #ifdef _WIN32
00586             Sleep(1000);
00587 #else
00588             sleep(1);
00589 #endif
00590                 if(ConnectServer(server_name, _port) == 0)
00591                 {
00592                         std::cout<<"Unable to connect siftgpu sever\n";
00593                         return 0 ;
00594                 }
00595         }
00596         return 1;
00597 }
00598 
00599 void ServerSiftGPU::ParseParam(int argc, char **argv)
00600 {
00601     if(_connected == 1) 
00602         {
00603                 SetParamSiftGPU(argc, argv); 
00604         }else   
00605         {
00606                 _connected = InitializeConnection(argc, argv);
00607                 if(_server_name[0] && argc > 0) SetParamSiftGPU(argc, argv);
00608         }
00609 }
00610 
00611 int ServerSiftGPU::VerifyContextGL()
00612 {
00614         if(!_connected) return 0;
00615 
00616         int result = 0;
00617 
00618         if(SocketUtil::writeint(_socketfd, COMMAND_INITIALIZE) && 
00619                 SocketUtil::readint(_socketfd, &result) && result)
00620         {
00621                 return SiftGPU::SIFTGPU_FULL_SUPPORTED;
00622         }else
00623         {
00624                 std::cout<<"SifGPU failed to initialize\n";
00625                 Disconnect();
00626                 return 0;
00627         }
00628 }
00629 
00630 int     ServerSiftGPU::GetFeatureNum()
00631 {
00632         if(!_connected) return 0;
00633         int result = 0;
00634         SocketUtil::writeint(_socketfd, COMMAND_GET_FEATURE_COUNT);
00635         SocketUtil::readint(_socketfd, &result);
00636         return result;
00637 }
00638 
00639 int ServerSiftGPU::AllocatePyramid(int width, int height)
00640 {
00641         if(!_connected) return 0;
00642         int command[3] = {COMMAND_ALLOCATE_PYRAMID, width, height};
00643         return SocketUtil::writedata(_socketfd, command, sizeof(int) * 3);
00644 }
00645 
00647 void ServerSiftGPU::SaveSIFT(const char * szFileName)
00648 {
00649         if(!_connected) return;
00650 
00651         char cret = '\n';
00652         SocketUtil::writeint(_socketfd, COMMAND_SAVE_SIFT);
00653         SocketUtil::writedata(_socketfd, szFileName, (int)strlen(szFileName));
00654         SocketUtil::writedata(_socketfd, &cret, 1);
00655 
00656 }
00657 
00658 void ServerSiftGPU::GetFeatureVector(SiftGPU::SiftKeypoint * keys, float * descriptors)
00659 {
00660         if(!_connected) return;
00661 
00662         int num = GetFeatureNum(), result = 1;
00663         if(keys && num > 0)
00664         {
00665                 result&= SocketUtil::writeint(_socketfd, COMMAND_GET_KEY_VECTOR);
00666                 result &= SocketUtil::readdata(_socketfd, keys, num * sizeof(SiftGPU::SiftKeypoint));
00667         }
00668         if(descriptors && num > 0)
00669         {
00670                 result&= SocketUtil::writeint(_socketfd, COMMAND_GET_DES_VECTOR);
00671                 result&= SocketUtil::readdata(_socketfd, descriptors, num * 128 * sizeof(float));
00672         }
00673 }
00674 
00675 void ServerSiftGPU::SetKeypointList(int num, const SiftGPU::SiftKeypoint * keys, int keys_have_orientation)
00676 {
00677         if(!_connected) return;
00678 
00679         SocketUtil::writeint(_socketfd, COMMAND_SET_KEYPOINT);
00680         SocketUtil::writeint(_socketfd, num);
00681         SocketUtil::writeint(_socketfd, keys_have_orientation);
00682         SocketUtil::writedata(_socketfd, keys, sizeof(SiftGPU::SiftKeypoint) * num);    
00683 
00684 }
00685 
00686 void ServerSiftGPU::SetTightPyramid(int tight)
00687 {
00688         if(!_connected) return ;
00689         SocketUtil::writeint(_socketfd, COMMAND_SET_TIGHTPYRAMID);
00690         SocketUtil::writeint(_socketfd, tight);
00691 
00692 }
00693 
00694 void ServerSiftGPU::SetMaxDimension(int sz)
00695 {
00696         if(!_connected) return ;
00697         SocketUtil::writeint(_socketfd, COMMAND_SET_MAX_DIMENSION);
00698         SocketUtil::writeint(_socketfd, sz);
00699 
00700 }
00701 
00702 int ServerSiftGPU::GetPixelSizeGL(unsigned int gl_format, unsigned int gl_type)
00703 {
00704     int num_channel_byte = 0;
00705     int num_channels  = 0;
00706     switch(gl_type)
00707     {
00708     case GL_BITMAP:
00709     case GL_UNSIGNED_BYTE:
00710     case GL_BYTE:
00711         num_channel_byte = 1;
00712         break;
00713     case GL_UNSIGNED_SHORT:
00714     case GL_SHORT:
00715         num_channel_byte = 2;
00716         break;
00717     case GL_UNSIGNED_INT:
00718     case GL_INT: 
00719     case GL_FLOAT:
00720         num_channel_byte = 4;
00721         break;
00722     default:
00723         num_channel_byte = 0;
00724         break;
00725     }
00726 
00727     switch(gl_format)
00728     {
00729     case GL_RED:
00730     case GL_GREEN:
00731     case GL_BLUE:
00732     case GL_ALPHA:
00733     case GL_LUMINANCE:
00734         num_channels = 1;
00735         break;
00736     case GL_RGB:
00737     case GL_BGR_EXT:
00738         num_channels = 3;
00739         break;
00740     case GL_RGBA:
00741     case GL_BGRA_EXT:
00742 #ifdef GL_ARGB_I3D
00743     case GL_ARGB_I3D:
00744 #endif
00745         num_channels = 4;
00746         break;
00747     case GL_LUMINANCE_ALPHA:
00748 #ifdef GL_422_EXT
00749     case GL_422_EXT:
00750     case GL_422_REV_EXT: 
00751     case GL_422_AVERAGE_EXT:
00752     case GL_422_REV_AVERAGE_EXT:
00753 #endif
00754         num_channels = 2;
00755     default:
00756         num_channels = 0;
00757         break;
00758     }
00759     return num_channels * num_channel_byte;
00760 }
00761 
00762 int ServerSiftGPU::RunSIFT(int width, int height, const void * data, unsigned int gl_format, unsigned int gl_type)
00763 {
00764         if(width <=0 || height <= 0 || data == NULL || !_connected) return 0;
00765     int num_bytes = GetPixelSizeGL(gl_format , gl_type) * width * height;
00766     if(num_bytes == 0) return 0;
00767         SocketUtil::writeint(_socketfd, COMMAND_RUNSIFT_DATA);
00768         unsigned int data_des[5] = {width, height, gl_format, gl_type, num_bytes};
00769         SocketUtil::writedata(_socketfd, data_des, 5 * sizeof(unsigned int));
00770         SocketUtil::writedata(_socketfd, data, num_bytes);
00771         int result = 0; 
00772         return SocketUtil::readint(_socketfd, &result) && result;
00773 }
00774 
00775 int ServerSiftGPU::RunSIFT(int num, const SiftGPU::SiftKeypoint * keys, int keys_have_orientation)
00776 {
00777         if(num <= 0 || keys == NULL) return 0;
00778         if(!_connected) return 0;
00779         SocketUtil::writeint(_socketfd, COMMAND_RUNSIFT_KEY);
00780         SocketUtil::writeint(_socketfd, num);
00781         SocketUtil::writeint(_socketfd, keys_have_orientation);
00782         SocketUtil::writedata(_socketfd, keys, sizeof(SiftGPU::SiftKeypoint) * num);
00783         int result = 0; 
00784         return SocketUtil::readint(_socketfd, &result) && result;
00785 }
00786 
00787 int ServerSiftGPU::RunSIFT()
00788 {
00789         if(!_connected) return 0;
00790     int result = 0;
00791     SocketUtil::writeint(_socketfd, COMMAND_RUNSIFT);
00792         return SocketUtil::readint(_socketfd, &result) && result;
00793 }
00794 
00795 int ServerSiftGPU::RunSIFT(const char *imgpath)
00796 {
00797         if(!_connected) return 0;
00798         int result = 0; char cret = '\n';
00799         SocketUtil::writeint(_socketfd, COMMAND_RUNSIFT_FILE);
00800         SocketUtil::writedata(_socketfd, imgpath, (int)strlen(imgpath));
00801         SocketUtil::writedata(_socketfd, &cret, 1);
00802         return SocketUtil::readint(_socketfd, &result) && result;
00803 }
00804 
00805 
00807 int ServerSiftGPU::_VerifyContextGL()
00808 {
00809         if(!_connected) return 0;
00810         int result;
00811         if( SocketUtil::writeint(_socketfd, COMMAND_MATCH_INITIALIZE) &&
00812                 SocketUtil::readint(_socketfd, &result) && result)
00813         {
00814                 return 1;
00815         }else
00816         {
00817                 Disconnect();
00818                 return 0;
00819         }
00820 }
00821 
00822 void ServerSiftGPU::SetLanguage(int gpu_language)
00823 {
00824         if(!_connected) return ;
00825         SocketUtil::writeint(_socketfd, COMMAND_MATCH_SET_LANGUAGE);
00826         SocketUtil::writeint(_socketfd, gpu_language);
00827 }
00828 
00829 void ServerSiftGPU::SetDeviceParam(int argc, char**argv)
00830 {
00831     ServerSiftGPU::ParseParam(argc, argv);
00832 }
00833 
00834 
00835 void ServerSiftGPU::SetMaxSift(int max_sift)
00836 {
00837         if(!_connected) return;
00838         SocketUtil::writeint(_socketfd, COMMAND_MATCH_SET_MAXSIFT);
00839         SocketUtil::writeint(_socketfd, max_sift);
00840 }
00841 
00842 void ServerSiftGPU::SetDescriptors(int index, int num, const float* descriptors, int id)
00843 {
00844         if(!_connected) return ;
00845         int command[4] = {COMMAND_MATCH_SET_DES_FLOAT, index, num, id};
00846         SocketUtil::writedata(_socketfd, command, sizeof(command));
00847         SocketUtil::writedata(_socketfd, descriptors, sizeof(float) * 128 * num);
00848 }
00849 
00850 
00851 void ServerSiftGPU::SetDescriptors(int index, int num, const unsigned char * descriptors, int id)
00852 {
00853         if(!_connected) return ;
00854         int command[4] = {COMMAND_MATCH_SET_DES_BYTE, index, num, id};
00855         SocketUtil::writedata(_socketfd, command, sizeof(command));
00856         SocketUtil::writedata(_socketfd, descriptors, sizeof(unsigned char) * 128 * num);
00857 }
00858 
00859 int  ServerSiftGPU::GetSiftMatch(int max_match, int match_buffer[][2], 
00860         float distmax, float ratiomax,  int mutual_best_match)
00861 {
00862         if(!_connected) return 0;
00863         int command[3] = {COMMAND_MATCH_GET_MATCH, max_match, mutual_best_match};
00864         float fcommand[2] = {distmax, ratiomax};
00865         SocketUtil::writedata(_socketfd, command, sizeof(command));
00866         SocketUtil::writedata(_socketfd, fcommand, sizeof(fcommand));
00867         int nm; SocketUtil::readint(_socketfd, &nm);
00868         if(nm > 0) SocketUtil::readint(_socketfd, match_buffer[0], 2 * nm);
00869         return nm;
00870 }
00871 
00872 void RunServerLoop(int port, int argc, char** argv)
00873 {
00874     ServerSiftGPU::ServerLoop(port, argc, argv);
00875 }
00876 
00877 
00878 ComboSiftGPU* CreateRemoteSiftGPU(int port, char* remote_server)
00879 {
00880         return new ServerSiftGPU(port, remote_server);
00881 }
00882 
00883 
00884 #else
00885 
00886 #include "../SiftGPU/LiteWindow.h"
00887 #include "../SiftGPU/SiftGPU.h"
00888 
00889 ComboSiftGPU* CreateRemoteSiftGPU(int port, char* remote_server)
00890 {
00891     std::cout << "ServerSiftGPU need marcro SERVER_SIFTGPU_EANBLED.\n"
00892               << "Use local SiftGPU/SiftMatchGPU instead. \n";
00893         return new ComboSiftGPU;
00894 }
00895 
00896 void RunServerLoop(int port, int argc, char** argv)
00897 {
00898     std::cout << "ServerSiftGPU need marcro SERVER_SIFTGPU_EANBLED.\n"
00899  }
00900 
00901 #endif
00902 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


rgbd_registration
Author(s): Ross Kidson
autogenerated on Sun Oct 6 2013 12:00:40