1 // Sun, 18 Jun 2000 2 // Y.Takagi 3 4 #include <DataIO.h> 5 #include <Message.h> 6 #include <Directory.h> 7 #include <net/netdb.h> 8 #include <Alert.h> 9 #include <stdio.h> 10 #include <unistd.h> 11 #include <iomanip> 12 #include "_sstream" 13 14 #include "LpsClient.h" 15 #include "LprSetupDlg.h" 16 #include "LprTransport.h" 17 #include "LprDefs.h" 18 #include "DbgMsg.h" 19 20 #if (!__MWERKS__ || defined(WIN32)) 21 using namespace std; 22 #else 23 #define std 24 #endif 25 26 LprTransport::LprTransport(BMessage *msg) 27 : BDataIO() 28 { 29 __server[0] = '\0'; 30 __queue[0] = '\0'; 31 __file[0] = '\0'; 32 __user[0] = '\0'; 33 __jobid = 0; 34 __error = false; 35 36 DUMP_BMESSAGE(msg); 37 38 const char *spool_path = msg->FindString(SPOOL_PATH); 39 if (spool_path && *spool_path) { 40 BDirectory dir(spool_path); 41 DUMP_BDIRECTORY(&dir); 42 43 dir.ReadAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, __server, sizeof(__server)); 44 if (__server[0] == '\0') { 45 LprSetupDlg *dlg = new LprSetupDlg(&dir); 46 if (dlg->Go() == B_ERROR) { 47 __error = true; 48 return; 49 } 50 } 51 52 dir.ReadAttr(LPR_SERVER_NAME, B_STRING_TYPE, 0, __server, sizeof(__server)); 53 dir.ReadAttr(LPR_QUEUE_NAME, B_STRING_TYPE, 0, __queue, sizeof(__queue)); 54 dir.ReadAttr(LPR_JOB_ID, B_INT32_TYPE, 0, &__jobid, sizeof(__jobid)); 55 __jobid++; 56 if (__jobid > 255) { 57 __jobid = 1; 58 } 59 dir.WriteAttr(LPR_JOB_ID, B_INT32_TYPE, 0, &__jobid, sizeof(__jobid)); 60 61 getusername(__user, sizeof(__user)); 62 if (__user[0] == '\0') { 63 strcpy(__user, "baron"); 64 } 65 66 sprintf(__file, "%s/%s@ipp.%ld", spool_path, __user, __jobid); 67 68 __fs.open(__file, ios::in | ios::out | ios::binary | ios::trunc); 69 if (__fs.good()) { 70 DBGMSG(("spool_file: %s\n", __file)); 71 return; 72 } 73 } 74 __error = true; 75 } 76 77 LprTransport::~LprTransport() 78 { 79 char hostname[128]; 80 gethostname(hostname, sizeof(hostname)); 81 82 char username[128]; 83 #ifdef WIN32 84 unsigned long length = sizeof(hostname); 85 GetUserName(username, &length); 86 #else 87 getusername(username, sizeof(username)); 88 #endif 89 90 ostringstream cfname; 91 cfname << "cfA" << setw(3) << setfill('0') << __jobid << hostname; 92 93 ostringstream dfname; 94 dfname << "dfA" << setw(3) << setfill('0') << __jobid << hostname; 95 96 ostringstream cf; 97 cf << 'H' << hostname << '\n'; 98 cf << 'P' << username << '\n'; 99 cf << 'l' << dfname.str() << '\n'; 100 cf << 'U' << dfname.str() << '\n'; 101 102 long cfsize = cf.str().length(); 103 long dfsize = __fs.tellg(); 104 __fs.seekg(0, ios::beg); 105 106 try { 107 LpsClient lpr(__server); 108 109 lpr.connect(); 110 lpr.receiveJob(__queue); 111 112 lpr.receiveControlFile(cfsize, cfname.str().c_str()); 113 lpr.transferData(cf.str().c_str(), cfsize); 114 lpr.endTransfer(); 115 116 lpr.receiveDataFile(dfsize, dfname.str().c_str()); 117 lpr.transferData(__fs, dfsize); 118 lpr.endTransfer(); 119 } 120 121 catch (LPSException &err) { 122 DBGMSG(("error: %s\n", err.what())); 123 BAlert *alert = new BAlert("", err.what(), "OK"); 124 alert->Go(); 125 } 126 127 unlink(__file); 128 } 129 130 ssize_t LprTransport::Read(void *, size_t) 131 { 132 return 0; 133 } 134 135 ssize_t LprTransport::Write(const void *buffer, size_t size) 136 { 137 // DBGMSG(("write: %d\n", size)); 138 if (!__fs.write((char *)buffer, size)) { 139 __error = true; 140 return 0; 141 } 142 // return __fs.pcount(); 143 return size; 144 } 145