src/org/sipdroid/net/SipdroidSocket.java
changeset 823 2036ebfaccda
child 830 c8b4ace735ea
equal deleted inserted replaced
536:537ddd8aa407 823:2036ebfaccda
       
     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 			android.util.Log.d("TEST","name : " + InetAddress.getByName("0"));
       
    46 		}
       
    47 	}
       
    48 	
       
    49 	public void close() {
       
    50 		super.close();
       
    51 		if (loaded) impl.close();
       
    52 	}
       
    53 	
       
    54 	public void setSoTimeout(int val) throws SocketException {
       
    55 		if (loaded) impl.setOption(SocketOptions.SO_TIMEOUT, val);
       
    56 		else super.setSoTimeout(val);
       
    57 	}
       
    58 	
       
    59 	public void receive(DatagramPacket pack) throws IOException {
       
    60 		if (loaded) impl.receive(pack);
       
    61 		else super.receive(pack);
       
    62 	}
       
    63 	
       
    64 	public void send(DatagramPacket pack) throws IOException {
       
    65 		if (loaded) impl.send(pack);
       
    66 		else super.send(pack);
       
    67 	}
       
    68 	
       
    69 	public boolean isConnected() {
       
    70 		if (loaded) return true;
       
    71 		else return super.isConnected();
       
    72 	}
       
    73 	
       
    74 	public void disconnect() {
       
    75 		if (!loaded) super.disconnect();
       
    76 	}
       
    77 	
       
    78 	public void connect(InetAddress addr,int port) {
       
    79 		if (!loaded) super.connect(addr,port);
       
    80 	}
       
    81 
       
    82 	static {
       
    83 		        System.loadLibrary("OSNetworkSystem");
       
    84 		        OSNetworkSystem.getOSNetworkSystem().oneTimeInitialization(true);
       
    85 		        SipdroidSocket.loaded = true;
       
    86 		        android.util.Log.d("OSNetworkSystem", "LOADED");
       
    87 			
       
    88 	}
       
    89 }