2007-02-25 02:43:56 +00:00
|
|
|
/*
|
2007-07-05 23:24:25 +00:00
|
|
|
* $Id: AudioScrobblerClient.m 869 2007-06-18 15:51:33Z stephen_booth $
|
2007-02-25 02:43:56 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2007 Stephen F. Booth <me@sbooth.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a port of the BlockingClient client class from
|
|
|
|
* the Last.fm ScrobSub library by sharevari
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "AudioScrobblerClient.h"
|
|
|
|
|
|
|
|
#define kBufferSize 1024
|
|
|
|
#define kPortsToStep 5
|
|
|
|
|
|
|
|
static in_addr_t
|
|
|
|
addressForHost(NSString *hostname)
|
|
|
|
{
|
|
|
|
NSCParameterAssert(nil != hostname);
|
|
|
|
|
|
|
|
in_addr_t address;
|
|
|
|
struct hostent *hostinfo;
|
|
|
|
|
|
|
|
address = inet_addr([hostname cStringUsingEncoding:NSASCIIStringEncoding]);
|
|
|
|
|
|
|
|
if(INADDR_NONE == address) {
|
|
|
|
hostinfo = gethostbyname([hostname cStringUsingEncoding:NSASCIIStringEncoding]);
|
|
|
|
if(NULL == hostinfo) {
|
2007-07-05 23:24:25 +00:00
|
|
|
NSLog(@"AudioScrobblerClient error: Unable to resolve address for \"%@\".", hostname);
|
2007-02-25 02:43:56 +00:00
|
|
|
return INADDR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
address = *((in_addr_t *)hostinfo->h_addr_list[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface AudioScrobblerClient (Private)
|
2007-07-05 23:24:25 +00:00
|
|
|
- (BOOL) connectToSocket:(in_addr_t)remoteAddress port:(in_port_t)port;
|
2007-02-25 02:43:56 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobblerClient
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if((self = [super init])) {
|
|
|
|
_socket = -1;
|
|
|
|
_doPortStepping = YES;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
- (BOOL) connectToHost:(NSString *)hostname port:(in_port_t)port
|
2007-02-25 02:43:56 +00:00
|
|
|
{
|
2007-07-05 23:24:25 +00:00
|
|
|
NSParameterAssert(nil != hostname);
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
in_addr_t remoteAddress = addressForHost(hostname);
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(INADDR_NONE != remoteAddress)
|
|
|
|
return [self connectToSocket:remoteAddress port:port];
|
|
|
|
|
|
|
|
return NO;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
- (BOOL) isConnected
|
2007-02-25 02:43:56 +00:00
|
|
|
{
|
2007-07-05 23:24:25 +00:00
|
|
|
return (-1 != _socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (in_port_t) connectedPort
|
|
|
|
{
|
|
|
|
return _port;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) send:(NSString *)data
|
|
|
|
{
|
2007-02-25 02:43:56 +00:00
|
|
|
const char *utf8data = [data UTF8String];
|
|
|
|
unsigned len = strlen(utf8data);
|
|
|
|
unsigned bytesToSend = len;
|
|
|
|
unsigned totalBytesSent = 0;
|
|
|
|
ssize_t bytesSent = 0;
|
2007-07-05 23:24:25 +00:00
|
|
|
|
|
|
|
if(NO == [self isConnected]) {
|
|
|
|
NSLog(@"AudioScrobblerClient error: Can't send data, client not connected");
|
|
|
|
return;
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
while(totalBytesSent < bytesToSend && -1 != bytesSent) {
|
|
|
|
bytesSent = send(_socket, utf8data + totalBytesSent, bytesToSend - totalBytesSent, 0);
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesSent || 0 == bytesSent)
|
|
|
|
NSLog(@"AudioScrobblerClient error: Unable to send data through socket: %s", strerror(errno));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
totalBytesSent += bytesSent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) receive
|
|
|
|
{
|
|
|
|
char buffer [ kBufferSize ];
|
|
|
|
int readSize = kBufferSize - 1;
|
|
|
|
ssize_t bytesRead = 0;
|
|
|
|
BOOL keepGoing = YES;
|
|
|
|
NSString *result = nil;
|
2007-07-05 23:24:25 +00:00
|
|
|
|
|
|
|
if(NO == [self isConnected]) {
|
|
|
|
NSLog(@"AudioScrobblerClient error: Can't receive data, client not connected");
|
|
|
|
return nil;
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
bytesRead = recv(_socket, buffer, readSize, 0);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesRead || 0 == bytesRead) {
|
|
|
|
NSLog(@"AudioScrobblerClient error: Unable to receive data through socket: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
if('\n' == buffer[bytesRead - 1]) {
|
|
|
|
--bytesRead;
|
|
|
|
keepGoing = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[bytesRead] = '\0';
|
2008-08-26 17:45:16 +00:00
|
|
|
result = [[[NSString alloc] autorelease] initWithUTF8String:buffer];
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
} while(keepGoing);
|
|
|
|
|
2008-08-26 17:45:16 +00:00
|
|
|
return result;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) shutdown
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
char buffer [ kBufferSize ];
|
|
|
|
ssize_t bytesRead;
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
if(NO == [self isConnected]) {
|
2007-02-25 02:43:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = shutdown(_socket, SHUT_WR);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == result)
|
|
|
|
NSLog(@"AudioScrobblerClient error: Socket shutdown failed: %s", strerror(errno));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
bytesRead = recv(_socket, buffer, kBufferSize, 0);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == bytesRead)
|
|
|
|
NSLog(@"AudioScrobblerClient error: Waiting for shutdown confirmation failed: %s", strerror(errno));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
|
|
|
if(0 != bytesRead) {
|
2007-07-05 23:24:25 +00:00
|
|
|
NSString *received = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
|
|
|
|
NSLog(@"Received unexpected bytes during shutdown: %@", received);
|
|
|
|
[received release];
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
2007-07-05 23:24:25 +00:00
|
|
|
else
|
2007-02-25 02:43:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = close(_socket);
|
2007-07-05 23:24:25 +00:00
|
|
|
if(-1 == result)
|
|
|
|
NSLog(@"Couldn't close socket (%s)", strerror(errno));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
_socket = -1;
|
|
|
|
_port = 0;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AudioScrobblerClient (Private)
|
|
|
|
|
2007-07-05 23:24:25 +00:00
|
|
|
- (BOOL) connectToSocket:(in_addr_t)remoteAddress port:(in_port_t)port
|
2007-02-25 02:43:56 +00:00
|
|
|
{
|
2007-07-05 23:24:25 +00:00
|
|
|
NSParameterAssert(INADDR_NONE != remoteAddress);
|
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_port = port;
|
|
|
|
|
|
|
|
int result;
|
|
|
|
do {
|
|
|
|
struct sockaddr_in socketAddress;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(-1 == _socket) {
|
|
|
|
NSLog(@"Unable to create socket (%s)", strerror(errno));
|
|
|
|
return NO;
|
|
|
|
}
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
socketAddress.sin_family = AF_INET;
|
|
|
|
socketAddress.sin_addr.s_addr = remoteAddress;
|
|
|
|
socketAddress.sin_port = htons(_port);
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
result = connect(_socket, (const struct sockaddr *)&socketAddress, sizeof(struct sockaddr_in));
|
|
|
|
if(-1 == result) {
|
|
|
|
close(_socket);
|
|
|
|
_socket = -1;
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
_port++;
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
2009-03-28 03:00:45 +00:00
|
|
|
} while (YES == _doPortStepping && -1 == result && _port < (port + kPortsToStep));
|
2007-02-25 02:43:56 +00:00
|
|
|
|
2009-03-28 03:00:45 +00:00
|
|
|
return (-1 != result);
|
2007-02-25 02:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|