remove more relay-only/dead code

This commit is contained in:
Eric House 2022-05-14 14:03:47 -07:00
parent 0a3851fba2
commit fbec2c926f
3 changed files with 37 additions and 340 deletions

View file

@ -1,6 +1,6 @@
/* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */
/*
* Copyright 2009 - 2012 by Eric House (xwords@eehouse.org). All
* Copyright 2009 - 2022 by Eric House (xwords@eehouse.org). All
* rights reserved.
*
* This program is free software; you can redistribute it and/or
@ -26,36 +26,13 @@ import android.content.Context;
import org.eehouse.android.xw4.jni.CommsAddrRec;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
import org.eehouse.android.xw4.jni.JNIThread;
import org.eehouse.android.xw4.jni.JNIThread.JNICmd;
import org.eehouse.android.xw4.jni.TransportProcs;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.UnresolvedAddressException;
import java.util.Iterator;
import java.util.Vector;
public class CommsTransport implements TransportProcs,
NetStateCache.StateChangedIf {
public class CommsTransport implements TransportProcs {
private static final String TAG = CommsTransport.class.getSimpleName();
private Selector m_selector;
private SocketChannel m_socketChannel;
private CommsAddrRec m_relayAddr;
private String m_useHost;
private JNIThread m_jniThread;
private CommsThread m_thread;
private TransportProcs.TPMsgHandler m_tpHandler;
private boolean m_done = false;
private Vector<ByteBuffer> m_buffersOut;
private ByteBuffer m_bytesOut;
private ByteBuffer m_bytesIn;
private Context m_context;
private long m_rowid;
@ -69,281 +46,6 @@ public class CommsTransport implements TransportProcs,
m_context = context;
m_tpHandler = handler;
m_rowid = rowid;
m_buffersOut = new Vector<>();
m_bytesIn = ByteBuffer.allocate( 2048 );
NetStateCache.register( context, this );
}
public class CommsThread extends Thread {
@Override
public void run()
{
if ( !BuildConfig.UDP_ENABLED ) {
m_done = false;
boolean failed = true;
try {
if ( XWApp.onEmulator() ) {
System.setProperty("java.net.preferIPv6Addresses", "false");
}
m_selector = Selector.open();
failed = loop();
closeSocket();
} catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe );
} catch ( UnresolvedAddressException uae ) {
Log.w( TAG, "bad address: name: %s; port: %s; exception: %s",
m_useHost, m_relayAddr.ip_relay_port, uae.toString() );
}
m_thread = null;
if ( failed ) {
m_jniThread.handle( JNICmd.CMD_TRANSFAIL,
CommsConnType.COMMS_CONN_RELAY );
}
}
}
private boolean loop()
{
boolean failed = false;
if ( !BuildConfig.UDP_ENABLED ) {
outer_loop:
while ( !m_done ) {
try {
synchronized( this ) {
// if we have data and no socket, try to connect.
if ( null == m_socketChannel
&& 0 < m_buffersOut.size() ) {
try {
m_socketChannel = SocketChannel.open();
m_socketChannel.configureBlocking( false );
Log.i( TAG, "connecting to %s:%d",
m_useHost, m_relayAddr.ip_relay_port );
InetSocketAddress isa = new
InetSocketAddress(m_useHost,
m_relayAddr.ip_relay_port );
m_socketChannel.connect( isa );
} catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe );
failed = true;
break outer_loop;
}
}
if ( null != m_socketChannel ) {
int ops = figureOps();
// DbgUtils.logf( "calling with ops=%x", ops );
m_socketChannel.register( m_selector, ops );
}
}
m_selector.select();
} catch ( ClosedChannelException cce ) {
// we get this when relay goes down. Need to notify!
failed = true;
closeSocket();
Log.w( TAG, "exiting: %s", cce.toString() );
break; // don't try again
} catch ( java.io.IOException ioe ) {
closeSocket();
Log.w( TAG, "exiting: %s", ioe.toString() );
Log.w( TAG, ioe.toString() );
} catch ( java.nio.channels.NoConnectionPendingException ncp ) {
Log.ex( TAG, ncp );
closeSocket();
break;
}
Iterator<SelectionKey> iter = m_selector.selectedKeys().iterator();
while ( iter.hasNext() ) {
SelectionKey key = iter.next();
SocketChannel channel = (SocketChannel)key.channel();
iter.remove();
try {
if (key.isValid() && key.isConnectable()) {
if ( !channel.finishConnect() ) {
key.cancel();
}
}
if (key.isValid() && key.isReadable()) {
m_bytesIn.clear(); // will wipe any pending data!
// DbgUtils.logf( "socket is readable; buffer has space for "
// + m_bytesIn.remaining() );
int nRead = channel.read( m_bytesIn );
if ( nRead == -1 ) {
channel.close();
} else {
addIncoming();
}
ConnStatusHandler.
updateStatusIn( m_context,
CommsConnType.COMMS_CONN_RELAY,
0 <= nRead );
}
if (key.isValid() && key.isWritable()) {
getOut();
if ( null != m_bytesOut ) {
int nWritten = channel.write( m_bytesOut );
ConnStatusHandler.
updateStatusOut( m_context,
CommsConnType.COMMS_CONN_RELAY,
0 < nWritten );
}
}
} catch ( java.io.IOException ioe ) {
Log.w( TAG, "%s: cancelling key", ioe.toString() );
key.cancel();
failed = true;
break outer_loop;
} catch ( java.nio.channels.
NoConnectionPendingException ncp ) {
Log.ex( TAG, ncp );
break outer_loop;
}
}
}
}
return failed;
} // loop
}
public void setReceiver( JNIThread jnit )
{
m_jniThread = jnit;
}
public void waitToStop()
{
waitToStopImpl();
NetStateCache.unregister( m_context, this );
}
//////////////////////////////////////////////////////////////////////
// NetStateCache.StateChangedIf interface
//////////////////////////////////////////////////////////////////////
public void onNetAvail( boolean nowAvailable )
{
if ( !nowAvailable ) {
waitToStopImpl();
m_jniThread.handle( JNICmd.CMD_TRANSFAIL,
CommsConnType.COMMS_CONN_RELAY );
}
}
private synchronized void putOut( final byte[] buf )
{
if ( !BuildConfig.UDP_ENABLED ) {
int len = buf.length;
ByteBuffer netbuf = ByteBuffer.allocate( len + 2 );
netbuf.putShort( (short)len );
netbuf.put( buf );
m_buffersOut.add( netbuf );
Assert.assertEquals( netbuf.remaining(), 0 );
if ( null != m_selector ) {
// tell it it's got some writing to do
m_selector.wakeup(); // getting NPE inside here -- see below
}
}
}
private synchronized void closeSocket()
{
if ( !BuildConfig.UDP_ENABLED && null != m_socketChannel ) {
try {
m_socketChannel.close();
} catch ( Exception e ) {
Log.w( TAG, "closing socket: %s", e.toString() );
}
m_socketChannel = null;
}
}
private synchronized void getOut()
{
if ( null != m_bytesOut && m_bytesOut.remaining() == 0 ) {
m_bytesOut = null;
}
if ( null == m_bytesOut && m_buffersOut.size() > 0 ) {
m_bytesOut = m_buffersOut.remove(0);
m_bytesOut.flip();
}
}
private synchronized int figureOps() {
int ops;
if ( null == m_socketChannel ) {
ops = 0;
} else if ( m_socketChannel.isConnected() ) {
ops = SelectionKey.OP_READ;
if ( (null != m_bytesOut && m_bytesOut.hasRemaining())
|| m_buffersOut.size() > 0 ) {
ops |= SelectionKey.OP_WRITE;
}
} else {
ops = SelectionKey.OP_CONNECT;
}
return ops;
}
private void addIncoming( )
{
if ( !BuildConfig.UDP_ENABLED ) {
m_bytesIn.flip();
for ( ; ; ) {
int len = m_bytesIn.remaining();
if ( len <= 0 ) {
break;
}
if ( null == m_packetIn ) { // we're not mid-packet
Assert.assertTrue( len > 1 ); // tell me if I see this case
if ( len == 1 ) { // half a length byte...
break; // can I leave it in the buffer?
} else {
m_packetIn = new byte[m_bytesIn.getShort()];
m_haveLen = 0;
}
} else { // we're mid-packet
int wantLen = m_packetIn.length - m_haveLen;
if ( wantLen > len ) {
wantLen = len;
}
m_bytesIn.get( m_packetIn, m_haveLen, wantLen );
m_haveLen += wantLen;
if ( m_haveLen == m_packetIn.length ) {
// send completed packet
m_jniThread.handle( JNICmd.CMD_RECEIVE, m_packetIn, null );
m_packetIn = null;
}
}
}
}
}
private void waitToStopImpl()
{
if ( !BuildConfig.UDP_ENABLED ) {
m_done = true; // this is in a race!
if ( null != m_selector ) {
m_selector.wakeup(); // getting NPE inside here -- see below
}
if ( null != m_thread ) { // synchronized this? Or use Thread method
try {
m_thread.join(100); // wait up to 1/10 second
} catch ( java.lang.InterruptedException ie ) {
Log.ex( TAG, ie );
}
m_thread = null;
}
}
}
// TransportProcs interface
@ -363,20 +65,14 @@ public class CommsTransport implements TransportProcs,
Assert.assertNotNull( addr );
Assert.assertTrueNR( addr.contains( conType ) ); // fired per google
if ( !BuildConfig.UDP_ENABLED && conType == CommsConnType.COMMS_CONN_RELAY
&& null == m_relayAddr ) {
m_relayAddr = new CommsAddrRec( addr );
m_useHost = NetUtils.forceHost( m_relayAddr.ip_relay_hostName );
if ( !BuildConfig.UDP_ENABLED && conType == CommsConnType.COMMS_CONN_RELAY ) {
Assert.failDbg();
}
if ( !BuildConfig.UDP_ENABLED && conType == CommsConnType.COMMS_CONN_RELAY ) {
if ( NetStateCache.netAvail( m_context ) ) {
putOut( buf ); // add to queue
if ( null == m_thread ) {
m_thread = new CommsThread();
m_thread.start();
}
nSent = buf.length;
Assert.failDbg();
nSent = -1;
}
} else {
nSent = sendForAddr( m_context, addr, conType, m_rowid, gameID,

View file

@ -39,36 +39,38 @@ public class NetStateCache {
private static final String TAG = NetStateCache.class.getSimpleName();
private static final long WAIT_STABLE_MILLIS = 2 * 1000;
public interface StateChangedIf {
public void onNetAvail( boolean nowAvailable );
}
// I'm leaving this stuff commented out because MQTT might want to use it
// to try resending when the net comes back.
// public interface StateChangedIf {
// public void onNetAvail( boolean nowAvailable );
// }
// private static HashSet<StateChangedIf> s_ifs;
private static AtomicBoolean s_haveReceiver = new AtomicBoolean( false );
private static HashSet<StateChangedIf> s_ifs;
private static boolean s_netAvail = false;
private static boolean s_isWifi;
private static PvtBroadcastReceiver s_receiver;
private static final boolean s_onSDKSim = Build.PRODUCT.contains("sdk"); // not genymotion
public static void register( Context context, StateChangedIf proc )
{
if ( Utils.isOnUIThread() ) {
initIfNot( context );
synchronized( s_ifs ) {
s_ifs.add( proc );
}
}
}
// public static void register( Context context, StateChangedIf proc )
// {
// if ( Utils.isOnUIThread() ) {
// initIfNot( context );
// synchronized( s_ifs ) {
// s_ifs.add( proc );
// }
// }
// }
public static void unregister( Context context, StateChangedIf proc )
{
if ( Utils.isOnUIThread() ) {
initIfNot( context );
synchronized( s_ifs ) {
s_ifs.remove( proc );
}
}
}
// public static void unregister( Context context, StateChangedIf proc )
// {
// if ( Utils.isOnUIThread() ) {
// initIfNot( context );
// synchronized( s_ifs ) {
// s_ifs.remove( proc );
// }
// }
// }
static long s_lastNetCheck = 0;
public static boolean netAvail( Context context )
@ -152,7 +154,7 @@ public class NetStateCache {
context.getApplicationContext()
.registerReceiver( s_receiver, filter );
s_ifs = new HashSet<>();
// s_ifs = new HashSet<>();
s_haveReceiver.set( true );
}
}
@ -250,12 +252,12 @@ public class NetStateCache {
Log.i( TAG, "notifyStateChanged(%b)", s_netAvail );
synchronized( s_ifs ) {
Iterator<StateChangedIf> iter = s_ifs.iterator();
while ( iter.hasNext() ) {
iter.next().onNetAvail( s_netAvail );
}
}
// synchronized( s_ifs ) {
// Iterator<StateChangedIf> iter = s_ifs.iterator();
// while ( iter.hasNext() ) {
// iter.next().onNetAvail( s_netAvail );
// }
// }
if ( s_netAvail ) {
CommsConnType typ = CommsConnType

View file

@ -216,7 +216,6 @@ public class JNIThread extends Thread implements AutoCloseable {
if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) {
m_xport = new CommsTransport( context, xportHandler, m_rowid,
m_gi.serverRole );
m_xport.setReceiver( this );
}
CommonPrefs cp = CommonPrefs.get( context );