Skip to content

Commit

Permalink
SendDataSynchronous method implemented
Browse files Browse the repository at this point in the history
SendDataSynchronous method implemented for TCPClientConnection and
TCPConnector
  • Loading branch information
nsiatras committed Jan 19, 2014
1 parent f297f9b commit 38a7626
Show file tree
Hide file tree
Showing 41 changed files with 144 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun, 24 Nov 2013 18:12:25 +0200
#Sun, 19 Jan 2014 18:17:42 +0200


D\:\\Development\\Source\ Rabbit\\Extasys\\Extasys\ for\ Java\ Examples\\Extasys.Examples.TCPClient=
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ of this software and associated documentation files (the "Software"), to deal
import Extasys.Network.TCP.Client.Exceptions.ConnectorCannotSendPacketException;
import Extasys.Network.TCP.Client.Exceptions.ConnectorDisconnectedException;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
Expand All @@ -33,6 +36,7 @@ public class TCPClient extends Extasys.Network.TCP.Client.ExtasysTCPClient
{

private AutoSendMessages fAutoSendMessagesThread;
private Charset fCharset = Charset.forName("UTF-8");

public TCPClient(String name, String description, InetAddress remoteHostIP, int remoteHostPort, int corePoolSize, int maximumPoolSize)
{
Expand All @@ -49,7 +53,16 @@ public TCPClient(String name, String description, InetAddress remoteHostIP, int
@Override
public void OnDataReceive(TCPConnector connector, DataFrame data)
{
//System.out.println("Data received: " + new String(data.getBytes()));
try
{
String dataStr = new String(data.getBytes(), fCharset);

connector.SendData(dataStr + "#SPLITTER#");
}
catch (Exception ex)
{

}
}

@Override
Expand All @@ -68,8 +81,21 @@ public void OnDisconnect(TCPConnector connector)
public void StartSendingMessages()
{
StopSendingMessages();
fAutoSendMessagesThread = new AutoSendMessages(this);
fAutoSendMessagesThread.start();
try
{
SendData("1#SPLITTER#");

//fAutoSendMessagesThread = new AutoSendMessages(this);
//fAutoSendMessagesThread.start();
}
catch (ConnectorDisconnectedException ex)
{
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ConnectorCannotSendPacketException ex)
{
Logger.getLogger(TCPClient.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void StopSendingMessages()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Sun, 19 Jan 2014 16:20:13 +0200


D\:\\Development\\Source\ Rabbit\\Extasys\\Extasys\ for\ Java\ Examples\\Extasys.Examples.TCPServer=

D\:\\Development\\Source\ Rabbit\\Extasys\\Extasys\ for\ Java=
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sun, 24 Nov 2013 18:12:22 +0200
#Sun, 19 Jan 2014 18:17:12 +0200


D\:\\Development\\Source\ Rabbit\\Extasys\\Extasys\ for\ Java\ Examples\\Extasys.Examples.TCPServer=
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ of this software and associated documentation files (the "Software"), to deal
import Extasys.Network.TCP.Server.Listener.TCPClientConnection;
import Extasys.Network.TCP.Server.Listener.TCPListener;
import java.net.InetAddress;
import java.nio.charset.Charset;

/**
*
Expand All @@ -32,6 +33,7 @@ public class TCPServer extends Extasys.Network.TCP.Server.ExtasysTCPServer
{

private TCPListener fMyTCPListener;
private Charset fCharset = Charset.forName("UTF-8");

public TCPServer(String name, String description, InetAddress listenerIP, int port, int maxConnections, int connectionsTimeOut, int corePoolSize, int maximumPoolSize)
{
Expand All @@ -52,7 +54,9 @@ public void OnDataReceive(TCPClientConnection sender, DataFrame data)
{
try
{
sender.SendData("GET#SPLITTER#");
byte[] bytes = data.getBytes();

sender.SendDataSynchronous(new String(bytes, fCharset) + "#SPLITTER#");
}
catch (Exception ex)
{
Expand Down
4 changes: 4 additions & 0 deletions Extasys for Java/build/built-jar.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Sun, 19 Jan 2014 17:04:29 +0200


D\:\\Development\\Source\ Rabbit\\Extasys\\Extasys\ for\ Java=
Empty file.
Empty file.
Binary file modified Extasys for Java/build/classes/Extasys/ManualResetEvent.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file modified Extasys for Java/dist/Extasys.jar
Binary file not shown.
6 changes: 5 additions & 1 deletion Extasys for Java/nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
<group>
<file>file:/D:/Development/Source%20Rabbit/Extasys/Extasys%20for%20Java/src/Extasys/Network/TCP/Client/Connectors/TCPConnector.java</file>
<file>file:/D:/Development/Source%20Rabbit/Extasys/Extasys%20for%20Java/src/Extasys/Network/TCP/Server/Listener/Packets/OutgoingTCPClientConnectionPacket.java</file>
<file>file:/D:/Development/Source%20Rabbit/Extasys/Extasys%20for%20Java/src/Extasys/Network/TCP/Server/Listener/TCPClientConnection.java</file>
</group>
</open-files>
</project-private>
2 changes: 1 addition & 1 deletion Extasys for Java/src/Extasys/ManualResetEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public synchronized void WaitOne(long milliseconds)
public synchronized void Set()
{
fInitialState = true;
notify();
notifyAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,41 @@ public void SendData(byte[] bytes, int offset, int length) throws ConnectorDisco
}
}

/**
* Send data to server and wait until data transfer complete.
*
* @param data is the string data to send
* @throws ConnectorDisconnectedException
*/
public void SendDataSynchronous(String data) throws ConnectorDisconnectedException
{
byte[] bytes = data.getBytes();
SendDataSynchronous(bytes, 0, bytes.length);
}

/**
* Send data to server and wait until data transfer complete.
*
* @param bytes is the byte array to be send.
* @param offset is the position in the data buffer at witch to begin
* sending.
* @param length is the number of the bytes to be send.
* @throws ConnectorDisconnectedException
*/
public void SendDataSynchronous(byte[] bytes, int offset, int length) throws ConnectorDisconnectedException
{
try
{
fOutput.write(bytes, offset, length);
fBytesOut += length;
}
catch (IOException ex)
{
Stop();
throw new ConnectorDisconnectedException(this);
}
}

/**
* Returns the main Extasys TCP Client of the connector.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void run()
{
fClient.fOutput.write(fBytes, fOffset, fLength);
fClient.fBytesOut += fLength;
fClient.fMyListener.fBytesOut += fLength;
}
catch (IOException ioException)
{
Expand All @@ -111,6 +112,8 @@ public void run()
try
{
fClient.fOutput.write(fBytes, fOffset, fLength);
fClient.fBytesOut += fLength;
fClient.fMyListener.fBytesOut += fLength;
}
catch (IOException ioException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,42 @@ of this software and associated documentation files (the "Software"), to deal
*
* @author Nikos Siatras
*/
public class TCPClientConnection
public final class TCPClientConnection
{

//Connection properties
public Socket fConnection;
protected Socket fConnection;
protected boolean fActive = false;
protected boolean fIsConnected = false;
private TCPListener fMyListener;
public TCPListener fMyListener;
public ExtasysTCPServer fMyExtasysServer;
private String fIPAddress;
private String fName = "";
private Object fTag = null;
private Thread fClientDataReaderThread;
private Date fConnectionStartUpDateTime;
private final Date fConnectionStartUpDateTime;
//Data input-output streams.
public InputStream fInput;
public OutputStream fOutput;
//Data throughput.
public long fBytesIn = 0, fBytesOut = 0;
//Message collector.
public TCPClientConnectionMessageCollector fMyMessageCollector;
protected boolean fUseMessageCollector;
protected final boolean fUseMessageCollector;
//Messages IO.
public IncomingTCPClientConnectionPacket fLastIncomingPacket = null;
public OutgoingTCPClientConnectionPacket fLastOugoingPacket = null;
public MessageCollectorTCPClientConnectionPacket fLastMessageCollectorPacket = null;

public TCPClientConnection(Socket socket, TCPListener myTCPListener, boolean useMessageCollector, String ETX)
{
fConnectionStartUpDateTime = ExtasysCalendar.fCalendar.getTime();
fUseMessageCollector = useMessageCollector;
fConnection = socket;

try
{
fUseMessageCollector = useMessageCollector;
fIPAddress = socket.getInetAddress().toString() + ":" + String.valueOf(socket.getPort());
fConnection = socket;
fIsConnected = true;

if (myTCPListener.getConnectionTimeOut() > 0) //Connection time-out.
Expand Down Expand Up @@ -105,8 +107,6 @@ public TCPClientConnection(Socket socket, TCPListener myTCPListener, boolean use

fMyListener.AddClient(this);
StartReceivingData();

fConnectionStartUpDateTime = ExtasysCalendar.fCalendar.getTime();
}

private void StartReceivingData()
Expand Down Expand Up @@ -162,11 +162,54 @@ public void SendData(byte[] bytes, int offset, int length) throws ClientIsDiscon
{
throw new ClientIsDisconnectedException(this);
}
fBytesOut += length;
fMyListener.fBytesOut += length;
fLastOugoingPacket = new OutgoingTCPClientConnectionPacket(this, bytes, offset, length, fLastOugoingPacket);
}

/**
* Send data to client and wait until data transfer complete.
*
* @param data is the string to send.
* @throws ClientIsDisconnectedException
*/
public void SendDataSynchronous(String data) throws ClientIsDisconnectedException
{
byte[] bytes = data.getBytes();
SendDataSynchronous(bytes, 0, bytes.length);
}

/**
* Send data to client and wait until data transfer complete.
*
* @param bytes is the byte array to be send.
* @param offset is the position in the data buffer at witch to begin
* @param length is the number of the bytes to be send.
* @throws
* Extasys.Network.TCP.Server.Listener.Exceptions.ClientIsDisconnectedException
*/
public void SendDataSynchronous(byte[] bytes, int offset, int length) throws ClientIsDisconnectedException
{
if (!this.fIsConnected)
{
throw new ClientIsDisconnectedException(this);
}

try
{
fOutput.write(bytes, offset, length);
fBytesOut += length;
fMyListener.fBytesOut += length;
}
catch (IOException ex)
{
if (fLastOugoingPacket != null)
{
fLastOugoingPacket.Cancel();
}
this.DisconnectMe();
throw new ClientIsDisconnectedException(this);
}
}

/**
* Disconnect this client.
*/
Expand Down

0 comments on commit 38a7626

Please sign in to comment.