Skip to content

Latest commit

 

History

History
177 lines (143 loc) · 4.31 KB

README.md

File metadata and controls

177 lines (143 loc) · 4.31 KB

NET Module (ICL.net)

Classes related to networking. For example,
sending emails through SMTP, connecting or
creating TCP/UDP servers, getting network
interfaces and perform netmask calculations, etc.

JavaDoc

Usage

Follow the instructions on the last published version in maven repository

Email

By using this class, you can be sure the emails conform with the expected format.

try {
    Email email = new Email("some@example-com")
} catch(EmailMalformedException e) {
    Log.e("Email was not correct", e)
}

Smtp

Class to send emails using SMTP server. For convenience, you can set up most of the settings in the configuration file:

Configuration

# file: config.properties

mail.smtp.host=mail.example.com
# If port 465 or 587 are used, encryption settings will be added automatically
mail.smtp.port=25
# If no username is set, authentication will be disabled
mail.smtp.username=user9000
mail.smtp.password=top-secret
mail.smtp.from[email protected]
mail.smtp.fromName=Sender Name
mail.smtp.replyTo[email protected]

# If most or all of the messages are destined to a single account:
mail.smtp.defaultTo[email protected]

# When true, it will only report in logs and it won't send the message
mail.smtp.simulate=false

Example

Smtp smtp = new Smtp()
smtp.addAttachment(new File("manual.pdf"))
if(smtp.send(new Email("[email protected]"), "This is a test", "Please ignore this message")) {
    Log.i("Mail sent successfully")
}
// Other options:
smtp.sendDefault(subject, body)
smtp.send(listOfAddresses, subject, body)

MacAddress

Convert format from and to MacAddress format:

Note: although it is usually used with networking, this class can be used in any other context in which you need to handle pairs of hexadecimal values.

From String to binary array

MacAddress mac = MacAddress.fromString("3F:1A:99:05:3C:1B") //it will be stored as byte[]
byte[] bytes = mac.bytes
println bytes // [63, 42, 143, 5, 60, 27]

From binary array to String:

MacAddress mac = new MacAddress(
    bytes : [63, 42, 143, 5, 60, 27] as byte[],
    separator : '-',    // Default ':'
    upperCase : false   // Default true
)
println mac.toString() // 3f-2a-99-05-3c-1b

From String to String:

MacAddress mac = MacAddress.fromString("3F-1a-99-05-3c-1B") // mixed case, any separator, any length
mac.separator = ''
println mac.toString() // 3F1A99053C1B

Network

Useful methods about networking

Examples

// Get all IPs in a range
List<Inet4Address> addresses = Network.getIpsFromRange("192.168.1.10-250")

// Get all network interfaces
List<NetFace> interfaces = Network.networkInterfaces()

NetFace

Simple representation of a Network Interface, it stores:

  • Interface name
  • Mac Address
  • IP (Inet4Address and Inet6Address)

FtpClient

Simple to use FTP client wrapper of FTPClient from Apache Commons

Configuration

# By default the client will use 'passive' mode, you can change it with:
ftp.active=true

Example

FtpClient ftp = new FtpClient(ip, port, user, pass, path)
if (ftp.connect()) {
    ftp.cd("pictures")
    List<String> files = ftp.listFiles()
    File file = ftp.getFile(files.first())
    BufferedImage img = ftp.getImage(files.last())
    ftp.noop()
    ftp.cdToParent()
    ftp.uploadFile(file)
    ftp.abort()
    ftp.disconnect()
}

TCPServer && TCPClient

TCP servers and clients implemented using java ServerSocket class.

Example

TCPServer server = new TCPServer(port, {
    String clientCommand ->
        /* do something with the command */
        return "pong" //Response
})

TCPClient client = new TCPClient(serverIP, port)
client.sendRequest(new Request("ping", {
    Response response ->
        if(response.sent) {
            println response.toString() //output: "pong"
        }
}))

server.quit()

UDPServer && UDPClient

UDP server is an implementation of Java DatagramSocket:

Example

UDPServer server = new UDPServer(port, {
    String clientCommand ->
        return "pong" // response
})

UDPClient client = new UDPClient(serverIP, port)
client.send("ping", {
    String response ->
        println response // output: "pong"
})
client.quit()