Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jnr/jnr-unixsocket
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Mar 9, 2016
2 parents 148a01d + 90a17d7 commit 52cd420
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ target/
\.rej$
\.conflict\~$

.idea
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ notifications:
on_failure: always
template:
- "%{repository} (%{branch}:%{commit} by %{author}): %{message} (%{build_url})"
sudo: false
69 changes: 62 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<packaging>jar</packaging>
<version>0.9-SNAPSHOT</version>
<version>0.12-SNAPSHOT</version>
<name>jnr-unixsocket</name>
<description>Native I/O access for java</description>
<url>http://github.com/jnr/jnr-unixsocket</url>
Expand Down Expand Up @@ -47,25 +47,25 @@
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-ffi</artifactId>
<version>2.0.3</version>
<version>2.0.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-constants</artifactId>
<version>0.8.7</version>
<version>0.9.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-enxio</artifactId>
<version>0.9</version>
<version>0.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
<version>3.0.12</version>
<version>3.0.28</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -76,10 +76,65 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<configuration>
<instructions>
<_nouses>true</_nouses>
<Import-Package>*,jnr.ffi.mapper,jnr.ffi.provider.converters,jnr.ffi.provider.jffi,com.kenai.jffi</Import-Package>
</instructions>
</configuration>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/jnr/unixsocket/SockAddrUnix.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final ProtocolFamily getFamily() {
*
* @param path The unix socket address
*/
final void setPath(java.lang.String path) {
void setPath(java.lang.String path) {
getPathField().set(path);
}

Expand Down Expand Up @@ -113,6 +113,11 @@ static final class BSDSockAddrUnix extends SockAddrUnix {
public final Unsigned8 sun_family = new Unsigned8();
public final UTF8String sun_addr = new UTF8String(ADDR_LENGTH);

@Override
public void setPath(java.lang.String path) {
super.setPath(path);
sun_len.set(path.length());
}
protected final UTF8String getPathField() {
return sun_addr;
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/jnr/unixsocket/UnixServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public class UnixServerSocket {
final UnixServerSocketChannel channel;
final int fd;
volatile UnixSocketAddress localAddress;

public UnixServerSocket() throws IOException {
this.channel = new UnixServerSocketChannel(this);
Expand All @@ -47,14 +48,14 @@ public void bind(SocketAddress endpoint, int backlog) throws java.io.IOException
if (!(endpoint instanceof UnixSocketAddress)) {
throw new IOException("Invalid address");
}
UnixSocketAddress addr = (UnixSocketAddress) endpoint;
localAddress = (UnixSocketAddress) endpoint;

if (Native.bind(fd, addr.getStruct(), addr.length()) < 0) {
throw new IOException("bind failed: " + Native.getLastErrorString());
if (Native.bind(fd, localAddress.getStruct(), localAddress.length()) < 0) {
throw new IOException(Native.getLastErrorString());
}

if (Native.listen(fd, backlog) < 0) {
throw new IOException("listen failed: " + Native.getLastErrorString());
throw new IOException(Native.getLastErrorString());
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/jnr/unixsocket/UnixServerSocketChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public UnixSocketChannel accept() throws IOException {
public final UnixServerSocket socket() {
return socket;
}

public final UnixSocketAddress getRemoteSocketAddress() {
return null;
}

public final UnixSocketAddress getLocalSocketAddress() {
return socket.localAddress;
}
}
16 changes: 15 additions & 1 deletion src/main/java/jnr/unixsocket/UnixSocketAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class UnixSocketAddress extends java.net.SocketAddress {
public UnixSocketAddress(java.io.File path) {
address = SockAddrUnix.create();
address.setFamily(ProtocolFamily.PF_UNIX);
address.setPath(path.getAbsolutePath());
address.setPath(path.getPath());
}

SockAddrUnix getStruct() {
Expand All @@ -42,8 +42,22 @@ int length() {
return address.length();
}

public String path() {
return address.getPath();
}

@Override
public String toString() {
return "[family=" + address.getFamily() + " path=" + address.getPath() + "]";
}

@Override
public boolean equals(Object _other) {
if (!(_other instanceof UnixSocketAddress)) return false;

UnixSocketAddress other = (UnixSocketAddress)_other;

return address.getFamily() == other.address.getFamily() &&
address.getPath().equals(other.address.getPath());
}
}
28 changes: 27 additions & 1 deletion src/main/java/jnr/unixsocket/UnixSocketChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public static final UnixSocketChannel open() throws IOException {

public static final UnixSocketChannel open(UnixSocketAddress remote) throws IOException {
UnixSocketChannel channel = new UnixSocketChannel();
channel.connect(remote);
try {
channel.connect(remote);
} catch (IOException e) {
channel.close();
throw e;
}
return channel;
}

Expand All @@ -64,6 +69,27 @@ public static final UnixSocketChannel[] pair() throws IOException {
};
}

/**
* Create a UnixSocketChannel to wrap an existing file descriptor (presumably itself a UNIX socket).
*
* @param fd the file descriptor to wrap
* @return the new UnixSocketChannel instance
*/
public static final UnixSocketChannel fromFD(int fd) {
return fromFD(fd, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}

/**
* Create a UnixSocketChannel to wrap an existing file descriptor (presumably itself a UNIX socket).
*
* @param fd the file descriptor to wrap
* @param ops the SelectionKey operations the socket supports
* @return the new UnixSocketChannel instance
*/
public static final UnixSocketChannel fromFD(int fd, int ops) {
return new UnixSocketChannel(fd, ops);
}

private UnixSocketChannel() throws IOException {
super(Native.socket(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0),
SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);
Expand Down
Loading

0 comments on commit 52cd420

Please sign in to comment.