From 058125005a39b8ca1cd13b2ee8c8a273cf531c00 Mon Sep 17 00:00:00 2001 From: Joey Grover Date: Wed, 13 Mar 2019 09:51:23 -0400 Subject: Refactor GenericTransport to CustomTransport --- .../smartdevicelink/transport/CustomTransport.java | 170 +++++++++++++++++++++ .../transport/CustomTransportConfig.java | 53 +++++++ .../transport/GenericTransport.java | 170 --------------------- .../transport/GenericTransportConfig.java | 53 ------- .../transport/TransportManager.java | 4 +- .../transport/enums/TransportType.java | 2 +- 6 files changed, 226 insertions(+), 226 deletions(-) create mode 100644 base/src/main/java/com/smartdevicelink/transport/CustomTransport.java create mode 100644 base/src/main/java/com/smartdevicelink/transport/CustomTransportConfig.java delete mode 100644 base/src/main/java/com/smartdevicelink/transport/GenericTransport.java delete mode 100644 base/src/main/java/com/smartdevicelink/transport/GenericTransportConfig.java (limited to 'base') diff --git a/base/src/main/java/com/smartdevicelink/transport/CustomTransport.java b/base/src/main/java/com/smartdevicelink/transport/CustomTransport.java new file mode 100644 index 000000000..6e029d1b5 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/transport/CustomTransport.java @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2019 Livio, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Livio Inc. nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +package com.smartdevicelink.transport; + +import com.smartdevicelink.protocol.SdlPacket; +import com.smartdevicelink.transport.enums.TransportType; +import com.smartdevicelink.transport.utl.TransportRecord; +import com.smartdevicelink.util.DebugTool; + +import java.nio.ByteBuffer; + +public abstract class CustomTransport implements TransportInterface{ + private static final String TAG = "CustomTransport"; + + final TransportRecord transportRecord; + final SdlPsm psm; + TransportCallback transportCallback; + + + + public CustomTransport(String address) { + //Creates a callback for when packets + psm = new SdlPsm(); + transportRecord = new TransportRecord(TransportType.CUSTOM,address); + } + + public TransportRecord getTransportRecord(){ + return this.transportRecord; + } + + + /** + * Call this method when reading a byte array off the transport + * @param bytes the bytes read off the transport + */ + public synchronized void onByteArrayReceived (byte[] bytes) { + + if(bytes != null && bytes.length > 0){ + boolean stateProgress; + int length = bytes.length; + for(int i = 0; i < length; i++){ + stateProgress = psm.handleByte(bytes[i]); + if (!stateProgress) {//We are trying to weed through the bad packet info until we get something + //Log.w(TAG, "Packet State Machine did not move forward from state - "+ psm.getState()+". PSM being Reset."); + psm.reset(); + } + + if (psm.getState() == SdlPsm.FINISHED_STATE) { + SdlPacket packet = psm.getFormedPacket(); + if (transportCallback != null && packet != null) { + packet.setTransportRecord(transportRecord); + transportCallback.onPacketReceived(packet); + } + //We put a trace statement in the message read so we can avoid all the extra bytes + psm.reset(); + } + } + + } + } + + /** + * Call this method when reading a ByteBuffer off the transport + * @param message the byte buffer that was read off the transport + */ + public synchronized void onByteBufferReceived (ByteBuffer message) { + if(message != null){ + boolean stateProgress; + while (message.hasRemaining()) { + stateProgress = psm.handleByte(message.get()); + if (!stateProgress) {//We are trying to weed through the bad packet info until we get something + + //Log.w(TAG, "Packet State Machine did not move forward from state - "+ psm.getState()+". PSM being Reset."); + psm.reset(); + } + + if (psm.getState() == SdlPsm.FINISHED_STATE) { + SdlPacket packet = psm.getFormedPacket(); + if (transportCallback != null && packet != null) { + packet.setTransportRecord(transportRecord); + transportCallback.onPacketReceived(packet); + } + //We put a trace statement in the message read so we can avoid all the extra bytes + psm.reset(); + } + } + + } + } + + @Override + public void start() { + if (transportCallback != null) { + transportCallback.onConnectionEstablished(); + } + } + + @Override + public void stop() { + if (transportCallback != null) { + transportCallback.onConnectionTerminated(); + } + } + + @Override + public void write(SdlPacket packet) { + byte[] bytes = packet.constructPacket(); + if(bytes != null && bytes.length > 0) { + try { + onWrite(bytes); + } catch (Exception exc) { + DebugTool.logError("Error attempting to write packet", exc); + } + } + } + + @Override + public void setCallback(TransportCallback transportCallback) { + this.transportCallback = transportCallback; + } + + public void onError(){ + if (transportCallback != null) { + transportCallback.onError(); + } + } + + + /** + * Integrator should write out these bytes to whatever actual transport there is. This will be called from the + * internals of the library. + * @param bytes a deconstructed packet into a byte array that needs to be written out + */ + public abstract void onWrite(byte[] bytes); + + + + + +} diff --git a/base/src/main/java/com/smartdevicelink/transport/CustomTransportConfig.java b/base/src/main/java/com/smartdevicelink/transport/CustomTransportConfig.java new file mode 100644 index 000000000..af0613aa1 --- /dev/null +++ b/base/src/main/java/com/smartdevicelink/transport/CustomTransportConfig.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Livio, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Livio Inc. nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +package com.smartdevicelink.transport; + +import com.smartdevicelink.transport.enums.TransportType; + +public class CustomTransportConfig extends BaseTransportConfig { + + final CustomTransport customTransport; + + public CustomTransportConfig(CustomTransport customTransport){ + this.customTransport = customTransport; + } + + @Override + public TransportType getTransportType() { + return TransportType.CUSTOM; + } + + public TransportInterface getTransportInterface(){ + return this.customTransport; + } +} diff --git a/base/src/main/java/com/smartdevicelink/transport/GenericTransport.java b/base/src/main/java/com/smartdevicelink/transport/GenericTransport.java deleted file mode 100644 index f0774dbbc..000000000 --- a/base/src/main/java/com/smartdevicelink/transport/GenericTransport.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (c) 2019 Livio, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Livio Inc. nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -package com.smartdevicelink.transport; - -import com.smartdevicelink.protocol.SdlPacket; -import com.smartdevicelink.transport.enums.TransportType; -import com.smartdevicelink.transport.utl.TransportRecord; -import com.smartdevicelink.util.DebugTool; - -import java.nio.ByteBuffer; - -public abstract class GenericTransport implements TransportInterface{ - private static final String TAG = "GenericTransport"; - - final TransportRecord transportRecord; - final SdlPsm psm; - TransportCallback transportCallback; - - - - public GenericTransport (String address) { - //Creates a callback for when packets - psm = new SdlPsm(); - transportRecord = new TransportRecord(TransportType.GENERIC,address); - } - - public TransportRecord getTransportRecord(){ - return this.transportRecord; - } - - - /** - * Call this method when reading a byte array off the transport - * @param bytes the bytes read off the transport - */ - public synchronized void onByteArrayReceived (byte[] bytes) { - - if(bytes != null && bytes.length > 0){ - boolean stateProgress; - int length = bytes.length; - for(int i = 0; i < length; i++){ - stateProgress = psm.handleByte(bytes[i]); - if (!stateProgress) {//We are trying to weed through the bad packet info until we get something - //Log.w(TAG, "Packet State Machine did not move forward from state - "+ psm.getState()+". PSM being Reset."); - psm.reset(); - } - - if (psm.getState() == SdlPsm.FINISHED_STATE) { - SdlPacket packet = psm.getFormedPacket(); - if (transportCallback != null && packet != null) { - packet.setTransportRecord(transportRecord); - transportCallback.onPacketReceived(packet); - } - //We put a trace statement in the message read so we can avoid all the extra bytes - psm.reset(); - } - } - - } - } - - /** - * Call this method when reading a ByteBuffer off the transport - * @param message the byte buffer that was read off the transport - */ - public synchronized void onByteBufferReceived (ByteBuffer message) { - if(message != null){ - boolean stateProgress; - while (message.hasRemaining()) { - stateProgress = psm.handleByte(message.get()); - if (!stateProgress) {//We are trying to weed through the bad packet info until we get something - - //Log.w(TAG, "Packet State Machine did not move forward from state - "+ psm.getState()+". PSM being Reset."); - psm.reset(); - } - - if (psm.getState() == SdlPsm.FINISHED_STATE) { - SdlPacket packet = psm.getFormedPacket(); - if (transportCallback != null && packet != null) { - packet.setTransportRecord(transportRecord); - transportCallback.onPacketReceived(packet); - } - //We put a trace statement in the message read so we can avoid all the extra bytes - psm.reset(); - } - } - - } - } - - @Override - public void start() { - if (transportCallback != null) { - transportCallback.onConnectionEstablished(); - } - } - - @Override - public void stop() { - if (transportCallback != null) { - transportCallback.onConnectionTerminated(); - } - } - - @Override - public void write(SdlPacket packet) { - byte[] bytes = packet.constructPacket(); - if(bytes != null && bytes.length > 0) { - try { - onWrite(bytes); - } catch (Exception exc) { - DebugTool.logError("Error attempting to write packet", exc); - } - } - } - - @Override - public void setCallback(TransportCallback transportCallback) { - this.transportCallback = transportCallback; - } - - public void onError(){ - if (transportCallback != null) { - transportCallback.onError(); - } - } - - - /** - * Integrator should write out these bytes to whatever actual transport there is. This will be called from the - * internals of the library. - * @param bytes a deconstructed packet into a byte array that needs to be written out - */ - public abstract void onWrite(byte[] bytes); - - - - - -} diff --git a/base/src/main/java/com/smartdevicelink/transport/GenericTransportConfig.java b/base/src/main/java/com/smartdevicelink/transport/GenericTransportConfig.java deleted file mode 100644 index 13cce7749..000000000 --- a/base/src/main/java/com/smartdevicelink/transport/GenericTransportConfig.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2019 Livio, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following - * disclaimer in the documentation and/or other materials provided with the - * distribution. - * - * Neither the name of the Livio Inc. nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -package com.smartdevicelink.transport; - -import com.smartdevicelink.transport.enums.TransportType; - -public class GenericTransportConfig extends BaseTransportConfig { - - final GenericTransport genericTransport; - - public GenericTransportConfig(GenericTransport genericTransport){ - this.genericTransport = genericTransport; - } - - @Override - public TransportType getTransportType() { - return TransportType.GENERIC; - } - - public TransportInterface getTransportInterface(){ - return this.genericTransport; - } -} diff --git a/base/src/main/java/com/smartdevicelink/transport/TransportManager.java b/base/src/main/java/com/smartdevicelink/transport/TransportManager.java index ed7a05d9f..aafaefe1b 100644 --- a/base/src/main/java/com/smartdevicelink/transport/TransportManager.java +++ b/base/src/main/java/com/smartdevicelink/transport/TransportManager.java @@ -63,8 +63,8 @@ public class TransportManager extends TransportManagerBase{ case WEB_SOCKET_SERVER: transport = new WebSocketServer2((WebSocketServerConfig)config, new SingleTransportCallbackImpl(new TransportRecord(TransportType.WEB_SOCKET_SERVER,"127.0.0.1:"+((WebSocketServerConfig)config).port))); break; - case GENERIC: - transport = ((GenericTransportConfig) config).getTransportInterface(); + case CUSTOM: + transport = ((CustomTransportConfig) config).getTransportInterface(); transport.setCallback(new SingleTransportCallbackImpl(transport.getTransportRecord())); break; } diff --git a/base/src/main/java/com/smartdevicelink/transport/enums/TransportType.java b/base/src/main/java/com/smartdevicelink/transport/enums/TransportType.java index 86b974c46..c3c68a82c 100644 --- a/base/src/main/java/com/smartdevicelink/transport/enums/TransportType.java +++ b/base/src/main/java/com/smartdevicelink/transport/enums/TransportType.java @@ -27,7 +27,7 @@ public enum TransportType { * This transport is setup to be essentially a proxy to a different transport. It allows a developer to create a * custom transport without much effort. */ - GENERIC, + CUSTOM, ; -- cgit v1.2.1