-
Notifications
You must be signed in to change notification settings - Fork 2k
Web socket support. #19
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.examples; | ||
|
||
import io.kubernetes.client.ApiClient; | ||
import io.kubernetes.client.ApiException; | ||
import io.kubernetes.client.util.Config; | ||
import io.kubernetes.client.util.WebSockets; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
|
||
public class WebSocketsExample { | ||
public static void main(String... args) throws ApiException, IOException { | ||
final ApiClient client = Config.defaultClient(); | ||
WebSockets.stream(args[0], "GET", client, new WebSockets.SocketListener() { | ||
public void open() {} | ||
public void close() { | ||
// Trigger shutdown of the dispatcher's executor so this process can exit cleanly. | ||
client.getHttpClient().getDispatcher().getExecutorService().shutdown(); | ||
} | ||
public void bytesMessage(InputStream is) {} | ||
public void textMessage(Reader in) { | ||
try { | ||
BufferedReader reader = new BufferedReader(in); | ||
for (String line = reader.readLine(); line != null; line = reader.readLine()) { | ||
System.out.println(line); | ||
} | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
.gitignore | ||
git_push.sh | ||
# Remove this once swagger-codegen 2.2.3 is released and we update. | ||
# We want https://github.com/swagger-api/swagger-codegen/pull/5629 | ||
# in the release. | ||
# Verify the following PRs are in the release: | ||
# * https://github.com/swagger-api/swagger-codegen/pull/5629 | ||
# * https://github.com/swagger-api/swagger-codegen/pull/5648 | ||
src/main/java/io/kubernetes/client/ApiClient.java | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package io.kubernetes.client.util; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No license header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please add license header to all files. we need a check for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
import io.kubernetes.client.ApiClient; | ||
import io.kubernetes.client.ApiException; | ||
import io.kubernetes.client.Pair; | ||
|
||
import com.google.common.net.HttpHeaders; | ||
import com.squareup.okhttp.MediaType; | ||
import com.squareup.okhttp.Request; | ||
import com.squareup.okhttp.Response; | ||
import com.squareup.okhttp.ResponseBody; | ||
import com.squareup.okhttp.ws.WebSocket; | ||
import com.squareup.okhttp.ws.WebSocketCall; | ||
import com.squareup.okhttp.ws.WebSocketListener; | ||
import okio.Buffer; | ||
|
||
import static com.squareup.okhttp.ws.WebSocket.BINARY; | ||
import static com.squareup.okhttp.ws.WebSocket.TEXT; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Reader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class WebSockets { | ||
public static final String V4_STREAM_PROTOCOL = "v4.channel.k8s.io"; | ||
public static final String V3_STREAM_PROTOCOL = "v3.channel.k8s.io"; | ||
public static final String V2_STREAM_PROTOCOL = "v2.channel.k8s.io"; | ||
public static final String V1_STREAM_PROTOCOL = "channel.k8s.io"; | ||
public static final String STREAM_PROTOCOL_HEADER = "X-Stream-Protocol-Version"; | ||
public static final String SPDY_3_1 = "SPDY/3.1"; | ||
|
||
/** | ||
* A simple interface for a listener on a web socket | ||
*/ | ||
public interface SocketListener { | ||
/** | ||
* Called when the socket is opened | ||
*/ | ||
public void open(); | ||
|
||
/** | ||
* Callled when a binary media type message is received | ||
* @param in The input stream containing the binary data | ||
*/ | ||
public void bytesMessage(InputStream in); | ||
|
||
/** | ||
* Called when a text media type message is received | ||
* @param in The character stream containing the message | ||
*/ | ||
public void textMessage(Reader in); | ||
|
||
/** | ||
* Called when the stream is closed. | ||
*/ | ||
public void close(); | ||
} | ||
|
||
/** | ||
* Create a new WebSocket stream | ||
* @param path The HTTP Path to request from the API | ||
* @param method The HTTP method to use for the call | ||
* @param client The ApiClient for communicating with the API | ||
* @param listener The socket listener to handle socket events | ||
*/ | ||
public static void stream(String path, String method, ApiClient client, SocketListener listener) throws ApiException, IOException { | ||
HashMap<String, String> headers = new HashMap<String, String>(); | ||
String allProtocols = String.format("%s,%s,%s,%s", V4_STREAM_PROTOCOL, V3_STREAM_PROTOCOL, V2_STREAM_PROTOCOL, V1_STREAM_PROTOCOL); | ||
headers.put(STREAM_PROTOCOL_HEADER, allProtocols); | ||
headers.put(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE); | ||
headers.put(HttpHeaders.UPGRADE, SPDY_3_1); | ||
|
||
Request request = client.buildRequest(path, method, new ArrayList<Pair>(), null, headers, new HashMap<String, Object>(), new String[0], null); | ||
WebSocketCall.create(client.getHttpClient(), request).enqueue(new Listener(listener)); | ||
} | ||
|
||
private static class Listener implements WebSocketListener { | ||
private SocketListener listener; | ||
|
||
public Listener(SocketListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void onOpen(WebSocket webSocket, Response response) { | ||
listener.open(); | ||
} | ||
|
||
@Override | ||
public void onMessage(ResponseBody body) throws IOException { | ||
if (body.contentType() == TEXT) { | ||
listener.bytesMessage(body.byteStream()); | ||
} else if (body.contentType() == BINARY) { | ||
listener.textMessage(body.charStream()); | ||
} | ||
body.close(); | ||
} | ||
|
||
@Override | ||
public void onPong(Buffer payload) { | ||
} | ||
|
||
@Override | ||
public void onClose(int code, String reason) { | ||
listener.close(); | ||
} | ||
|
||
@Override | ||
public void onFailure(IOException e, Response res) { | ||
e.printStackTrace(); | ||
listener.close(); | ||
} | ||
} | ||
} |
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.
new line at the end of the file.
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.
done.