Пример #1
0
 CodeGeneratorResponse generate(CodeGeneratorRequest request) {
   CodeGeneratorResponse.Builder builder = CodeGeneratorResponse.newBuilder();
   for (FileDescriptorProto file : request.getProtoFileList()) {
     String javaFilename = getJavaFilename(file.getPackage(), file.getName());
     for (DescriptorProto msg : file.getMessageTypeList()) {
       for (EnumDescriptorProto en : msg.getEnumTypeList()) {
         File.Builder f = File.newBuilder();
         f.setName(javaFilename);
         f.setInsertionPoint(
             format("enum_scope:%s.%s.%s", file.getPackage(), msg.getName(), en.getName()));
         f.setContent(getEnumVisitor(en));
         builder.addFile(f);
       }
     }
     // insert visitors at outer_class_scope
   }
   return builder.build();
 }
Пример #2
0
  private void testCallsInternal(Configuration conf) throws IOException {
    Server server =
        new RPC.Builder(conf)
            .setProtocol(TestProtocol.class)
            .setInstance(new TestImpl())
            .setBindAddress(ADDRESS)
            .setPort(0)
            .build();
    TestProtocol proxy = null;
    try {
      server.start();

      InetSocketAddress addr = NetUtils.getConnectAddress(server);
      proxy = RPC.getProxy(TestProtocol.class, TestProtocol.versionID, addr, conf);

      proxy.ping();

      String stringResult = proxy.echo("foo");
      assertEquals(stringResult, "foo");

      stringResult = proxy.echo((String) null);
      assertEquals(stringResult, null);

      // Check rpcMetrics
      MetricsRecordBuilder rb = getMetrics(server.rpcMetrics.name());
      assertCounter("RpcProcessingTimeNumOps", 3L, rb);
      assertCounterGt("SentBytes", 0L, rb);
      assertCounterGt("ReceivedBytes", 0L, rb);

      // Number of calls to echo method should be 2
      rb = getMetrics(server.rpcDetailedMetrics.name());
      assertCounter("EchoNumOps", 2L, rb);

      // Number of calls to ping method should be 1
      assertCounter("PingNumOps", 1L, rb);

      String[] stringResults = proxy.echo(new String[] {"foo", "bar"});
      assertTrue(Arrays.equals(stringResults, new String[] {"foo", "bar"}));

      stringResults = proxy.echo((String[]) null);
      assertTrue(Arrays.equals(stringResults, null));

      UTF8 utf8Result = (UTF8) proxy.echo(new UTF8("hello world"));
      assertEquals(utf8Result, new UTF8("hello world"));

      utf8Result = (UTF8) proxy.echo((UTF8) null);
      assertEquals(utf8Result, null);

      int intResult = proxy.add(1, 2);
      assertEquals(intResult, 3);

      intResult = proxy.add(new int[] {1, 2});
      assertEquals(intResult, 3);

      // Test protobufs
      EnumDescriptorProto sendProto = EnumDescriptorProto.newBuilder().setName("test").build();
      EnumDescriptorProto retProto = proxy.exchangeProto(sendProto);
      assertEquals(sendProto, retProto);
      assertNotSame(sendProto, retProto);

      boolean caught = false;
      try {
        proxy.error();
      } catch (IOException e) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Caught " + e);
        }
        caught = true;
      }
      assertTrue(caught);

      proxy.testServerGet();

      // create multiple threads and make them do large data transfers
      System.out.println("Starting multi-threaded RPC test...");
      server.setSocketSendBufSize(1024);
      Thread threadId[] = new Thread[numThreads];
      for (int i = 0; i < numThreads; i++) {
        Transactions trans = new Transactions(proxy, datasize);
        threadId[i] = new Thread(trans, "TransactionThread-" + i);
        threadId[i].start();
      }

      // wait for all transactions to get over
      System.out.println("Waiting for all threads to finish RPCs...");
      for (int i = 0; i < numThreads; i++) {
        try {
          threadId[i].join();
        } catch (InterruptedException e) {
          i--; // retry
        }
      }

    } finally {
      server.stop();
      if (proxy != null) RPC.stopProxy(proxy);
    }
  }
Пример #3
0
 private String getEnumVisitor(EnumDescriptorProto en) {
   return format("// create a %sVisitor", en.getName());
 }