Skip to content

Commit

Permalink
Merge pull request #25 from ngrok/nikolay/session-close-tunnel
Browse files Browse the repository at this point in the history
Expose session close tunnel api
  • Loading branch information
nikolay-ngrok committed Sep 8, 2023
2 parents 606b630 + 08df87d commit 9a71a31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ngrok-java-native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,17 @@ impl<'local> com_ngrok::NativeSessionRs<'local> for NativeSessionRsImpl<'local>
}
}

fn close_tunnel(
&self,
this: ComNgrokNativeSession<'local>,
tunnel_id: String,
) -> Result<(), jaffi_support::Error<IOExceptionErr>> {
let rt = RT.get().expect("runtime not initialized");

let sess: MutexGuard<Session> = self.get_native(this);
rt.block_on(sess.close_tunnel(tunnel_id)).map_err(io_exc)
}

fn close(
&self,
this: ComNgrokNativeSession<'local>,
Expand Down
9 changes: 9 additions & 0 deletions ngrok-java-native/src/main/java/com/ngrok/NativeSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ public String getMetadata() {
*/
public native NativeLabeledTunnel labeledTunnel(NativeLabeledTunnel.Builder builder) throws IOException;

/**
* Closes the native tunnel by ID in this session.
*
* @param tunnelId the ID of the tunnel to close
* @throws IOException if an I/O error occurs
*/
@Override
public native void closeTunnel(String tunnelId) throws IOException;

/**
* Closes the native session.
*
Expand Down
9 changes: 9 additions & 0 deletions ngrok-java-native/src/test/java/com/ngrok/ForwardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public void testForward() throws Exception {
var tunnel = session.httpTunnel(new HttpTunnel.Builder().domain("ngrok-java-test.ngrok.io"));
assertNotNull(tunnel);

new Thread(() -> {
try {
Thread.sleep(10000);
session.closeTunnel(tunnel.getId());
} catch(Throwable th) {
th.printStackTrace();
}
}).start();

tunnel.forwardTcp("127.0.0.1:8000");
assertTrue(true);
}
Expand Down
8 changes: 8 additions & 0 deletions ngrok-java/src/main/java/com/ngrok/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public default LabeledTunnel labeledTunnel() throws IOException {
*/
public LabeledTunnel labeledTunnel(LabeledTunnel.Builder builder) throws IOException;

/**
* Closes a tunnel by its ID.
*
* @param tunnelId the id of the tunnel to close
* @throws IOException if an I/O error occurs during tunnel close
*/
public void closeTunnel(String tunnelId) throws IOException;

/**
* Configures a function which is called when the ngrok service requests that this {@link Session} stops.
* Your application may choose to interpret this callback as a request to terminate the {@link Session} or the entire process.
Expand Down

0 comments on commit 9a71a31

Please sign in to comment.