Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/tracking/trackerd_position.cc

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 Peter Amstutz, Reed Hedges
00006 
00007     This software was written at the University of Massachusetts with
00008     support from NSF grant #EIA 9703217
00009 
00010     This library is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2 of the License, or (at your option) any later version.
00014 
00015     This library is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this library; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00023 
00024     Peter Amstutz <tetron@interreality.org>
00025     Reed Hedges <reed@interreality.org>
00026 */
00027 
00028 
00029 #include <sys/types.h>
00030 #include <unistd.h>
00031 #include <errno.h>
00032 #include <string>
00033 #include <stdio.h>
00034 #include <sys/socket.h>
00035 #include <netinet/in.h>
00036 #include <netdb.h>
00037 #include <errno.h>
00038 #include <iostream>
00039 
00040 #include "trackerd_position.hh"
00041 
00042 using namespace std;
00043 
00044 TrackerdPosition::TrackerdPosition(const char* host, unsigned int port, unsigned int tid) throw (DeviceError)
00045 {
00046     id = tid;
00047     struct sockaddr_in address;
00048     if( (socket_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
00049         throw DeviceError(string("Error creating socket: ") + strerror(errno));
00050     }
00051     struct hostent* he;
00052     if(!(he = gethostbyname(host))) {
00053         throw DeviceError(string("Invalid address: ") + host);
00054     }
00055     memset(&address, 0, sizeof(address));
00056     address.sin_port=htons(port);
00057     address.sin_family=AF_INET;
00058     address.sin_addr=*((struct in_addr *)he->h_addr);
00059     if( connect(socket_fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
00060         char port_s[16];
00061         snprintf(port_s, sizeof(port_s), "%d", port);
00062         throw DeviceError(string("Error connecting to server ") + host + ":" + port_s + ". " + strerror(errno));
00063     }
00064 }
00065 
00066 TrackerdPosition::~TrackerdPosition()
00067 {
00068     shutdown(socket_fd, 2);
00069 }
00070 
00071 void TrackerdPosition::getPosition(double* x, double* y, double* z)  throw(DeviceError)
00072 {
00073     char msg[256];
00074     unsigned int id = 0;
00075     if( write(socket_fd, "p:0", 4) != 4) {
00076         throw DeviceError( string("Error sending command to trackerd: ") + strerror(errno));
00077     }
00078     int nrecvd = recv(socket_fd, msg, 256, 0);
00079     //cerr << "Debug: got oa from trackerd: \"" << msg << "\"\n";
00080     if(sscanf(msg, "p:%u=%lf,%lf,%lf", &id, x, y, z) < 4) {
00081         throw DeviceError("Misparse of data from trackerd");
00082     }
00083     *x *= xScale;
00084     *y *= yScale;
00085     *z *= zScale;
00086     *x += xOffset;
00087     *y += yOffset;
00088     *z += zOffset;
00089 
00090 }
00091 

Generated on Tue Aug 12 03:55:38 2003 for Interreality Project - VOS by doxygen 1.3.2