Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.my.testprog.clientsocket;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.net.SocketAddress;
- import java.net.SocketTimeoutException;
- import javax.json.JsonArray;
- import javax.swing.SortingFocusTraversalPolicy;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import net.minidev.json.JSONArray;
- import net.minidev.json.JSONObject;
- public class ClientSocket {
- private String hostName;
- private int port;
- public ClientSocket(String hostName, int port) {
- this.hostName = hostName;
- this.port = port;
- }
- /**
- * Open a socket connection to the given server on the given port. This
- * method currently sets the socket timeout value to 10 seconds. (A second
- * version of this method could allow the user to specify this timeout.)
- */
- private Socket openSocket(String server, int port) throws Exception {
- Socket socket;
- // create a socket with a timeout
- try {
- InetAddress inteAddress = InetAddress.getByName(server);
- SocketAddress socketAddress = new InetSocketAddress(inteAddress, port);
- // create a socket
- socket = new Socket();
- // this method will block no more than timeout ms.
- socket.connect(socketAddress);
- return socket;
- } catch (SocketTimeoutException ste) {
- System.err.println("Timed out waiting for the socket.");
- ste.printStackTrace();
- throw ste;
- }
- }
- private void writeToSocket(Socket socket, String writeTo) throws Exception {
- try {
- // write text to the socket
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
- bufferedWriter.write(writeTo);
- bufferedWriter.flush();
- } catch (IOException e) {
- e.printStackTrace();
- throw e;
- }
- }
- private String readFromSocket(Socket socket) throws Exception {
- // memory buffer for the socket data.
- byte[] bytes = new byte[4096];
- int len;
- while (true) {
- // while the socket has more data (ie: not closed)
- while ((len = socket.getInputStream().read(bytes)) > 0) {
- // convert the bytes to a string.
- String message = new String(bytes, 0, len);
- System.out.println("Received data from client: " + message + "\n");
- processMessage(socket, message);
- for (int j = 0; j < 4096; j++) {
- bytes[j] = 0;
- }
- }
- }
- }
- private void processMessage(Socket socket, String message) {
- ObjectMapper mapper = new ObjectMapper();
- try {
- JsonNode jsonNode = mapper.readTree(message);
- String response = "";
- // Get request method
- String method = jsonNode.get("method").asText();
- if (method.equals("echo")) {
- response = processEchoMethod(socket, jsonNode);
- } else if (method.equals("get_schema")) {
- response = processGetSchemaMethod(socket, jsonNode);
- } else if (method.equals("monitor")) {
- response = processMonitorMethod(socket, jsonNode);
- }
- // Send the response message to the server
- try {
- writeToSocket(socket, response);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private String processMonitorMethod(Socket socket, JsonNode jsonNode) {
- String monitorResponse = "\"error\":null,\"result\":{\"Physical_Port\":{\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\":{\"new\":{\"_uuid\":[\"uuid\",\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\"],\"vlan_stats\":[\"map\",[]],\"vlan_bindings\":[\"map\",[]],\"name\":\"po100\",\"description\":\"\",\"port_fault_status\":[\"set\",[]]}}},\"Physical_Switch\":{\"d61d43dd-b275-4cd6-aecd-936535e580b3\":{\"new\":{\"_uuid\":[\"uuid\",\"d61d43dd-b275-4cd6-aecd-936535e580b3\"],\"tunnels\":[\"set\",[]],\"ports\":[\"uuid\",\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\"],\"management_ips\":\"10.23.236.51\",\"tunnel_ips\":\"1.1.1.1\",\"name\":\"NSX-1\",\"description\":\"\",\"switch_fault_status\":[\"set\",[]]}}}}";
- String reqUuid = jsonNode.get("id").asText();
- return "{" + "\"id\":\"" + reqUuid + "\"" + "," + monitorResponse + "}";
- }
- private String processGetSchemaMethod(Socket socket, JsonNode jsonNode) {
- String schemaResponse = "\"error\":null,\"result\":{\"name\":\"hardware_vtep\",\"tables\":{\"Arp_Sources_Local\":{\"columns\":{\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"src_mac\":{\"type\":\"string\"}},\"isRoot\":true},\"Arp_Sources_Remote\":{\"columns\":{\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"src_mac\":{\"type\":\"string\"}},\"isRoot\":true},\"Global\":{\"columns\":{\"managers\":{\"type\":{\"key\":{\"refTable\":\"Manager\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}},\"switches\":{\"type\":{\"key\":{\"refTable\":\"Physical_Switch\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}}},\"isRoot\":true,\"maxRows\":1},\"Logical_Binding_Stats\":{\"columns\":{\"bytes_from_local\":{\"type\":\"integer\",\"ephemeral\":true},\"bytes_to_local\":{\"type\":\"integer\",\"ephemeral\":true},\"packets_from_local\":{\"type\":\"integer\",\"ephemeral\":true},\"packets_to_local\":{\"type\":\"integer\",\"ephemeral\":true}}},\"Logical_Router\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"static_routes\":{\"type\":{\"key\":{\"type\":\"string\"},\"value\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"switch_binding\":{\"type\":{\"key\":{\"type\":\"string\"},\"value\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"},\"min\":0,\"max\":\"unlimited\"}}},\"indexes\":[[\"name\"]],\"isRoot\":true},\"Logical_Switch\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"tunnel_key\":{\"type\":{\"key\":\"integer\",\"min\":0,\"max\":1}}},\"indexes\":[[\"name\"]],\"isRoot\":true},\"Manager\":{\"columns\":{\"inactivity_probe\":{\"type\":{\"key\":\"integer\",\"min\":0,\"max\":1}},\"is_connected\":{\"ephemeral\":true,\"type\":\"boolean\"},\"max_backoff\":{\"type\":{\"key\":{\"minInteger\":1000,\"type\":\"integer\"},\"min\":0,\"max\":1}},\"other_config\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"target\":{\"type\":\"string\"}},\"indexes\":[[\"target\"]],\"isRoot\":false},\"Mcast_Macs_Local\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator_set\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator_Set\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Mcast_Macs_Remote\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator_set\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator_Set\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Physical_Locator\":{\"columns\":{\"dst_ip\":{\"mutable\":false,\"type\":\"string\"},\"encapsulation_type\":{\"mutable\":false,\"type\":{\"key\":{\"enum\":[\"set\",[\"vxlan_over_ipv4\"]],\"type\":\"string\"}}}},\"indexes\":[[\"encapsulation_type\",\"dst_ip\"]]},\"Physical_Locator_Set\":{\"columns\":{\"locators\":{\"mutable\":false,\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"},\"min\":1,\"max\":\"unlimited\"}}}},\"Physical_Port\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"port_fault_status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"min\":0,\"max\":\"unlimited\"}},\"vlan_bindings\":{\"type\":{\"key\":{\"maxInteger\":4095,\"minInteger\":0,\"type\":\"integer\"},\"max\":\"unlimited\",\"min\":0,\"value\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}},\"vlan_stats\":{\"type\":{\"key\":{\"maxInteger\":4095,\"minInteger\":0,\"type\":\"integer\"},\"max\":\"unlimited\",\"min\":0,\"value\":{\"refTable\":\"Logical_Binding_Stats\",\"type\":\"uuid\"}},\"ephemeral\":true}}},\"Physical_Switch\":{\"columns\":{\"description\":{\"type\":\"string\"},\"management_ips\":{\"type\":{\"key\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"name\":{\"type\":\"string\"},\"ports\":{\"type\":{\"key\":{\"refTable\":\"Physical_Port\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}},\"switch_fault_status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"min\":0,\"max\":\"unlimited\"}},\"tunnel_ips\":{\"type\":{\"key\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"tunnels\":{\"type\":{\"key\":{\"refTable\":\"Tunnel\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}}},\"indexes\":[[\"name\"]]},\"Tunnel\":{\"columns\":{\"bfd_config_local\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_config_remote\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_params\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_status\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"},\"ephemeral\":true},\"local\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"remote\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}}}},\"Ucast_Macs_Local\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Ucast_Macs_Remote\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true}},\"version\":\"1.3.0\"}";
- String reqUuid = jsonNode.get("id").asText();
- return "{" + "\"id\":\"" + reqUuid + "\"" + "," + schemaResponse + "}";
- }
- private String processEchoMethod(Socket socket, JsonNode jsonNode) {
- // Request: {"id":"5bd2390a-61ea-4449-be84-7e9b3ca83079","method":"echo","params":[]}
- // Resonse: {"result":[],"error":null,"id":"5bd2390a-61ea-4449-be84-7e9b3ca83079"}
- String reqUuid = jsonNode.get("id").asText();
- JSONObject resObj = new JSONObject();
- resObj.put("id", reqUuid);
- resObj.put("result", new JSONArray());
- resObj.put("error", null);
- return resObj.toJSONString();
- }
- // call our constructor to start the program
- public static void main(String[] args) {
- String hostName = "localhost";
- int port = 6640;
- ClientSocket clientSocket = new ClientSocket(hostName, port);
- try {
- // open a socket
- Socket socket = clientSocket.openSocket(hostName, port);
- // Keep reading from the socket.
- clientSocket.readFromSocket(socket);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement