/***************************************************************************
 *   Copyright (C) 2008-2009 by Fabrizio Montesi <famontesi@gmail.com>     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library 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 Library 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.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.net;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import javax.bluetooth.L2CAPConnection;
import jolie.Interpreter;
import jolie.net.protocols.CommProtocol;

public class BTL2CapCommChannel extends StreamingCommChannel implements PollableCommChannel
{
	private final L2CAPConnection connection;
	private final int sendMTU;
	private final int recvMTU;
	
	public BTL2CapCommChannel( L2CAPConnection connection, URI location, CommProtocol protocol )
		throws IOException
	{
		super( location, protocol );
		this.connection = connection;
		sendMTU = connection.getTransmitMTU();
		recvMTU = connection.getReceiveMTU();
		setToBeClosed( false ); // Bluetooth connections are kept open by default.
	}
	
	protected void sendImpl( CommMessage message )
		throws IOException
	{
		ByteArrayOutputStream ostream = new ByteArrayOutputStream();
		protocol().send( ostream, message, null ); // TODO Fix this null pointer.
		byte[] result = ostream.toByteArray();
		if ( result.length >= sendMTU ) {
			int times = (result.length / sendMTU) + 1;
			byte[] chunk;
			for( int i = 0; i < times; i++ ) {
				chunk = Arrays.copyOfRange( result, i*sendMTU, (i*sendMTU)+1 );
				connection.send( chunk );
			}
		} else {
			connection.send( result );
		}
	}
	
	protected CommMessage recvImpl()
		throws IOException
	{
		ByteArrayOutputStream ostream = new ByteArrayOutputStream();
		byte[] chunk;
		int len = recvMTU + 1;
		while( len >= recvMTU ) {
			chunk = new byte[ recvMTU ];
			len = connection.receive( chunk );
			ostream.write( chunk );
		}
		ByteArrayInputStream istream = new ByteArrayInputStream( ostream.toByteArray() );
		return protocol().recv( istream, null ); // TODO fix this null pointer
	}
	
	protected void closeImpl()
		throws IOException
	{
		connection.close();
	}
	
	@Override
	protected void disposeForInputImpl()
		throws IOException
	{
		Interpreter.getInstance().commCore().registerForPolling( this );
	}
	
	public boolean isReady()
	{
		try {
			return connection.ready();
		} catch( IOException e ) {
			return false;
		}
	}
}

@@ -2888,19 +2888,469 @@
  );%0A%09%09%7D%0A
+%09%09if ( result.length %3E= sendMTU ) %7B%0A%09%09%09int times = (result.length / sendMTU);%0A%09%09%09byte%5B%5D chunk;%0A%09%09%09int i;%0A%09%09%09for( i = 0; i %3C times; i++ ) %7B%0A%09%09%09%09chunk = Arrays.copyOfRange( result, i*sendMTU, ((i+1)*sendMTU) - 1 );%0A%09%09%09%09connection.send( chunk );%0A%09%09%09%7D%0A%09%09%09int remaining = result.length %25 sendMTU;%0A%09%09%09if ( remaining %3E 0 ) %7B%0A%09%09%09%09chunk = Arrays.copyOfRange( result, i*sendMTU, (i*sendMTU) + remaining - 1 );%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09connection.send( result );%0A%09%09%7D%0A
 %09%7D%0A
-
 %09%0A%09prote
@@ -3499,20 +3499,16 @@
  recvMTU
- + 1
 ;%0A%09%09whil
@@ -3599,16 +3599,16 @@
 hunk );%0A
-
 %09%09%09ostre
@@ -3618,24 +3618,32 @@
 write( chunk
+, 0, len
  );%0A%09%09%7D%0A%09%09By

Fix for correct MTU handling.