Warning: Can't synchronize with repository "(default)" (No changeset 96d22ec3fa3ef6de3ea8dc0d7d398adc9aa071cf in the repository). Look in the Trac log for more information.

source: libfsotransport/fsotransport/transport.vala @ 2e0b54c

Revision 2e0b54c, 7.9 KB checked in by Simon Busch <morphis@…>, 14 months ago (diff)

libfsotransport: let the TransportSpec? class act as a factory for a transport

  • Property mode set to 100644
Line 
1/*
2 * Copyright (C) 2009-2011 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17 *
18 */
19
20//===========================================================================
21public errordomain FsoFramework.TransportError
22{
23    UNABLE_TO_OPEN,
24    UNABLE_TO_WRITE,
25}
26
27//===========================================================================
28public enum FsoFramework.TransportState
29{
30    CLOSED,
31    OPEN,
32    ALIVE,
33    FROZEN,
34    DEAD,
35}
36
37public class FsoFramework.TransportSpec
38{
39    public TransportSpec( string type, string name = "", uint speed = 0, bool raw = true, bool hard = true )
40    {
41        this.type = type;
42        this.name = name;
43        this.speed = speed;
44        this.raw = raw;
45        this.hard = hard;
46    }
47
48    public static TransportSpec parse( string line )
49    {
50        string[] tokens = { };
51        string current_token = "";
52        int depth = 0;
53        string type = "", name = "";
54        int speed = 0;
55
56        for ( int n = 0; n < line.length; n++ )
57        {
58            if ( line[n] == ':' && depth == 0 )
59            {
60                tokens += current_token;
61                current_token = "";
62                continue;
63            }
64
65            if ( line[n] == '[' )
66            {
67                depth++;
68                continue;
69            }
70            else if ( line[n] == ']' )
71            {
72                depth--;
73                continue;
74            }
75
76            current_token += @"$(line[n])";
77        }
78
79        if ( current_token.length > 0 )
80            tokens += current_token;
81
82        if ( tokens.length == 3 )
83        {
84            type = tokens[0];
85            name = tokens[1];
86            speed = int.parse(tokens[2]);
87        }
88        else if ( tokens.length == 2 )
89        {
90            type = tokens[0];
91            name = tokens[1];
92        }
93
94        return new TransportSpec( type, name, speed );
95    }
96
97    public FsoFramework.Transport create()
98    {
99        FsoFramework.Transport transport = null;
100
101        switch ( type )
102        {
103            case "serial":
104                transport = new FsoFramework.SerialTransport( name, speed, raw, hard );
105                break;
106            case "pty":
107                transport = new FsoFramework.PtyTransport();
108                break;
109            case "unix":
110            case "udp":
111            case "tcp":
112                transport = new FsoFramework.SocketTransport( type, name, speed );
113                break;
114            case "null":
115                transport = new FsoFramework.NullTransport();
116                break;
117            case "raw":
118                transport = new FsoFramework.RawTransport( name );
119                break;
120            default:
121                FsoFramework.theLogger.warning( @"Invalid transport type $type. Using NullTransport" );
122                transport = new FsoFramework.NullTransport();
123                break;
124        }
125
126        return transport;
127    }
128
129    public string type;
130    public string name;
131    public uint speed;
132    public bool raw;
133    public bool hard;
134}
135
136//===========================================================================
137public abstract class FsoFramework.Transport : Object
138{
139    /**
140     * Create @a FsoFramework.Transport as indicated by @a type
141     **/
142    public static Transport? create( string type, string name = "", uint speed = 0, bool raw = true, bool hard = true )
143    {
144        switch ( type )
145        {
146            case "serial":
147                return new FsoFramework.SerialTransport( name, speed, raw, hard );
148            case "pty":
149                return new FsoFramework.PtyTransport();
150            case "unix":
151            case "udp":
152            case "tcp":
153                return new FsoFramework.SocketTransport( type, name, speed );
154            case "ngsmbasic":
155                return new FsoFramework.NgsmBasicMuxTransport( name, speed );
156            case "ngsmadvanced":
157                return new FsoFramework.NgsmAdvancedMuxTransport( name, speed );
158            case "combined":
159                return new FsoFramework.CombinedTransport( name );
160            default:
161                return null;
162        }
163    }
164    /**
165     * @returns true, if the @a transport is open; else false.
166     */
167    public abstract bool isOpen();
168    /**
169     * Open the transport asynchronously. @returns true, if successful; else false.
170     */
171    public abstract async bool openAsync();
172    /**
173     * Close the transport. Closing an already closed transport is allowed.
174     **/
175    public abstract bool open();
176    /**
177     * Close the transport. Closing an already closed transport is allowed.
178     **/
179    public abstract void close();
180    /**
181     * Return the transport identification.
182     **/
183    public abstract string getName();
184    /**
185     * Set delegates for being called when there is something to read or there has been an exception.
186     **/
187    public abstract void setDelegates( TransportFunc? readfunc, TransportFunc? hupfunc );
188    /**
189     * Get delegates
190     **/
191    public abstract void getDelegates( out TransportFunc? readfun, out TransportFunc? hupfun );
192    /**
193     * Set priorities for reading and writing
194     **/
195    public abstract void setPriorities( int rp, int wp );
196    /**
197     * Set buffered or unbuffered mode
198     **/
199    public abstract void setBuffered( bool on );
200    /**
201     * Write data to the transport and wait for a response.
202     * Read the response into a buffer provided and owned by the caller.
203     **/
204    public abstract int writeAndRead( void* wdata, int wlength, void* rdata, int rlength, int maxWait = 5000 );
205    /**
206     * Read data from the transport into buffer provided and owned by caller.
207     **/
208    public abstract int read( void* data, int length );
209    /**
210     * Write data to the transport.
211     **/
212    public abstract int write( void* data, int length );
213    /**
214     * Pause reading and writing from/to the transport.
215     * @returns the file descriptor that can now be used from another process.
216     **/
217    public abstract int freeze();
218    /**
219     * Resume reading and writing from/to the transport.
220     * @note This invalidates the file descriptor retuned by freeze().
221     **/
222    public abstract void thaw();
223    /**
224     * Drain the transport (wait until everything has been written to the underlying device)
225     **/
226    public abstract void drain();
227    /**
228     * Flush the transport (discard everything in the buffers not sent)
229     **/
230    public abstract void flush();
231    /**
232     * Suspend the transport. This is to handle hardware suspend of the underlaying
233     * hardware and not the transport logic itself.
234     **/
235    public abstract bool suspend();
236    /**
237     * Resumse the transport after it was suspended
238     **/
239    public abstract void resume();
240    /**
241     * Should not be here, but wants to be accessed from the command queue
242     **/
243    public FsoFramework.Logger logger;
244}
245
246//===========================================================================
247public delegate void FsoFramework.TransportFunc( Transport transport );
248public delegate int FsoFramework.TransportDataFunc( void* data, int length, Transport transport );
249public delegate bool FsoFramework.TransportBoolFunc( Transport transport );
250public delegate int FsoFramework.TransportIntFunc( Transport transport );
251
252// vim:ts=4:sw=4:expandtab
Note: See TracBrowser for help on using the repository browser.