/** Required. First procedure ever called. Adjust to your needs. */ public static void init() { if (false) { net.ddp2p.common.plugin_data.PluginRequest sentMenuItem = new net.ddp2p.common.plugin_data.PluginRequest(); sentMenuItem.type = net.ddp2p.common.plugin_data.PluginRequest.REGISTER_MENU; sentMenuItem.plugin_GID = plugin_GID; sentMenuItem.column = net.ddp2p.common.plugin_data.PluginMenus .COLUMN_MYSELF; // for a new plugin how to decide which col? enqueue(sentMenuItem); } }
/** * @param message * @param peerGID * @param peer * @return the message actually sent (and its sequence numbers) */ public static ChatMessage sendMessage(String message, String peerGID, D_Peer peer) { if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendMessage enter: " + message); if (peer != null && peerGID == null) peerGID = peer.getGID(); // create a hashtable where ht(key=peerGID ) ==> cm (chatMessage) ChannelDataOut channeldata_out = ChannelDataOut.get(peerGID); ChannelDataIn channeldata_in = ChannelDataIn.get(peerGID); if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendMessage got channel out: " + channeldata_out); if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendMessage got channel in: " + channeldata_in); ChatMessage cm = new ChatMessage(); cm.session_id = channeldata_out.getSessionID(); // cm.session_id_ack = cmsg.session_id; cm.message_type = ChatMessage.MT_TEXT; cm.first_in_this_sequence = channeldata_out.getFirstInSequence(); // cmsg.first_in_this_sequence; ChatElem ce = new ChatElem(); ce.type = 0; // name ce.val = Main.name; cm.content = new ArrayList<ChatElem>(); cm.content.add(ce); cm.msg = message; // first time message to this peer cm.session_id_ack = channeldata_in.getSessionID(); cm.last_acknowledged_in_sequence = channeldata_in.getLastInSequence(); cm.received_out_of_sequence = channeldata_in.getOutOfSequence(); cm.sequence = channeldata_out.getNextSequence(); // msgTrackingSend.put(peer_GID, cm); // only store the last message info for each peer // save to DB for history ( peer_GID:sender(myself): Date/Time : msg as object ) if (DEBUG) System.out.println("PLUGIN CHAT: Main: send: " + cm); // confirm the sending into the GUI receiver.addSentMessage(cm, peerGID, channeldata_out); byte[] _msg = cm.encode(); net.ddp2p.common.plugin_data.PluginRequest envelope = new net.ddp2p.common.plugin_data.PluginRequest(); envelope.msg = _msg; envelope.peer_GID = peerGID; // destination envelope.type = net.ddp2p.common.plugin_data.PluginRequest.MSG; envelope.plugin_GID = (String) Main.getPluginDataHashtable().get("plugin_GID"); if (DEBUG) System.out.println("PLUGIN CHAT: Main: send envelope: " + envelope); Main.enqueue(envelope); if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendMessage exit"); return cm; }
/** Called from outside: */ public static void handleReceivedMessage(byte[] msg, String peer_GID) { // boolean DEBUG = true; if (DEBUG) System.out.println( "PLUGIN CHAT: From: " + peer_GID + " got " + net.ddp2p.common.plugin_data.PluginRequest.byteToHex(msg, ":")); ChatMessage cmsg = null; try { synchronized (monitor) { cmsg = new ChatMessage().decode(new Decoder(msg)); if (DEBUG) System.out.println("PLUGIN CHAT: got msg: " + cmsg); // extract acknowledgment piggybacked in messages ChannelDataOut channeldata_out = ChannelDataOut.get(peer_GID); receiver.confirmMessages( cmsg.last_acknowledged_in_sequence, cmsg.received_out_of_sequence, peer_GID, cmsg.session_id_ack, channeldata_out); if (cmsg.message_type != ChatMessage.MT_EMPTY) forwardMsgToReceiverAndSendAckToSender(cmsg, peer_GID); } } catch (ASN1DecoderFail e) { e.printStackTrace(); return; } }
/** * Sends an acknowledgment for cmsg. Not setting a sequence number, since the ack message is * empty. * * @param cmsg * @param peer_GID */ private static void sendAckMsg(ChannelDataIn channeldata_in, ChatMessage cmsg, String peer_GID) { // boolean DEBUG = true; if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendAckMsg: enter"); ChannelDataOut cd = ChannelDataOut.get(peer_GID); ChatMessage cm = new ChatMessage(); cm.message_type = ChatMessage .MT_EMPTY; // just to inform session_id, session_id_ack, first_in_this_sequence, // last_acknowledged_in_sequence, received_out_of_sequence cm.last_acknowledged_in_sequence = channeldata_in.getLastInSequence(); // cmsg.sequence; cm.received_out_of_sequence = channeldata_in.getOutOfSequence(); cm.session_id_ack = channeldata_in.getSessionID(); // cmsg.session_id; cm.session_id = cd.getSessionID(); cm.first_in_this_sequence = cd.getFirstInSequence(); // cmsg.first_in_this_sequence; // cm.sequence = cd.getNextSequence(); //cmsg.sequence. probably should not be set for empty // messages! ChatElem ce = new ChatElem(); ce.type = 0; // name ce.val = Main.name; cm.content = new ArrayList<ChatElem>(); cm.content.add(ce); // cm.content = null; cm.msg = null; if (DEBUG) System.out.println("PLUGIN CHAT: sendAckMsg: sending " + cm); byte[] _msg = cm.encode(); net.ddp2p.common.plugin_data.PluginRequest envelope = new net.ddp2p.common.plugin_data.PluginRequest(); envelope.msg = _msg; envelope.peer_GID = peer_GID; // destination envelope.type = net.ddp2p.common.plugin_data.PluginRequest.MSG; envelope.plugin_GID = (String) Main.getPluginDataHashtable().get("plugin_GID"); Main.enqueue(envelope); if (DEBUG) System.out.println("PLUGIN CHAT: sendAckMsg: sending env " + envelope); if (DEBUG) System.out.println("PLUGIN CHAT: Main: sendAckMsg: exit"); }