lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2009年01月13日

基于apache mina 2.0.0 M4和google Protocol Buffers 2.0.3的java RPC实例(3.定义RPC请求和应答)

Filed under: Java — 标签:, , , , — lizongbo @ 23:59

接上一篇: http://618119.com/archives/2009/01/13/127.html

google Protocol Buffers RPC的应答包:
[code]
package com.lizongbo.protobufrpc;

import .io.*;
import java.nio.ByteBuffer;

public class ProtobufRPCRequest {
private String serviceName;
private String methodName;
private byte[] requestMessage;
private int reqByteLen;
public String getServiceName() {
return serviceName;
}

public String getMethodName() {
return methodName;
}

public byte[] getRequestMessage() {

return requestMessage;
}

public int getReqByteLen() {
return reqByteLen;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public void setMethodName(String methodName) {
this.methodName = methodName;
}

public void setRequestMessage(byte[] requestMessage) {

this.requestMessage = requestMessage;
}

public void setReqByteLen(int reqByteLen) {
this.reqByteLen = reqByteLen;
}

public void readFrom(byte[] b, int offset) {
System.out.println(“readFrom:” + new String(b));
java.io.ByteArrayInputStream bai = new ByteArrayInputStream(b, offset,
b.length – offset);
DataInputStream buf = new DataInputStream(bai);

try {
// buf.getInt();
int serviceNameLen = buf.readInt();
System.out.println(“serviceNameLen==” + serviceNameLen);
byte bt[] = new byte[serviceNameLen];
buf.read(bt);
serviceName = new String(bt);
System.out.println(“serviceName==” + serviceName);
int methodNameLen = buf.readInt();
System.out.println(“methodNameLen==” + methodNameLen);
bt = new byte[methodNameLen];
buf.read(bt);
methodName = new String(bt);
int requestLen = buf.readInt();
System.out.println(“requestLen==” + requestLen);
requestMessage = new byte[requestLen];
buf.read(requestMessage);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public byte[] toByteArray() {
java.io.ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream out = new
DataOutputStream(bao);

try {
byte[] serviceNameb = serviceName.getBytes();
System.out.println(“serviceNameLen==” + serviceNameb.length);
out.writeInt(serviceNameb.length);
out.write(serviceNameb);
byte[] methodNameb = methodName.getBytes();
System.out.println(“methodNameLen==” + methodNameb.length);
out.writeInt(methodNameb.length);
out.write(methodNameb);
System.out.println(“requestMessageLen==” + requestMessage.length);
out.writeInt(requestMessage.length);
out.write(requestMessage);
byte[] reqByte = bao.toByteArray();
reqByteLen = reqByte.length;
bao = new ByteArrayOutputStream();
out = new DataOutputStream(bao);
System.out.println(“reqByteLen==” + (reqByte.length + 4));
out.writeInt(reqByte.length + 4);
out.write(reqByte);
} catch (IOException ex) {
ex.printStackTrace();
}
return bao.toByteArray();
}

}

[/code]

google Protocol Buffers RPC的应答包:
[code]
package com.lizongbo.protobufrpc;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.io.DataInputStream;
import java.io.ByteArrayInputStream;

public class ProtobufRPCResponse {
private int resByteLen;
private byte[] responseMessage;
public int getResByteLen() {
return resByteLen;
}

public byte[] getResponseMessage() {
return responseMessage;
}

public void setResByteLen(int resByteLen) {
this.resByteLen = resByteLen;
}

public void setResponseMessage(byte[] responseMessage) {
this.responseMessage = responseMessage;
}

public void readFrom(byte[] b, int offset) {

try {//此处不够完善
java.io.ByteArrayInputStream bai = new ByteArrayInputStream(b, offset,
b.length – offset);
DataInputStream buf = new DataInputStream(bai);
resByteLen = buf.readInt();
responseMessage = new byte[resByteLen – 4];
buf.read(responseMessage);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public byte[] toByteArray() {
java.io.ByteArrayOutputStream bao = new ByteArrayOutputStream();
DataOutputStream out = new
DataOutputStream(bao);

try {
out.writeInt(responseMessage.length + 4);
out.write(responseMessage);
} catch (IOException ex) {
ex.printStackTrace();
}
return bao.toByteArray();
}

}

[/code]

没有评论 »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress