<commit_before>/**
 * Copyright (C) 2009 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

namespace FsoGsm
{
    public const int MUX_TRANSPORT_MAX_BUFFER = 1024;
}

//===========================================================================
public class FsoGsm.LibGsm0710muxTransport : FsoFramework.BaseTransport
//===========================================================================
{
    static Gsm0710mux.Manager manager;
    private Gsm0710mux.ChannelInfo channelinfo;
    private FsoFramework.DelegateTransport tdelegate;

    private char[] buffer;
    private int length;

    static construct
    {
        manager = new Gsm0710mux.Manager();
    }

    public LibGsm0710muxTransport( int channel = 0 )
    {
        base( "LibGsm0710muxTransport" );

        buffer = new char[1024];
        length = 0;

        var version = manager.getVersion();
        var hasAutoSession = manager.hasAutoSession();
        assert( hasAutoSession ); // we do not support non-autosession yet

        tdelegate = new FsoFramework.DelegateTransport(
                                                      delegateWrite,
                                                      delegateRead,
                                                      delegateHup,
                                                      delegateOpen,
                                                      delegateClose,
                                                      delegateFreeze,
                                                      delegateThaw );

        channelinfo.tspec = FsoFramework.TransportSpec( "foo", "bar" );
        channelinfo.tspec.transport = tdelegate;
        channelinfo.number = channel;
        channelinfo.consumer = "fsogsmd";

        assert( logger.debug( @"Created. Using libgsm0710mux version $version; autosession is $hasAutoSession" ) );
    }

    public override bool open()
    {
        try
        {
            manager.allocChannel( ref channelinfo );
        }
        catch ( Gsm0710mux.MuxerError e )
        {
            logger.error( @"Can't open allocate channel from MUX: $(e.message)" );
            return false;
        }
        return true;
    }

    public override int read( void* data, int length )
    {
        assert( this.length > 0 );
        assert( this.length < length );
        GLib.Memory.copy( data, this.buffer, this.length );
        message( @"READ %d from MUX: %s", length, ((string)data).escape( "" ) );
        var l = this.length;
        this.length = 0;
        return l;
    }

    public override int write( void* data, int length )
    {
        assert( this.length == 0 ); // NOT REENTRANT!
        assert( length < MUX_TRANSPORT_MAX_BUFFER );
        message( @"WRITE %d to MUX: %s", length, ((string)data).escape( "" ) );
        this.length = length;
        GLib.Memory.copy( this.buffer, data, length );
        tdelegate.readfunc( tdelegate );
        assert( this.length == 0 ); // everything has been consumed
        return length;
    }

    public override void freeze()
    {
    }

    public override void thaw()
    {
    }

    public override string repr()
    {
        return "<LibGsm0710muxFsoFramework.Transport>";
    }

    //
    // delegate transport interface
    //
    public bool delegateOpen( FsoFramework.Transport t )
    {
        message( "FROM MODEM OPEN ACK" );
        return true;
    }

    public void delegateClose( FsoFramework.Transport t )
    {
        message( "FROM MODEM CLOSE REQ" );
    }

    public int delegateWrite( void* data, int length, FsoFramework.Transport t )
    {
        assert( this.length == 0 );
        message( "FROM MODEM WRITE %d bytes", length );
        assert( length < MUX_TRANSPORT_MAX_BUFFER );
        GLib.Memory.copy( this.buffer, data, length ); // prepare data
        this.length = length;<commit_msg>fsogsmd: 0710transport: add virtual channel number in repr<commit_after>/**
 * Copyright (C) 2009 Michael 'Mickey' Lauer <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

using GLib;

namespace FsoGsm
{
    public const int MUX_TRANSPORT_MAX_BUFFER = 1024;
}

//===========================================================================
public class FsoGsm.LibGsm0710muxTransport : FsoFramework.BaseTransport
//===========================================================================
{
    static Gsm0710mux.Manager manager;
    private Gsm0710mux.ChannelInfo channelinfo;
    private FsoFramework.DelegateTransport tdelegate;

    private char[] buffer;
    private int length;

    static construct
    {
        manager = new Gsm0710mux.Manager();
    }

    public LibGsm0710muxTransport( int channel = 0 )
    {
        base( "LibGsm0710muxTransport" );

        buffer = new char[1024];
        length = 0;

        var version = manager.getVersion();
        var hasAutoSession = manager.hasAutoSession();
        assert( hasAutoSession ); // we do not support non-autosession yet

        tdelegate = new FsoFramework.DelegateTransport(
                                                      delegateWrite,
                                                      delegateRead,
                                                      delegateHup,
                                                      delegateOpen,
                                                      delegateClose,
                                                      delegateFreeze,
                                                      delegateThaw );

        channelinfo.tspec = FsoFramework.TransportSpec( "foo", "bar" );
        channelinfo.tspec.transport = tdelegate;
        channelinfo.number = channel;
        channelinfo.consumer = "fsogsmd";

        assert( logger.debug( @"Created. Using libgsm0710mux version $version; autosession is $hasAutoSession" ) );
    }

    public override bool open()
    {
        try
        {
            manager.allocChannel( ref channelinfo );
        }
        catch ( Gsm0710mux.MuxerError e )
        {
            logger.error( @"Can't open allocate channel from MUX: $(e.message)" );
            return false;
        }
        return true;
    }

    public override int read( void* data, int length )
    {
        assert( this.length > 0 );
        assert( this.length < length );
        GLib.Memory.copy( data, this.buffer, this.length );
        message( @"READ %d from MUX: %s", length, ((string)data).escape( "" ) );
        var l = this.length;
        this.length = 0;
        return l;
    }

    public override int write( void* data, int length )
    {
        assert( this.length == 0 ); // NOT REENTRANT!
        assert( length < MUX_TRANSPORT_MAX_BUFFER );
        message( @"WRITE %d to MUX: %s", length, ((string)data).escape( "" ) );
        this.length = length;
        GLib.Memory.copy( this.buffer, data, length );
        tdelegate.readfunc( tdelegate );
        assert( this.length == 0 ); // everything has been consumed
        return length;
    }

    public override void freeze()
    {
    }

    public override void thaw()
    {
    }

    public override string repr()
    {
        return @"<gsm0710mux:$(channelinfo.number)>";
    }

    //
    // delegate transport interface
    //
    public bool delegateOpen( FsoFramework.Transport t )
    {
        message( "FROM MODEM OPEN ACK" );
        return true;
    }

    public void delegateClose( FsoFramework.Transport t )
    {
        message( "FROM MODEM CLOSE REQ" );
    }

    public int delegateWrite( void* data, int length, FsoFramework.Transport t )
    {
        assert( this.length == 0 );
        message( "FROM MODEM WRITE %d bytes", length );
        assert( length < MUX_TRANSPORT_MAX_BUFFER );
        GLib.Memory.copy( this.buffer, data, length ); // prepare data
        this.length = length;
{'fraction_non_alphanumeric': 0.11713078340015169, 'fraction_numerical': 0.015169574168382273, 'mean_word_length': 2.0006501950585176, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '3805034', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2289, 'n_words': 764}