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
55ee1265c5SPhilippe Saint-Pierre	fEndpoint = new(std::nothrow) BNetEndpoint(SOCK_STREAM);
56ee1265c5SPhilippe Saint-Pierre	if (fEndpoint == NULL || (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();
60ee1265c5SPhilippe Saint-Pierre		delete fEndpoint;
61ee1265c5SPhilippe Saint-Pierre		fEndpoint = NULL;
62fd17f6d4SPhilippe Houdoin		return;
634339f8beSMichael Pfeiffer	}
64b7d73f97SPhilippe Houdoin
65fd17f6d4SPhilippe Houdoin	if (fEndpoint->Connect(fHost, fPort) == B_OK) {
66b7d73f97SPhilippe Houdoin		printf("Connected to HP JetDirect printer port at %s:%d\n",
67b7d73f97SPhilippe Houdoin			fHost.String(), fPort);
68179268dcSPhilippe Houdoin		fReady = B_OK;
69fd17f6d4SPhilippe Houdoin	} else {
70b7d73f97SPhilippe Houdoin		BAlert *alert = new BAlert("",
71b7d73f97SPhilippe Houdoin			"Can't connect to HP JetDirect printer port!", "OK");
72aed35104SHumdinger		alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
73fd17f6d4SPhilippe Houdoin		alert->Go();
7413dfd6ccSMichael Pfeiffer		fReady = B_ERROR;
754339f8beSMichael Pfeiffer	}
76fd17f6d4SPhilippe Houdoin}
77fd17f6d4SPhilippe Houdoin
78fd17f6d4SPhilippe Houdoin
79fd17f6d4SPhilippe HoudoinHPJetDirectPort::~HPJetDirectPort()
80fd17f6d4SPhilippe Houdoin{
81b7d73f97SPhilippe Houdoin	if (fEndpoint) {
82b7d73f97SPhilippe Houdoin		shutdown(fEndpoint->Socket(), SHUT_WR);
8313dfd6ccSMichael Pfeiffer		fEndpoint->Close();
84b7d73f97SPhilippe Houdoin	}
85fd17f6d4SPhilippe Houdoin	delete fEndpoint;
86fd17f6d4SPhilippe Houdoin}
87fd17f6d4SPhilippe Houdoin
88fd17f6d4SPhilippe Houdoin
89fd17f6d4SPhilippe Houdoinssize_t
90fd17f6d4SPhilippe HoudoinHPJetDirectPort::Read(void* buffer, size_t size)
91fd17f6d4SPhilippe Houdoin{
92fd17f6d4SPhilippe Houdoin	// printf("HPJetDirectPort::Read(%ld bytes)\n", size);
9313dfd6ccSMichael Pfeiffer	if (fEndpoint)
9413dfd6ccSMichael Pfeiffer		return (ssize_t) fEndpoint->Receive(buffer, size);
9513dfd6ccSMichael Pfeiffer	return 0;
96fd17f6d4SPhilippe Houdoin}
97fd17f6d4SPhilippe Houdoin
98fd17f6d4SPhilippe Houdoin
99fd17f6d4SPhilippe Houdoinssize_t
100fd17f6d4SPhilippe HoudoinHPJetDirectPort::Write(const void* buffer, size_t size)
101fd17f6d4SPhilippe Houdoin{
102fd17f6d4SPhilippe Houdoin	// printf("HPJetDirectPort::Write(%ld bytes)\n", size);
10313dfd6ccSMichael Pfeiffer	if (fEndpoint)
10413dfd6ccSMichael Pfeiffer		return (ssize_t) fEndpoint->Send(buffer, size);
10513dfd6ccSMichael Pfeiffer	return 0;
106fd17f6d4SPhilippe Houdoin}
107fd17f6d4SPhilippe Houdoin
108