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 24HPJetDirectPort::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(std::nothrow) BNetEndpoint(SOCK_STREAM); 56 if (fEndpoint == NULL || (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 delete fEndpoint; 61 fEndpoint = NULL; 62 return; 63 } 64 65 if (fEndpoint->Connect(fHost, fPort) == B_OK) { 66 printf("Connected to HP JetDirect printer port at %s:%d\n", 67 fHost.String(), fPort); 68 fReady = B_OK; 69 } else { 70 BAlert *alert = new BAlert("", 71 "Can't connect to HP JetDirect printer port!", "OK"); 72 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 73 alert->Go(); 74 fReady = B_ERROR; 75 } 76} 77 78 79HPJetDirectPort::~HPJetDirectPort() 80{ 81 if (fEndpoint) { 82 shutdown(fEndpoint->Socket(), SHUT_WR); 83 fEndpoint->Close(); 84 } 85 delete fEndpoint; 86} 87 88 89ssize_t 90HPJetDirectPort::Read(void* buffer, size_t size) 91{ 92 // printf("HPJetDirectPort::Read(%ld bytes)\n", size); 93 if (fEndpoint) 94 return (ssize_t) fEndpoint->Receive(buffer, size); 95 return 0; 96} 97 98 99ssize_t 100HPJetDirectPort::Write(const void* buffer, size_t size) 101{ 102 // printf("HPJetDirectPort::Write(%ld bytes)\n", size); 103 if (fEndpoint) 104 return (ssize_t) fEndpoint->Send(buffer, size); 105 return 0; 106} 107 108