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