-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from DataDog/landerson/named-pipes
Add named pipe support for windows
- Loading branch information
Showing
21 changed files
with
535 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.nio.channels.WritableByteChannel; | ||
|
||
public interface ClientChannel extends WritableByteChannel { | ||
String getTransportType(); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/timgroup/statsd/DatagramClientChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.io.IOException; | ||
import java.net.SocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.DatagramChannel; | ||
|
||
public class DatagramClientChannel implements ClientChannel { | ||
protected final DatagramChannel delegate; | ||
private final SocketAddress address; | ||
|
||
/** | ||
* Creates a new DatagramClientChannel using the default DatagramChannel. | ||
* @param address Address to connect the channel to | ||
* @throws IOException if an I/O error occurs | ||
*/ | ||
public DatagramClientChannel(SocketAddress address) throws IOException { | ||
this(DatagramChannel.open(), address); | ||
} | ||
|
||
/** | ||
* Creates a new DatagramClientChannel that wraps the delegate. | ||
* @param delegate Implementation this instance wraps | ||
* @param address Address to connect the channel to | ||
*/ | ||
public DatagramClientChannel(DatagramChannel delegate, SocketAddress address) { | ||
this.delegate = delegate; | ||
this.address = address; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return delegate.isOpen(); | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src) throws IOException { | ||
return delegate.send(src, address); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
delegate.close(); | ||
} | ||
|
||
@Override | ||
public String getTransportType() { | ||
return "udp"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + getTransportType() + "] " + address; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/timgroup/statsd/NamedPipeClientChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
|
||
public class NamedPipeClientChannel implements ClientChannel { | ||
private final RandomAccessFile randomAccessFile; | ||
private final FileChannel fileChannel; | ||
private final String pipe; | ||
|
||
/** | ||
* Creates a new NamedPipeClientChannel with the given address. | ||
* | ||
* @param address Location of named pipe | ||
* @throws FileNotFoundException if pipe does not exist | ||
*/ | ||
public NamedPipeClientChannel(NamedPipeSocketAddress address) throws FileNotFoundException { | ||
pipe = address.getPipe(); | ||
randomAccessFile = new RandomAccessFile(pipe, "rw"); | ||
fileChannel = randomAccessFile.getChannel(); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return fileChannel.isOpen(); | ||
} | ||
|
||
@Override | ||
public int write(ByteBuffer src) throws IOException { | ||
return fileChannel.write(src); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// closing the file also closes the channel | ||
randomAccessFile.close(); | ||
} | ||
|
||
@Override | ||
public String getTransportType() { | ||
return "namedpipe"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return pipe; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/timgroup/statsd/NamedPipeSocketAddress.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.net.SocketAddress; | ||
|
||
public class NamedPipeSocketAddress extends SocketAddress { | ||
private static final String NAMED_PIPE_PREFIX = "\\\\.\\pipe\\"; | ||
private final String pipe; | ||
|
||
public NamedPipeSocketAddress(String pipeName) { | ||
this.pipe = normalizePipeName(pipeName); | ||
} | ||
|
||
public String getPipe() { | ||
return pipe; | ||
} | ||
|
||
/** | ||
* A normalized version of the pipe name that includes the `\\.\pipe\` prefix | ||
*/ | ||
static String normalizePipeName(String pipeName) { | ||
if (pipeName.startsWith(NAMED_PIPE_PREFIX)) { | ||
return pipeName; | ||
} else { | ||
return NAMED_PIPE_PREFIX + pipeName; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.