-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add named pipe support for windows #169
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
26ab211
Initial commit
randomanderson 4487e13
Must open file for "rw"
randomanderson 7e0cc1b
Add named pipe tests
randomanderson aaca3ce
Add windows build
randomanderson b2f8caa
Install maven in windows build
randomanderson a15f30d
Send vs Write
randomanderson cb66621
Don't add blank messages
randomanderson d7743c5
UDS test doesn't work on windows
randomanderson 544f480
Cleanup imports add logging
randomanderson dcfe5aa
Handle case where client connects first
randomanderson 61ade20
Remove unused imports
randomanderson fb4cd25
Create a UnixdatagramClientChannel to encapsulate logic
randomanderson d0b85b3
PR feedback
randomanderson 5f7ddbe
Checkstyle
randomanderson 6baacb5
Add environment variable for named pipe
randomanderson 176721b
Merge branch 'master' into landerson/named-pipes
randomanderson 463c935
fix merge issue
randomanderson 954cde5
Fix javadoc warnings
randomanderson 5249a2e
Use single argument constructor
randomanderson 7ba4731
Attempt to fix racy test
randomanderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work?
And then DatagramChannel import can be removed, and maybe 2-argument constructor too (other client channel impls don't have one, so I guess we don't need one there too).