Skip to content

Commit 0cd9c67

Browse files
committed
Merge branch 'feature/task_4' into dev. This closes dddpaul#4.
2 parents 00e67a2 + 042410c commit 0cd9c67

File tree

10 files changed

+59
-33
lines changed

10 files changed

+59
-33
lines changed

netcat/src/main/java/com/github/dddpaul/netcat/NetCat.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ public void closeOutput()
4848
output = null;
4949
}
5050

51-
@Override
52-
public OutputStream getOutput()
53-
{
54-
return output;
55-
}
56-
5751
/**
5852
* Strip last CR+LF
5953
*/

netcat/src/main/java/com/github/dddpaul/netcat/NetCater.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ enum Proto { TCP, UDP }
2020
public void setInput( InputStream input );
2121
public void createOutput();
2222
public void closeOutput();
23-
public OutputStream getOutput();
2423
public String getOutputString();
2524
public boolean isConnected();
2625
public boolean isListening();

netcat/src/main/java/com/github/dddpaul/netcat/UdpNetCat.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
public class UdpNetCat extends NetCat
1616
{
17+
public static final String DISCONNECT_SEQUENCE = "~.";
18+
1719
private final String CLASS_NAME = getClass().getSimpleName();
1820

1921
private NetCatTask task;
@@ -127,7 +129,11 @@ private void receiveFromChannel() throws Exception
127129
bytesReceived = channel.read( buf );
128130
}
129131
if( bytesReceived > 0 ) {
130-
Log.d( CLASS_NAME, String.format( "%d bytes was received from %s", bytesReceived, remoteSocketAddress ));
132+
Log.d( CLASS_NAME, String.format( "%d bytes was received from %s (UDP)", bytesReceived, remoteSocketAddress ) );
133+
if( DISCONNECT_SEQUENCE.equals( new String( buf.array(), 0, buf.position() ).trim() ) ) {
134+
Log.d( CLASS_NAME, String.format( "Disconnect sequence was received from %s (UDP)", remoteSocketAddress ) );
135+
break;
136+
}
131137
output.write( buf.array(), 0, buf.position() );
132138
publishProgress( CONNECTED.toString(), output.toString() );
133139
buf.clear();

netcat/src/main/java/com/github/dddpaul/netcat/ui/MainFragment.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import android.widget.Button;
1515
import android.widget.CheckBox;
1616
import android.widget.EditText;
17+
import android.widget.TextView;
18+
import android.widget.Toast;
1719

1820
import com.github.dddpaul.netcat.Constants;
1921
import com.github.dddpaul.netcat.R;
@@ -25,6 +27,7 @@
2527

2628
import butterknife.ButterKnife;
2729
import butterknife.InjectView;
30+
import butterknife.OnCheckedChanged;
2831
import butterknife.OnClick;
2932
import de.greenrobot.event.EventBus;
3033
import events.FragmentEvent;
@@ -49,6 +52,9 @@ public class MainFragment extends Fragment
4952
@InjectView( R.id.c_tcp_udp )
5053
protected CheckBox udpCheckbox;
5154

55+
@InjectView( R.id.tv_udp_tooltip )
56+
protected TextView udpTooltip;
57+
5258
private SharedPreferences prefs;
5359
private Set<String> connectToSet;
5460
private ArrayAdapter<String> connectToArrayAdapter;
@@ -111,6 +117,16 @@ protected void onListenButtonClick()
111117
EventBus.getDefault().post( new FragmentEvent( LISTEN, listenOn ) );
112118
}
113119

120+
@OnCheckedChanged( R.id.c_tcp_udp )
121+
protected void onUdpChecked( boolean checked )
122+
{
123+
if( checked ) {
124+
udpTooltip.setVisibility( View.VISIBLE );
125+
} else {
126+
udpTooltip.setVisibility( View.INVISIBLE );
127+
}
128+
}
129+
114130
@Override
115131
public void onResume()
116132
{

netcat/src/main/java/com/github/dddpaul/netcat/ui/ResultFragment.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,7 @@ public void netCatIsCompleted( Result result )
127127
EventBus.getDefault().post( new ActivityEvent( state ) );
128128
break;
129129
case RECEIVE:
130-
// Strip last CR+LF
131-
String s = netCat.getOutput().toString();
132-
if( s.length() > 0 ) {
133-
outputView.setText( s.substring( 0, s.length() - 1 ) );
134-
}
130+
outputView.setText( netCat.getOutputString() );
135131
netCat.closeOutput();
136132
if( netCat.isConnected() ) {
137133
disconnect();

netcat/src/main/res/layout/fragment_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,18 @@
106106
android:textAppearance="?android:attr/textAppearanceSmall"
107107
/>
108108

109+
<TextView
110+
android:id="@+id/tv_udp_tooltip"
111+
android:text="@string/label_udp_tooltip"
112+
android:layout_width="wrap_content"
113+
android:layout_height="wrap_content"
114+
android:layout_marginTop="20dp"
115+
android:paddingLeft="5dp"
116+
android:paddingRight="5dp"
117+
android:layout_below="@+id/c_tcp_udp"
118+
android:layout_alignParentLeft="true"
119+
android:layout_alignParentStart="true"
120+
android:visibility="invisible"
121+
/>
122+
109123
</RelativeLayout>

netcat/src/main/res/layout/fragment_result.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
android:layout_width="match_parent"
8686
android:layout_height="match_parent"
8787
android:layout_alignParentLeft="true"
88+
android:layout_alignParentRight="true"
8889
android:layout_alignParentStart="true"
8990
android:layout_alignParentBottom="true"
9091
android:layout_alignParentEnd="true"

netcat/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<string name="error_port_format">Digits is expected</string>
2424
<string name="label_text_to_send">Text to send:</string>
2525
<string name="label_received_text">Received text:</string>
26-
<string name="label_tcp_udp">Use UDP instead of TCP (experimental)</string>
26+
<string name="label_tcp_udp">Use UDP instead of TCP</string>
27+
<string name="label_udp_tooltip">Send \"~.\" from remote side to disconnect</string>
2728

2829
</resources>

netcat/src/test/java/com/github/dddpaul/netcat/NetCatTestParent.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,23 @@ public void receive() throws IOException, InterruptedException
123123
}
124124

125125
// Send string from external nc process
126-
process.getOutputStream().write( inputFromProcess.getBytes() );
127-
process.getOutputStream().flush();
128-
process.getOutputStream().close();
126+
new Thread( new Runnable()
127+
{
128+
@Override
129+
public void run()
130+
{
131+
try {
132+
process.getOutputStream().write( inputFromProcess.getBytes() );
133+
process.getOutputStream().flush();
134+
Thread.sleep( 500 );
135+
process.getOutputStream().write( UdpNetCat.DISCONNECT_SEQUENCE.getBytes() );
136+
process.getOutputStream().flush();
137+
process.getOutputStream().close();
138+
} catch( Exception e ) {
139+
e.printStackTrace();
140+
}
141+
}
142+
} ).start();
129143

130144
// Receive string from external nc process
131145
netCat.createOutput();
@@ -136,6 +150,6 @@ public void receive() throws IOException, InterruptedException
136150
assertThat( result.op, is( RECEIVE ));
137151
String line = netCat.getOutputString();
138152
Log.i( CLASS_NAME, line );
139-
assertThat( line, is( INPUT_NC ));
153+
assertThat( line, is( inputFromProcess + UdpNetCat.DISCONNECT_SEQUENCE ));
140154
}
141155
}

netcat/src/test/java/com/github/dddpaul/netcat/UdpNetCatTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ public void run()
7777
// Start NetCat listener
7878
listen( port );
7979

80-
// Stop receiving after some delay required for NetCat to receive test data
81-
new Thread( new Runnable()
82-
{
83-
@Override
84-
public void run()
85-
{
86-
try {
87-
Thread.sleep( 1000 );
88-
netCat.cancel();
89-
} catch( Exception e ) {
90-
e.printStackTrace();
91-
}
92-
}
93-
} ).start();
94-
9580
receive();
9681
send();
9782
}

0 commit comments

Comments
 (0)