PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/prototypes/ocp-agent/src/main/java/org/ocpteam/protocol/ocp/OCPProtocol.java

https://code.google.com/
Java | 264 lines | 209 code | 40 blank | 15 comment | 31 complexity | f9441089f19e73cc2c50bc521f3e28e6 MD5 | raw file
  1. package org.ocpteam.protocol.ocp;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.EOFException;
  7. import java.io.Serializable;
  8. import java.net.Socket;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import org.ocpteam.component.Agent;
  12. import org.ocpteam.component.Protocol;
  13. import org.ocpteam.entity.Address;
  14. import org.ocpteam.entity.InputMessage;
  15. import org.ocpteam.entity.Session;
  16. import org.ocpteam.misc.Id;
  17. import org.ocpteam.misc.JLG;
  18. public class OCPProtocol extends Protocol {
  19. public OCPProtocol() throws Exception {
  20. super();
  21. }
  22. @Override
  23. public OCPDataSource ds() {
  24. return (OCPDataSource) super.ds();
  25. }
  26. public static final int SEPARATOR = 0;
  27. public static final String NODE_ID = "node_id";
  28. public static final String GENERATE_CAPTCHA = "generate_captcha";
  29. public static final String CREATE_USER = "create_user";
  30. public static final String CREATE_OBJECT = "create_object";
  31. public static final String GET_USER = "get_user";
  32. public static final String GET_ADDRESS = "get_address";
  33. public static final String REMOVE_ADDRESS = "remove_address";
  34. public static final byte[] SUCCESS = "0".getBytes();
  35. public static final byte[] ERROR = "ERROR".getBytes();
  36. public static final byte[] ADDRESS_NOT_FOUND = "not_found".getBytes();
  37. private OCPAgent agent;
  38. public OCPAgent getAgent() {
  39. if (this.agent == null) {
  40. this.agent = (OCPAgent) ds().getComponent(Agent.class);
  41. }
  42. return this.agent;
  43. }
  44. @Override
  45. public void process(Socket clientSocket) throws Exception {
  46. DataInputStream in = new DataInputStream(clientSocket.getInputStream());
  47. DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
  48. JLG.debug("about to read object");
  49. Serializable o = getStreamSerializer().readObject(in);
  50. if (o instanceof InputMessage) {
  51. InputMessage inputMessage = (InputMessage) o;
  52. Session session = new Session(ds(), clientSocket);
  53. JLG.debug("inputMessage.transid=" + inputMessage.transid);
  54. inputMessage.transaction = getMap().get(inputMessage.transid);
  55. if (inputMessage.transaction == null) {
  56. throw new Exception("transaction unknown: " + inputMessage.transid);
  57. }
  58. Serializable s = inputMessage.transaction.run(session,
  59. inputMessage.objects);
  60. getStreamSerializer().writeObject(out, s);
  61. }
  62. if (o instanceof byte[]) {
  63. byte[] input = (byte[]) o;
  64. byte[] output = oldprocess(input, clientSocket);
  65. getStreamSerializer().writeObject(out, output);
  66. }
  67. }
  68. public byte[] oldprocess(byte[] input, Socket clientSocket) throws Exception {
  69. getAgent();
  70. String request = new String(input);
  71. Iterator<byte[]> it = iterator(input);
  72. if (request.equalsIgnoreCase(NODE_ID)) {
  73. try {
  74. return agent.generateId().getBytes();
  75. } catch (Exception e) {
  76. return ERROR;
  77. }
  78. }
  79. if (request.startsWith(GET_USER)) {
  80. try {
  81. // Protocol.GET_USER + ":" + key
  82. Key key = new Key(new Id(it.next()));
  83. Content data = agent.get(key);
  84. if (data == null) {
  85. throw new Exception("Cannot find user for key = " + key);
  86. }
  87. // serialize it.
  88. return data.getContent();
  89. } catch (Exception e) {
  90. JLG.error(e);
  91. return ERROR;
  92. }
  93. }
  94. if (request.startsWith(GET_ADDRESS)) {
  95. try {
  96. // Protocol.GET_ADDRESS + ":" + address
  97. Address address = new Address(it.next());
  98. Content data = agent.get(address);
  99. if (data == null) {
  100. return ADDRESS_NOT_FOUND;
  101. }
  102. // serialize it.
  103. return ds().serializer.serialize(data);
  104. } catch (Exception e) {
  105. JLG.error(e);
  106. return ERROR;
  107. }
  108. }
  109. if (request.equalsIgnoreCase(GENERATE_CAPTCHA)) {
  110. try {
  111. Captcha captcha = new Captcha(agent);
  112. // serialize it.
  113. return JLG.serialize(captcha);
  114. } catch (Exception e) {
  115. return ERROR;
  116. }
  117. }
  118. if (request.startsWith(CREATE_USER)) {
  119. try {
  120. // String request = Protocol.message(Protocol.CREATE_USER, data,
  121. // link, captcha,
  122. // answer);
  123. // TODO handle when user already exists.
  124. ObjectData data = (ObjectData) ds().serializer.deserialize(it.next());
  125. Link link = (Link) ds().serializer.deserialize(it.next());
  126. Captcha captcha = (Captcha) ds().serializer.deserialize(it.next());
  127. String answer = new String(it.next());
  128. captcha.check(agent, answer);
  129. Key key = link.getKey();
  130. Key targetKey = link.getTargetKey();
  131. if (agent.exists(key) || agent.exists(targetKey)) {
  132. throw new Exception(
  133. "it seems that the user already exists.");
  134. }
  135. agent.setWithLink(null, data, link);
  136. return SUCCESS;
  137. } catch (Exception e) {
  138. JLG.error(e);
  139. return ERROR;
  140. }
  141. }
  142. if (request.startsWith(CREATE_OBJECT)) {
  143. try {
  144. // String request = Protocol.CREATE_OBJECT + ":" + address + ":"
  145. // + JLG.serialize(content);
  146. Address address = new Address(it.next());
  147. Content content = (Content) JLG.deserialize(it.next());
  148. agent.store(address, content);
  149. // serialize it.
  150. return SUCCESS;
  151. } catch (Exception e) {
  152. JLG.error(e);
  153. return ERROR;
  154. }
  155. }
  156. if (request.startsWith(REMOVE_ADDRESS)) {
  157. try {
  158. Address address = (Address) JLG.deserialize(it.next());
  159. byte[] addressSignature = it.next();
  160. agent.remove(address, addressSignature);
  161. // serialize it.
  162. return SUCCESS;
  163. } catch (Exception e) {
  164. JLG.error(e);
  165. return ERROR;
  166. }
  167. }
  168. throw new Exception();
  169. //return "ERROR:OCP message not understood".getBytes();
  170. }
  171. public static Iterator<byte[]> iterator(byte[] input) {
  172. LinkedList<byte[]> list = new LinkedList<byte[]>();
  173. try {
  174. ByteArrayInputStream bis = new ByteArrayInputStream(input);
  175. DataInputStream dis = new DataInputStream(bis);
  176. int b = -2;
  177. while ((b != SEPARATOR) && (b != -1)) {
  178. b = dis.read();
  179. }
  180. while (true) {
  181. try {
  182. int length = dis.readInt();
  183. byte[] serialized = new byte[length];
  184. dis.read(serialized, 0, length);
  185. list.addLast(serialized);
  186. } catch (EOFException e) {
  187. break;
  188. }
  189. }
  190. dis.close();
  191. bis.close();
  192. } catch (Exception e) {
  193. // TODO: handle exception
  194. }
  195. return list.iterator();
  196. }
  197. public static byte[] hasBeenDetached(OCPContact contact) {
  198. return ("INFORM_DETACH:" + contact.getName()).getBytes();
  199. }
  200. public static byte[] message(String function, Object... objects)
  201. throws Exception {
  202. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  203. DataOutputStream dos = new DataOutputStream(baos);
  204. dos.write(function.getBytes());
  205. dos.write(SEPARATOR);
  206. for (int i = 0; i < objects.length; i++) {
  207. byte[] serialized = null;
  208. if (objects[i].getClass() == byte[].class) {
  209. serialized = (byte[]) objects[i];
  210. } else if (objects[i].getClass() == String.class) {
  211. serialized = ((String) objects[i]).getBytes();
  212. } else {
  213. serialized = JLG.serialize((Serializable) objects[i]);
  214. }
  215. dos.writeInt(serialized.length);
  216. dos.write(serialized);
  217. }
  218. dos.flush();
  219. byte[] result = baos.toByteArray();
  220. dos.close();
  221. baos.close();
  222. return result;
  223. }
  224. }