<commit_before>/***************************************************************************
 *   Copyright (C) by <NAME>                                     *
 *                                                                         *
 *   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.protocols;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jolie.net.CommChannel;
import jolie.net.CommMessage;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;
import jolie.runtime.VariablePath;

/**
 * A CommProtocol implements a protocol for sending and receiving data under the form of CommMessage objects.
 * @author <NAME>
 */
abstract public class CommProtocol
{
	final private VariablePath configurationPath;
	private CommChannel channel = null;

	protected VariablePath configurationPath()
	{
		return configurationPath;
	}

	abstract public String name();
	
	public CommProtocol( VariablePath configurationPath )
	{
		this.configurationPath = configurationPath;
	}
	
	public void setChannel( CommChannel channel )
	{
		this.channel = channel;
	}

	protected CommChannel channel()
	{
		return this.channel;
	}
	
	protected ValueVector getParameterVector( String id )
	{
		return configurationPath.getValue().getChildren( id );
	}
	
	protected boolean hasParameter( String id )
	{
		return configurationPath.getValue().hasChildren( id );
	}
	
	/**
	 * Shortcut for getParameterVector( id ).first()<commit_msg>Use a dummy channel if unavailable.<commit_after>/***************************************************************************
 *   Copyright (C) by <NAME>                                     *
 *                                                                         *
 *   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.protocols;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jolie.net.CommChannel;
import jolie.net.CommMessage;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;
import jolie.runtime.VariablePath;

/**
 * A CommProtocol implements a protocol for sending and receiving data under the form of CommMessage objects.
 * @author <NAME>
 */
abstract public class CommProtocol
{
	private static class LazyDummyChannelHolder {
		private static class DummyChannel extends CommChannel {
			public void closeImpl() {}
			public void sendImpl( CommMessage message ) {}
			public CommMessage recvImpl() { return CommMessage.createEmptyMessage(); }
		}

		private static DummyChannel dummyChannel = new DummyChannel();
	}


	final private VariablePath configurationPath;
	private CommChannel channel = null;

	protected VariablePath configurationPath()
	{
		return configurationPath;
	}

	abstract public String name();
	
	public CommProtocol( VariablePath configurationPath )
	{
		this.configurationPath = configurationPath;
	}
	
	public void setChannel( CommChannel channel )
	{
		this.channel = channel;
	}

	protected CommChannel channel()
	{
		if ( this.channel == null ) {
			return LazyDummyChannelHolder.dummyChannel;
		}
		return this.channel;
	}
	
	protected ValueVector getParameterVector( String id )
	{
		return configurationPath.getValue().getChildren( id );
	}
	
	protected boolean hasParameter( String id )
	{
		return configurationPath.getValue().hasChildren( id );
	}
	
	/**
	 * Shortcut for getParameterVector( id ).first()
{'fraction_non_alphanumeric': 0.11371688881390248, 'fraction_numerical': 0.005061582588155897, 'mean_word_length': 2.1036649214659686, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363220', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1298, 'n_words': 494}