Skip to content

Commit

Permalink
Merge pull request #27 from Tlantic/v0.3.0
Browse files Browse the repository at this point in the history
V0.3.0 - Merge
  • Loading branch information
vinnylinck committed Apr 7, 2014
2 parents 82e73ae + 1f62df7 commit 64eeebf
Show file tree
Hide file tree
Showing 11 changed files with 1,181 additions and 792 deletions.
17 changes: 16 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.tlantic.plugins.socket"
version="0.2.0">
version="0.3.0">
<name>Socket</name>
<description>Tlantic TCP socket plugin</description>
<license>GPL</license>
Expand Down Expand Up @@ -41,4 +41,19 @@
<source-file src="src/ios/Connection.m" />
</platform>

<!-- windows8 -->
<platform name="windows8">
<js-module src="src/windows8/Connection.js" name="Connection">
<merges target="" />
</js-module>
<js-module src="src/windows8/SocketProxy.js" name="SocketProxy">
<merges target="" />
</js-module>
<config-file target="package.appxmanifest" parent="/Package/Capabilities">
<Capability Name="internetClientServer" />
<Capability Name="privateNetworkClientServer" />
<Capability Name="internetClient" />
</config-file>
</platform>

</plugin>
28 changes: 28 additions & 0 deletions specs/v03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Cordova TCP Socket Plugin v0.3.x release notes
==============================================

## v0.3.0

This is the third version of the plugin, with few support to attend basic needs regarding Windows8 platform.
Some issues were closed and a great improvement was made on socket calls. After opening a TCP socket connection,
you can use the connection id to perform operations like send or disconnect.

### Cordova/Phonegap Compatibility

The following versions of Cordova/Phonegap were used to test this plugin:

* Cordova 3.4
* Phonegap 3.4


### Platforms support

Platforms supported by this plugin:

* Android 4.1.1 or later
* iOS 7.x
* Windows 8 (desktop) or later - probably works with a WinRT version

### Limitations

The pause/resume limitation may exists. I didn't take a look on it yet to check if there is any issue.
262 changes: 145 additions & 117 deletions src/android/Connection.java
Original file line number Diff line number Diff line change
@@ -1,117 +1,145 @@
package com.tlantic.plugins.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


/**
* @author viniciusl
*
* This class represents a socket connection, behaving like a thread to listen
* a TCP port and receive data
*/
public class Connection extends Thread {
private SocketPlugin hook;

private Socket callbackSocket;
private PrintWriter writer;
private BufferedReader reader;

private Boolean mustClose;
private String host;
private int port;


/**
* Creates a TCP socket connection object.
*
* @param pool Object containing "sendMessage" method to be called as a callback for data receive.
* @param host Target host for socket connection.
* @param port Target port for socket connection
*/
public Connection(SocketPlugin pool, String host, int port) {
super();
setDaemon(true);

this.mustClose = false;
this.host = host;
this.port = port;
this.hook = pool;
}


/**
* Returns socket connection state.
*
* @return true if socket connection is established or false case else.
*/
public boolean isConnected() {
return this.callbackSocket.isConnected();
}

/**
* Closes socket connection.
*/
public void close() {
// closing connection
try {
this.mustClose = true;
this.writer.close();
this.reader.close();
callbackSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* Writes on socket output stream to send data to target host.
*
* @param data information to be sent
*/
public void write(String data) {
this.writer.println(data);
}



/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
String chunk = null;

// creating connection
try {
this.callbackSocket = new Socket(this.host, this.port);
this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true);
this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream()));

// receiving data chunk
while(!this.mustClose){

try {
chunk = reader.readLine().replaceAll("\"\"", "null");
System.out.print("## RECEIVED DATA: " + chunk);
hook.sendMessage(this.host, this.port, chunk);
} catch (IOException e) {
e.printStackTrace();
}
}

} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

}
package com.tlantic.plugins.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


/**
* @author viniciusl
*
* This class represents a socket connection, behaving like a thread to listen
* a TCP port and receive data
*/
public class Connection extends Thread {
private SocketPlugin hook;

private Socket callbackSocket;
private PrintWriter writer;
private BufferedReader reader;

private Boolean mustClose;
private String host;
private int port;


/**
* Creates a TCP socket connection object.
*
* @param pool Object containing "sendMessage" method to be called as a callback for data receive.
* @param host Target host for socket connection.
* @param port Target port for socket connection
*/
public Connection(SocketPlugin pool, String host, int port) {
super();
setDaemon(true);

this.mustClose = false;
this.host = host;
this.port = port;
this.hook = pool;
}


/**
* Returns socket connection state.
*
* @return true if socket connection is established or false case else.
*/
public boolean isConnected() {

boolean result = (
this.callbackSocket == null ? false :
this.callbackSocket.isConnected() &&
this.callbackSocket.isBound() &&
!this.callbackSocket.isClosed() &&
!this.callbackSocket.isInputShutdown() &&
!this.callbackSocket.isOutputShutdown());

// if everything apparently is fine, time to test the streams
if (result) {
try {
this.callbackSocket.getInputStream().available();
} catch (IOException e) {
// connection lost
result = false;
}
}

return result;
}

/**
* Closes socket connection.
*/
public void close() {
// closing connection
try {
//this.writer.close();
//this.reader.close();
callbackSocket.shutdownInput();
callbackSocket.shutdownOutput();
callbackSocket.close();
this.mustClose = true;
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* Writes on socket output stream to send data to target host.
*
* @param data information to be sent
*/
public void write(String data) {
this.writer.println(data);
}



/* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
String chunk = null;

// creating connection
try {
this.callbackSocket = new Socket(this.host, this.port);
this.writer = new PrintWriter(this.callbackSocket.getOutputStream(), true);
this.reader = new BufferedReader(new InputStreamReader(callbackSocket.getInputStream()));

// receiving data chunk
while(!this.mustClose){

try {

if (this.isConnected()) {
chunk = reader.readLine();

if (chunk != null) {
chunk = chunk.replaceAll("\"\"", "null");
System.out.print("## RECEIVED DATA: " + chunk);
hook.sendMessage(this.host, this.port, chunk);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

}
Loading

0 comments on commit 64eeebf

Please sign in to comment.