| 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 | //=========================================================================== |
|---|
| 21 | public errordomain FsoFramework.TransportError |
|---|
| 22 | { |
|---|
| 23 | UNABLE_TO_OPEN, |
|---|
| 24 | UNABLE_TO_WRITE, |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | //=========================================================================== |
|---|
| 28 | public enum FsoFramework.TransportState |
|---|
| 29 | { |
|---|
| 30 | CLOSED, |
|---|
| 31 | OPEN, |
|---|
| 32 | ALIVE, |
|---|
| 33 | FROZEN, |
|---|
| 34 | DEAD, |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public 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 void create() |
|---|
| 98 | { |
|---|
| 99 | if ( transport != null ) |
|---|
| 100 | { |
|---|
| 101 | transport.logger.warning( "Create called on already existing transport. Ignoring" ); |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | switch ( type ) |
|---|
| 106 | { |
|---|
| 107 | case "serial": |
|---|
| 108 | transport = new FsoFramework.SerialTransport( name, speed, raw, hard ); |
|---|
| 109 | break; |
|---|
| 110 | case "pty": |
|---|
| 111 | transport = new FsoFramework.PtyTransport(); |
|---|
| 112 | break; |
|---|
| 113 | case "unix": |
|---|
| 114 | case "udp": |
|---|
| 115 | case "tcp": |
|---|
| 116 | transport = new FsoFramework.SocketTransport( type, name, speed ); |
|---|
| 117 | break; |
|---|
| 118 | case "null": |
|---|
| 119 | transport = new FsoFramework.NullTransport(); |
|---|
| 120 | break; |
|---|
| 121 | case "raw": |
|---|
| 122 | transport = new FsoFramework.RawTransport( name ); |
|---|
| 123 | break; |
|---|
| 124 | default: |
|---|
| 125 | FsoFramework.theLogger.warning( @"Invalid transport type $type. Using NullTransport" ); |
|---|
| 126 | transport = new FsoFramework.NullTransport(); |
|---|
| 127 | break; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | public bool open() |
|---|
| 132 | { |
|---|
| 133 | if ( transport == null ) |
|---|
| 134 | { |
|---|
| 135 | create(); |
|---|
| 136 | } |
|---|
| 137 | return transport.open(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public string type; |
|---|
| 141 | public string name; |
|---|
| 142 | public uint speed; |
|---|
| 143 | public bool raw; |
|---|
| 144 | public bool hard; |
|---|
| 145 | public Transport transport; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | //=========================================================================== |
|---|
| 149 | public abstract class FsoFramework.Transport : Object |
|---|
| 150 | { |
|---|
| 151 | /** |
|---|
| 152 | * Create @a FsoFramework.Transport as indicated by @a type |
|---|
| 153 | **/ |
|---|
| 154 | public static Transport? create( string type, string name = "", uint speed = 0, bool raw = true, bool hard = true ) |
|---|
| 155 | { |
|---|
| 156 | switch ( type ) |
|---|
| 157 | { |
|---|
| 158 | case "serial": |
|---|
| 159 | return new FsoFramework.SerialTransport( name, speed, raw, hard ); |
|---|
| 160 | case "pty": |
|---|
| 161 | return new FsoFramework.PtyTransport(); |
|---|
| 162 | case "unix": |
|---|
| 163 | case "udp": |
|---|
| 164 | case "tcp": |
|---|
| 165 | return new FsoFramework.SocketTransport( type, name, speed ); |
|---|
| 166 | case "ngsmbasic": |
|---|
| 167 | return new FsoFramework.NgsmBasicMuxTransport( name, speed ); |
|---|
| 168 | case "ngsmadvanced": |
|---|
| 169 | return new FsoFramework.NgsmAdvancedMuxTransport( name, speed ); |
|---|
| 170 | case "combined": |
|---|
| 171 | return new FsoFramework.CombinedTransport( name ); |
|---|
| 172 | default: |
|---|
| 173 | return null; |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | /** |
|---|
| 177 | * @returns true, if the @a transport is open; else false. |
|---|
| 178 | */ |
|---|
| 179 | public abstract bool isOpen(); |
|---|
| 180 | /** |
|---|
| 181 | * Open the transport asynchronously. @returns true, if successful; else false. |
|---|
| 182 | */ |
|---|
| 183 | public abstract async bool openAsync(); |
|---|
| 184 | /** |
|---|
| 185 | * Close the transport. Closing an already closed transport is allowed. |
|---|
| 186 | **/ |
|---|
| 187 | public abstract bool open(); |
|---|
| 188 | /** |
|---|
| 189 | * Close the transport. Closing an already closed transport is allowed. |
|---|
| 190 | **/ |
|---|
| 191 | public abstract void close(); |
|---|
| 192 | /** |
|---|
| 193 | * Return the transport identification. |
|---|
| 194 | **/ |
|---|
| 195 | public abstract string getName(); |
|---|
| 196 | /** |
|---|
| 197 | * Set delegates for being called when there is something to read or there has been an exception. |
|---|
| 198 | **/ |
|---|
| 199 | public abstract void setDelegates( TransportFunc? readfunc, TransportFunc? hupfunc ); |
|---|
| 200 | /** |
|---|
| 201 | * Get delegates |
|---|
| 202 | **/ |
|---|
| 203 | public abstract void getDelegates( out TransportFunc? readfun, out TransportFunc? hupfun ); |
|---|
| 204 | /** |
|---|
| 205 | * Set priorities for reading and writing |
|---|
| 206 | **/ |
|---|
| 207 | public abstract void setPriorities( int rp, int wp ); |
|---|
| 208 | /** |
|---|
| 209 | * Set buffered or unbuffered mode |
|---|
| 210 | **/ |
|---|
| 211 | public abstract void setBuffered( bool on ); |
|---|
| 212 | /** |
|---|
| 213 | * Write data to the transport and wait for a response. |
|---|
| 214 | * Read the response into a buffer provided and owned by the caller. |
|---|
| 215 | **/ |
|---|
| 216 | public abstract int writeAndRead( void* wdata, int wlength, void* rdata, int rlength, int maxWait = 5000 ); |
|---|
| 217 | /** |
|---|
| 218 | * Read data from the transport into buffer provided and owned by caller. |
|---|
| 219 | **/ |
|---|
| 220 | public abstract int read( void* data, int length ); |
|---|
| 221 | /** |
|---|
| 222 | * Write data to the transport. |
|---|
| 223 | **/ |
|---|
| 224 | public abstract int write( void* data, int length ); |
|---|
| 225 | /** |
|---|
| 226 | * Pause reading and writing from/to the transport. |
|---|
| 227 | * @returns the file descriptor that can now be used from another process. |
|---|
| 228 | **/ |
|---|
| 229 | public abstract int freeze(); |
|---|
| 230 | /** |
|---|
| 231 | * Resume reading and writing from/to the transport. |
|---|
| 232 | * @note This invalidates the file descriptor retuned by freeze(). |
|---|
| 233 | **/ |
|---|
| 234 | public abstract void thaw(); |
|---|
| 235 | /** |
|---|
| 236 | * Drain the transport (wait until everything has been written to the underlying device) |
|---|
| 237 | **/ |
|---|
| 238 | public abstract void drain(); |
|---|
| 239 | /** |
|---|
| 240 | * Flush the transport (discard everything in the buffers not sent) |
|---|
| 241 | **/ |
|---|
| 242 | public abstract void flush(); |
|---|
| 243 | /** |
|---|
| 244 | * Suspend the transport. This is to handle hardware suspend of the underlaying |
|---|
| 245 | * hardware and not the transport logic itself. |
|---|
| 246 | **/ |
|---|
| 247 | public abstract bool suspend(); |
|---|
| 248 | /** |
|---|
| 249 | * Resumse the transport after it was suspended |
|---|
| 250 | **/ |
|---|
| 251 | public abstract void resume(); |
|---|
| 252 | /** |
|---|
| 253 | * Should not be here, but wants to be accessed from the command queue |
|---|
| 254 | **/ |
|---|
| 255 | public FsoFramework.Logger logger; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | //=========================================================================== |
|---|
| 259 | public delegate void FsoFramework.TransportFunc( Transport transport ); |
|---|
| 260 | public delegate int FsoFramework.TransportDataFunc( void* data, int length, Transport transport ); |
|---|
| 261 | public delegate bool FsoFramework.TransportBoolFunc( Transport transport ); |
|---|
| 262 | public delegate int FsoFramework.TransportIntFunc( Transport transport ); |
|---|
| 263 | |
|---|
| 264 | // vim:ts=4:sw=4:expandtab |
|---|