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 SetupWindow *setup = new SetupWindow(printer); 34 if (setup->Go() == B_ERROR) 35 return; 36 } 37 38 if (printer->ReadAttrString("transport_address", &address) < 0) 39 return; 40 41 printf("address = %s\n", address.String()); 42 43 int32 index = address.FindLast(':'); 44 if (index >= 0) { 45 fPort = atoi(address.String() + index); 46 address.MoveInto(fHost, 0, index); 47 } else 48 fHost = address; 49 50 printf("fHost = %s\n", fHost.String()); 51 printf("fPort = %d\n", fPort); 52 53 54 fEndpoint = new BNetEndpoint(SOCK_STREAM); 55 if ((fReady = fEndpoint->InitCheck()) != B_OK) { 56 BAlert *alert = new BAlert("", "Fail to create the NetEndpoint!", "OK"); 57 alert->Go(); 58 return; 59 } 60 61 if (fEndpoint->Connect(fHost, fPort) == B_OK) { 62 printf("Connected to HP JetDirect printer port at %s:%d\n", 63 fHost.String(), fPort); 64 fReady = B_OK; 65 } else { 66 BAlert *alert = new BAlert("", 67 "Can't connect to HP JetDirect printer port!", "OK"); 68 alert->Go(); 69 fReady = B_ERROR; 70 } 71 } 72 73 74 HPJetDirectPort::~HPJetDirectPort() 75 { 76 if (fEndpoint) { 77 shutdown(fEndpoint->Socket(), SHUT_WR); 78 fEndpoint->Close(); 79 } 80 delete fEndpoint; 81 } 82 83 84 ssize_t 85 HPJetDirectPort::Read(void* buffer, size_t size) 86 { 87 // printf("HPJetDirectPort::Read(%ld bytes)\n", size); 88 if (fEndpoint) 89 return (ssize_t) fEndpoint->Receive(buffer, size); 90 return 0; 91 } 92 93 94 ssize_t 95 HPJetDirectPort::Write(const void* buffer, size_t size) 96 { 97 // printf("HPJetDirectPort::Write(%ld bytes)\n", size); 98 if (fEndpoint) 99 return (ssize_t) fEndpoint->Send(buffer, size); 100 return 0; 101 } 102 103