1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.location.cts.suplClient; 18 19 import java.io.BufferedInputStream; 20 import java.io.IOException; 21 import java.net.Socket; 22 import java.net.UnknownHostException; 23 import java.nio.ByteBuffer; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * A TCP client that is used to send and receive SUPL request and responses by the SUPL client. The 28 * constructor establishes a connection to the SUPL server specified by a given address and port. 29 */ 30 public class SuplTcpClient { 31 32 private static final int READ_TIMEOUT_MILLIS = (int) TimeUnit.SECONDS.toMillis(10); 33 private static final short HEADER_SIZE = 2; 34 /** BUFFER_SIZE data size that is enough to hold SUPL responses */ 35 private static final int SUPL_RESPONSE_BUFFER_SIZE = 16384; 36 private static final byte[] SUPL_RESPONSE_BUFFER = new byte[SUPL_RESPONSE_BUFFER_SIZE]; 37 38 private Socket socket; 39 private BufferedInputStream bufferedInputStream; 40 41 public SuplTcpClient(String suplServerName, int suplServerPort) 42 throws UnknownHostException, IOException { 43 System.out.println("Connecting to " + suplServerName + " on port " + suplServerPort); 44 socket = new Socket(suplServerName, suplServerPort); 45 socket.setSoTimeout(READ_TIMEOUT_MILLIS); 46 System.out.println("Connection established to " + socket.getOutputStream()); 47 bufferedInputStream = new BufferedInputStream(socket.getInputStream()); 48 49 } 50 51 /** Sends a byte array of SUPL data to the server */ 52 public void sendSuplRequest(byte[] data) throws IOException { 53 socket.getOutputStream().write(data); 54 } 55 56 /** 57 * Reads SUPL server response and return it as a byte array. Upon the SUPL protocol, the size of 58 * the payload is stored in the first two bytes of the response, hence these two bytes are read 59 * first followed by reading a payload of that size. Null is returned if the size of the payload 60 * is not readable. 61 */ 62 public byte[] getSuplResponse() throws IOException { 63 int sizeOfRead = bufferedInputStream.read(SUPL_RESPONSE_BUFFER, 0, HEADER_SIZE); 64 if (sizeOfRead == HEADER_SIZE) { 65 byte[] lengthArray = {SUPL_RESPONSE_BUFFER[0], SUPL_RESPONSE_BUFFER[1]}; 66 short dataLength = ByteBuffer.wrap(lengthArray).getShort(); 67 bufferedInputStream.read(SUPL_RESPONSE_BUFFER, 2, dataLength - HEADER_SIZE); 68 return SUPL_RESPONSE_BUFFER; 69 } else { 70 return null; 71 } 72 } 73 74 /** Closes the TCP socket */ 75 public void closeSocket() throws IOException { 76 socket.close(); 77 } 78 } 79