src/org/sipdroid/net/SipdroidSocket.java
changeset 839 0e5b95573614
parent 835 4e40f3481f23
child 1005 a2cad81f348b
equal deleted inserted replaced
822:696b2880c994 839:0e5b95573614
       
     1 /*
       
     2  * Copyright (C) 2009 The Sipdroid Open Source Project
       
     3  * 
       
     4  * This file is part of Sipdroid (http://www.sipdroid.org)
       
     5  * 
       
     6  * Sipdroid is free software; you can redistribute it and/or modify
       
     7  * it under the terms of the GNU General Public License as published by
       
     8  * the Free Software Foundation; either version 3 of the License, or
       
     9  * (at your option) any later version.
       
    10  * 
       
    11  * This source code is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  * 
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this source code; if not, write to the Free Software
       
    18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    19  */
       
    20 
       
    21 package org.sipdroid.net;
       
    22 
       
    23 import java.io.IOException;
       
    24 import java.net.DatagramPacket;
       
    25 import java.net.DatagramSocket;
       
    26 import java.net.InetAddress;
       
    27 import java.net.SocketException;
       
    28 import java.net.SocketOptions;
       
    29 import java.net.UnknownHostException;
       
    30 
       
    31 import org.sipdroid.net.impl.OSNetworkSystem;
       
    32 import org.sipdroid.net.impl.PlainDatagramSocketImpl;
       
    33 
       
    34 public class SipdroidSocket extends DatagramSocket {
       
    35 
       
    36     PlainDatagramSocketImpl impl;
       
    37     public static boolean loaded = false;
       
    38 
       
    39     public SipdroidSocket(int port) throws SocketException, UnknownHostException {
       
    40 	super(!loaded?port:0);
       
    41 	if (loaded) {
       
    42 	    impl = new PlainDatagramSocketImpl();
       
    43 	    impl.create();
       
    44 	    impl.bind(port,InetAddress.getByName("0"));
       
    45 	}
       
    46     }
       
    47 
       
    48     public void close() {
       
    49 	super.close();
       
    50 	if (loaded) impl.close();
       
    51     }
       
    52 
       
    53     public void setSoTimeout(int val) throws SocketException {
       
    54 	if (loaded) impl.setOption(SocketOptions.SO_TIMEOUT, val);
       
    55 	else super.setSoTimeout(val);
       
    56     }
       
    57 
       
    58     public void receive(DatagramPacket pack) throws IOException {
       
    59 	if (loaded) impl.receive(pack);
       
    60 	else super.receive(pack);
       
    61     }
       
    62 
       
    63     public void send(DatagramPacket pack) throws IOException {
       
    64 	if (loaded) impl.send(pack);
       
    65 	else super.send(pack);
       
    66     }
       
    67 
       
    68     public boolean isConnected() {
       
    69 	if (loaded) return true;
       
    70 	else return super.isConnected();
       
    71     }
       
    72 
       
    73     public void disconnect() {
       
    74 	if (!loaded) super.disconnect();
       
    75     }
       
    76 
       
    77     public void connect(InetAddress addr,int port) {
       
    78 	if (!loaded) super.connect(addr,port);
       
    79     }
       
    80 
       
    81     static {
       
    82 	try {
       
    83 	    System.loadLibrary("OSNetworkSystem");
       
    84 	    OSNetworkSystem.getOSNetworkSystem().oneTimeInitialization(true);
       
    85 	    SipdroidSocket.loaded = true;
       
    86 	} catch (Throwable e) {
       
    87 	    e.printStackTrace();
       
    88 	}
       
    89     }
       
    90 }