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