1 /* 2 * Copyright 2001-2010 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Philippe Houdoin, 7 */ 8 9 10 #include "HPJetDirectTransport.h" 11 12 #include <stdio.h> 13 14 #include <Alert.h> 15 #include <Message.h> 16 #include <Directory.h> 17 #include <SupportKit.h> 18 #include <String.h> 19 #include <NetEndpoint.h> 20 21 #include "SetupWindow.h" 22 23 24 HPJetDirectPort::HPJetDirectPort(BDirectory* printer, BMessage *msg) 25 : fHost(""), 26 fPort(9100), 27 fEndpoint(NULL), 28 fReady(B_ERROR) 29 { 30 BString address; 31 32 if (printer->ReadAttrString("transport_address", &address) < 0 33 || address.Length() == 0) { 34 SetupWindow *setup = new SetupWindow(printer); 35 if (setup->Go() == B_ERROR) 36 return; 37 } 38 39 if (printer->ReadAttrString("transport_address", &address) < 0) 40 return; 41 42 printf("address = %s\n", address.String()); 43 44 int32 index = address.FindLast(':'); 45 if (index >= 0) { 46 fPort = atoi(address.String() + index + 1); 47 address.MoveInto(fHost, 0, index); 48 } else 49 fHost = address; 50 51 printf("fHost = %s\n", fHost.String()); 52 printf("fPort = %d\n", fPort); 53 54 55 fEndpoint = new BNetEndpoint(SOCK_STREAM); 56 if ((fReady = fEndpoint->InitCheck()) != B_OK) { 57 BAlert *alert = new BAlert("", "Fail to create the NetEndpoint!", "OK"); 58 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 59 alert->Go(); 60 return; 61 } 62 63 if (fEndpoint->Connect(fHost, fPort) == B_OK) { 64 printf("Connected to HP JetDirect printer port at %s:%d\n", 65 fHost.String(), fPort); 66 fReady = B_OK; 67 } else { 68 BAlert *alert = new BAlert("", 69 "Can't connect to HP JetDirect printer port!", "OK"); 70 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 71 alert->Go(); 72 fReady = B_ERROR; 73 } 74 } 75 76 77 HPJetDirectPort::~HPJetDirectPort() 78 { 79 if (fEndpoint) { 80 shutdown(fEndpoint->Socket(), SHUT_WR); 81 fEndpoint->Close(); 82 } 83 delete fEndpoint; 84 } 85 86 87 ssize_t 88 HPJetDirectPort::Read(void* buffer, size_t size) 89 { 90 // printf("HPJetDirectPort::Read(%ld bytes)\n", size); 91 if (fEndpoint) 92 return (ssize_t) fEndpoint->Receive(buffer, size); 93 return 0; 94 } 95 96 97 ssize_t 98 HPJetDirectPort::Write(const void* buffer, size_t size) 99 { 100 // printf("HPJetDirectPort::Write(%ld bytes)\n", size); 101 if (fEndpoint) 102 return (ssize_t) fEndpoint->Send(buffer, size); 103 return 0; 104 } 105 106