/***************************************************************************
 *   Copyright (C) by Fabrizio Montesi                                     *
 *                                                                         *
 *   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 cx.ath.matthew.unix.UnixSocket;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import jolie.Interpreter;
import jolie.net.protocols.CommProtocol;

public class LocalSocketCommChannel extends StreamingCommChannel implements PollableCommChannel
{
	private final UnixSocket socket;
	private PreBufferedInputStream bufferedInputStream;
	private final InputStream socketInputStream;
	
	public LocalSocketCommChannel( UnixSocket socket, URI location, CommProtocol protocol )
		throws IOException
	{
		super( location, protocol );
		
		this.socket = socket;
		this.socketInputStream = socket.getInputStream();
		this.bufferedInputStream = new PreBufferedInputStream( socketInputStream );
		
		setToBeClosed( false ); // LocalSocket connections are kept open by default
	}

	protected void sendImpl( CommMessage message )
		throws IOException
	{
		protocol().send( socket.getOutputStream(), message, bufferedInputStream );
	}
	
	protected CommMessage recvImpl()
		throws IOException
	{
		return protocol().recv( bufferedInputStream, socket.getOutputStream() );
	}
	
	protected void closeImpl()
		throws IOException
	{
		socket.close();
	}
	
	public synchronized boolean isReady()
		throws IOException
	{
		boolean ret = false;
		byte[] r = new byte[1];

		if ( socketInputStream.read( r ) > 0 ) {
			bufferedInputStream.append( r[0] );
			ret = true;
		}

		return ret;
	}
	
	@Override
	public void disposeForInputImpl()
		throws IOException
	{
		Interpreter.getInstance().commCore().registerForPolling( this );
	}
}

@@ -1650,16 +1650,45 @@
 Stream;%0A
+import java.io.OutputStream;%0A
 import j
@@ -1909,16 +1909,22 @@
 private 
+final 
 PreBuffe
@@ -2005,16 +2005,64 @@
 Stream;%0A
+%09private final OutputStream socketOutputStream;%0A
 %09%0A%09publi
@@ -2278,16 +2278,70 @@
 ream();%0A
+%09%09this.socketOutputStream = socket.getOutputStream();%0A
 %09%09this.b
@@ -2588,28 +2588,24 @@
 send( socket
-.get
 OutputStream
@@ -2604,18 +2604,16 @@
 utStream
-()
 , messag
@@ -2634,24 +2634,54 @@
 utStream );%0A
+%09%09socketOutputStream.flush();%0A
 %09%7D%0A%09%0A%09protec
@@ -2780,28 +2780,24 @@
 ream, socket
-.get
 OutputStream
@@ -2796,18 +2796,16 @@
 utStream
-()
  );%0A%09%7D%0A%09
@@ -2878,17 +2878,16 @@
 e();%0A%09%7D%0A
-%09
 %0A%09public
@@ -2965,16 +2965,91 @@
  false;%0A
+%0A%09%09if ( bufferedInputStream.hasCachedData() ) %7B%0A%09%09%09ret = true;%0A%09%09%7D else %7B%0A%09
 %09%09byte%5B%5D
@@ -3066,17 +3066,17 @@
 yte%5B1%5D;%0A
-%0A
+%09
 %09%09if ( s
@@ -3113,16 +3113,17 @@
  ) %7B%0A%09%09%09
+%09
 buffered
@@ -3150,16 +3150,17 @@
 r%5B0%5D );%0A
+%09
 %09%09%09ret =
@@ -3166,16 +3166,21 @@
 = true;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09r

Use the shared prebuffered socket from jolie.