Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LGmixin.cc

Go to the documentation of this file.
00001 #include "LGmixin.h"
00002 #include "Shared/ProjectInterface.h"
00003 #include "Vision/JPEGGenerator.h"
00004 
00005 #include <iostream>
00006 #include <fstream>
00007 #include <sys/stat.h>
00008 #include <sys/fcntl.h>
00009 #include <unistd.h>
00010 
00011 using namespace std;
00012 
00013 unsigned int LGmixin::instanceCount = 0;
00014 Socket* LGmixin::LGsock = NULL;
00015 LGmixin* LGmixin::theOne = NULL;
00016 
00017 LGmixin::LGmixin() {
00018   if ( instanceCount++ > 0 )
00019     return;
00020   if (theOne != NULL) {
00021       cerr << "LGmixin statics already constructed!?!?!" << endl;
00022       return;
00023     }
00024     theOne=this;
00025 
00026     LGsock = wireless->socket(Socket::SOCK_STREAM, 1024, LGbufferSize);
00027     wireless->setDaemon(LGsock, false);
00028     wireless->listen(LGsock, LGport);
00029 }
00030 
00031 LGmixin::~LGmixin() {
00032   if ( --instanceCount > 0 )
00033     return;
00034   if (theOne == NULL) {
00035     cerr << "LGmixin statics already destructed!?!?!" << endl;
00036     return;
00037   }
00038   wireless->close(LGsock->sock);
00039   theOne = NULL;
00040 }
00041 
00042 void LGmixin::uploadFile(const std::string &filename, bool display, bool isImage) {
00043   if ( !wireless->isConnected(LGsock->sock) ) {
00044     cerr << "LookingGlass not connected." << endl;
00045     return;
00046   }
00047 
00048   int in_file = open(filename.c_str(), O_RDONLY);
00049   if ( in_file < 0 ) {
00050     cerr << "Error: Unable to open file\n";
00051     return;
00052   }
00053   struct stat s;
00054   stat(filename.c_str(),&s);
00055   const std::string remoteFilename = filename.substr(filename.rfind('/')+1);
00056   LGsock->printf("UPLOAD_BINARY %s %u\n", remoteFilename.c_str(), (unsigned int)s.st_size);
00057   
00058   while(1){
00059     char *buffer = (char*)LGsock->getWriteBuffer(s.st_size);
00060     if ( buffer==NULL ) {
00061       cerr << "NULL buffer in LG file upload" << endl;
00062       break;
00063     }
00064     int read_size = read(in_file, buffer, s.st_size);
00065     if( read_size == 0 )
00066       break;
00067     
00068     if ( read_size < 0 ){
00069       cerr << "Error: Read error " << read_size << endl;
00070       break;
00071     }
00072     LGsock->write(read_size);
00073   }
00074   
00075   if(display)
00076     if ( isImage )
00077       displayImageFile(remoteFilename);
00078     else
00079       displayHtmlFile(remoteFilename);
00080 
00081   close(in_file);
00082 }
00083 
00084 void LGmixin::displayHtmlFile(const std::string &remoteFilename) {
00085   LGsock->printf("DISPLAY LookingGlass_Temp_File\n"); // work-around because renderer can't display same file twice
00086   LGsock->printf("DISPLAY %s\n",remoteFilename.c_str());
00087 }
00088 
00089 void LGmixin::displayImageFile(const std::string &remoteFilename) {
00090   LGsock->printf((string("UPLOAD_HTML %s.html\n") +
00091      string("<html><body><table width=100%% height=100%%><tr><td align=center valign=middle>") +
00092       string("<img src=\"%s\"></td></tr></table></body>\n</html>\n")).c_str(),
00093      remoteFilename.c_str(), remoteFilename.c_str());
00094   displayHtmlFile(remoteFilename+".html");
00095 }
00096 
00097 void LGmixin::displayHtmlText(const std::string &text) {
00098   unsigned int const msgSize = text.size();
00099   string const tempfilename = "temp9999";
00100   LGsock->printf("UPLOAD_BINARY %s %u\n", tempfilename.c_str(), msgSize);
00101   char *buffer = (char*)LGsock->getWriteBuffer(msgSize);
00102   memcpy(buffer,text.c_str(),msgSize);
00103   LGsock->write(msgSize);
00104   displayHtmlFile(tempfilename);
00105 }
00106 
00107 void LGmixin::sendCommand(const std::string &command) {
00108   LGsock->printf("%s\n", command.c_str());
00109 }
00110 
00111 void LGmixin::uploadCameraImage(const std::string &remoteFilename) {
00112     JPEGGenerator *jpeg = ProjectInterface::defColorJPEGGenerator;  
00113     // call to getImage must come before call to getImageSize
00114     char *image = (char*)jpeg->getImage(ProjectInterface::fullLayer, 0);
00115     int const imgSize = jpeg->getImageSize(ProjectInterface::fullLayer, 0);
00116     LGsock->printf("UPLOAD_BINARY %s %u\n", remoteFilename.c_str(), imgSize);
00117     char *buffer = (char*)LGsock->getWriteBuffer(imgSize);
00118     if ( buffer==NULL ) {
00119       cerr << "NULL buffer in LG camera upload" << endl;
00120       return;
00121     }
00122     memcpy(buffer,image,imgSize);
00123     LGsock->write(imgSize);
00124 }
00125 
00126 void LGmixin::uploadSketch(const DualCoding::Sketch<DualCoding::uchar> &/*sketch*/,
00127          const std::string &/*remoteFilename*/) {
00128   // this function has not been written yet
00129 }
00130 

Tekkotsu v4.0
Generated Thu Nov 22 00:54:53 2007 by Doxygen 1.5.4