Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

LGmixin.cc

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

Tekkotsu v3.0
Generated Wed Oct 4 00:03:44 2006 by Doxygen 1.4.7