/***************************************************************************
 *   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 joliex.io;


import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.channels.Channels;
import java.nio.channels.ClosedByInterruptException;

import jolie.net.CommMessage;
import jolie.runtime.JavaService;
import jolie.runtime.Value;

public class ConsoleService extends JavaService
{
	private class ConsoleInputThread extends Thread
	{
		private boolean keepRun = true;
		
		public void kill()
		{
			keepRun = false;
			this.interrupt();
		}

		@Override
		public void run()
		{
			BufferedReader stdin =
				new BufferedReader(
						new InputStreamReader(
							Channels.newInputStream(
								(new FileInputStream( FileDescriptor.in )).getChannel() ) ) );
			try {
				String line;
				while( keepRun ) {
					line = stdin.readLine();
					sendMessage( new CommMessage( "in", "/", Value.create( line ) ) );
				}
			} catch( ClosedByInterruptException ce ) {
			} catch( Exception e ) {
				e.printStackTrace();
			}
		}
	}
	
	private ConsoleInputThread consoleInputThread;
	
	public ConsoleService()
	{}
	
	public void registerForInput( CommMessage message )
	{
		consoleInputThread = new ConsoleInputThread();
		consoleInputThread.start();
	}
	
	@Override
	protected void finalize()
	{
		consoleInputThread.kill();
	}
	
	public void print( CommMessage message )
	{
		System.out.print( message.value().strValue() );
	}
	
	public void println( CommMessage message )
	{
		System.out.println( message.value().strValue() );
	}
}

@@ -2643,17 +2643,16 @@
 e()%0A%09%7B%7D%0A
-%09
 %0A%09public
@@ -2652,20 +2652,27 @@
 %09public 
-void
+CommMessage
  registe
@@ -2782,24 +2782,67 @@
 ad.start();%0A
+%09%09return CommMessage.createEmptyMessage();%0A
 %09%7D%0A%09%0A%09@Overr
@@ -2903,25 +2903,24 @@
 .kill();%0A%09%7D%0A
-%09
 %0A%09public voi
@@ -2912,28 +2912,35 @@
 %09%7D%0A%0A%09public 
-void
+CommMessage
  print( Comm
@@ -3015,24 +3015,73 @@
 );%0A%09
-%7D%0A%09%0A%09public void
+%09return CommMessage.createEmptyMessage();%0A%09%7D%0A%0A%09public CommMessage
  pri
@@ -3163,13 +3163,56 @@
 ue() );%0A
+%09%09return CommMessage.createEmptyMessage();%0A
 %09%7D%0A%7D%0A

Switch Console operations to RequestResponses.