/examples/kilim/examples/Ping.java

http://github.com/kilim/kilim · Java · 147 lines · 110 code · 13 blank · 24 comment · 17 complexity · 33df531e6a463ea8c0712073e5891873 MD5 · raw file

  1. /* Copyright (c) 2006, Sriram Srinivasan
  2. *
  3. * You may distribute this software under the terms of the license
  4. * specified in the file "License"
  5. */
  6. package kilim.examples;
  7. import java.io.EOFException;
  8. import java.io.IOException;
  9. import java.net.InetSocketAddress;
  10. import java.nio.ByteBuffer;
  11. import java.nio.channels.SocketChannel;
  12. import kilim.Pausable;
  13. import kilim.Scheduler;
  14. import kilim.nio.EndPoint;
  15. import kilim.nio.NioSelectorScheduler;
  16. import kilim.nio.SessionTask;
  17. /**
  18. * Example showing kilim's support for NIO.
  19. * Usage: java kilim.examples.Ping -server in one window
  20. * and java kilim.examples.Ping -client in one or more windows.
  21. * The client sends a number of 100 byte packets which are then echoed by the server.
  22. */
  23. public class Ping {
  24. static int PACKET_LEN = 100;
  25. static boolean server = false;
  26. static int port = 7262;
  27. public static void main(String args[]) throws Exception {
  28. if (args.length == 0) {
  29. usage();
  30. } else if (args[0].equalsIgnoreCase("-server")) {
  31. server = true;
  32. } else if (args[0].equalsIgnoreCase("-client")) {
  33. server = false;
  34. } else {
  35. usage();
  36. }
  37. if (args.length > 1)
  38. parsePort(args[1]);
  39. System.out.println("kilim.examples.Ping " + (server ? "-server " : "-client ") + port);
  40. if (server) {
  41. Server.run();
  42. } else {
  43. Client.run();
  44. }
  45. }
  46. /**
  47. * Server is a SessionTask, which means an instance of it is created by the
  48. * NioSelectorScheduler on an incoming connection.
  49. * The task contains an endpoint object, the bridge between the NIO system
  50. * and Kilim's scheduling.
  51. */
  52. public static class Server extends SessionTask {
  53. public static void run() throws IOException {
  54. Scheduler sessionScheduler = Scheduler.getDefaultScheduler(); // The scheduler/thread pool on which all tasks will be run
  55. NioSelectorScheduler nio = new NioSelectorScheduler(); // Starts a single thread that manages the select loop
  56. nio.listen(port, Server.class, sessionScheduler); //
  57. }
  58. @Override
  59. public void execute() throws Pausable, Exception {
  60. System.out.println("[" + this.id + "] Connection rcvd");
  61. try {
  62. while (true) {
  63. EndPoint ep = getEndPoint();
  64. ByteBuffer buf = ByteBuffer.allocate(PACKET_LEN);
  65. buf = ep.fill(buf, PACKET_LEN); // Pauses until at least PACKET_LEN bytes have been rcvd in buf.
  66. System.out.println("[" + this.id + "] Received pkt");
  67. buf.flip();
  68. ep.write(buf);
  69. System.out.println("[" + this.id + "] Echoed pkt");
  70. }
  71. } catch (EOFException eofe) {
  72. System.out.println("[" + this.id + "] Connection terminated");
  73. } catch (IOException ioe) {
  74. System.out.println("[" + this.id + "] IO Exception: " + ioe.getMessage());
  75. }
  76. }
  77. }
  78. /**
  79. * The Client is a conventional Java socket application.
  80. */
  81. public static class Client {
  82. public static void run() throws IOException {
  83. SocketChannel ch = SocketChannel.open(new InetSocketAddress("localhost", port));
  84. // Init ping packet
  85. ByteBuffer bb = ByteBuffer.allocate(PACKET_LEN);
  86. for (int i = 0; i < PACKET_LEN; i++) {
  87. bb.put((byte)i);
  88. }
  89. bb.flip();
  90. // Ping 5 times
  91. for (int i = 0 ; i < 5; i++) {
  92. System.out.print("Ping");
  93. writePkt(ch, bb);
  94. System.out.println(" .. ");
  95. // Read echo
  96. readPkt(ch, bb);
  97. bb.flip();
  98. System.out.println(" reply rcvd");
  99. }
  100. }
  101. private static void readPkt(SocketChannel ch, ByteBuffer bb) throws IOException, EOFException {
  102. int remaining = PACKET_LEN;
  103. bb.clear();
  104. while (remaining > 0) {
  105. int n = ch.read(bb);
  106. if (n == -1) {
  107. ch.close();
  108. throw new EOFException();
  109. }
  110. remaining -= n;
  111. }
  112. }
  113. private static void writePkt(SocketChannel ch, ByteBuffer bb) throws IOException {
  114. // Write packet
  115. int remaining = PACKET_LEN;
  116. while (remaining > 0) {
  117. int n = ch.write(bb);
  118. remaining -= n;
  119. }
  120. }
  121. }
  122. static private void usage() {
  123. System.err.println("Run java kilim.examples.Ping -server [port] in one window");
  124. System.err.println("and kilim.examples.Ping -client [port] in one or more");
  125. System.exit(1);
  126. }
  127. static void parsePort(String portstr) {
  128. try {
  129. port = Integer.parseInt(portstr);
  130. } catch (Exception e) {
  131. usage();
  132. }
  133. }
  134. }