📄 chatpanel.java
字号:
if (peer == null) {
peer = PeerNetwork.createInstance(tbIdentity.getText());
}
if (connected || connectInitiated) {
return;
}
connectInitiated = true;
// we will perform the actual operation in the poll thread
}
private boolean connect() throws Throwable {
connectInitiated = false;
String url = relayHost;
if (DEBUG) {
System.out.println("Connect():Connecting to " + url);
}
try {
long startTime, endTime;
if (QUANTIFY) {
startTime = System.currentTimeMillis();
}
state = peer.connect(url, state);
if (QUANTIFY) {
endTime = System.currentTimeMillis();
System.out.println("connect took " +
Long.toString(endTime-startTime));
}
connected = true;
if (DEBUG) {
System.out.println("Connected " + connected + "...");
}
String chatIdentity = isGroupChat ?
TALKNAME_PREFIX + currentBuddy :
TALKNAME_PREFIX + tbIdentity.getText();
if (PICSHARE_GROUPNAME.equals(currentBuddy)) {
chatIdentity = TALKNAME_PREFIX + PICSHARE_GROUPNAME;
}
String pipeType = isGroupChat ? "JxtaPropagate" : "JxtaUnicast";
String pipeId = null;
if (INSTANTP2P_GROUPNAME.equals(currentBuddy)) {
// listen on myJXTA's well-known pipe id if talking to it
pipeId = INSTANTP2P_PIPEID;
} else if (PICSHARE_GROUPNAME.equals(currentBuddy)) {
// listen on PicShare's well-known pipe id if talking to it
pipeId = PICSHARE_PIPEID;
}
if (DEBUG) {
System.out.println("Listening on " + chatIdentity);
}
peer.listen(chatIdentity, pipeId, pipeType);
int size = buddyList.getItemCount();
for (int i=0; i < size; i++) {
String buddy = buddyList.getItem(i);
peer.search(PeerNetwork.PIPE, TALKNAME_PREFIX + buddy);
}
} catch (Throwable ex) {
showAlert(Dialog.DIALOG_ERROR,
"Connect",
"Error connecting to relay: " + ex.getMessage());
throw ex;
//return false;
}
return true;
}
private void disconnect() {
String chatIdentity = isGroupChat ?
TALKNAME_PREFIX + currentBuddy :
TALKNAME_PREFIX + tbIdentity.getText();
if (PICSHARE_GROUPNAME.equals(currentBuddy)) {
chatIdentity = TALKNAME_PREFIX + PICSHARE_GROUPNAME;
}
String pipeType = isGroupChat ? "JxtaPropagate" : "JxtaUnicast";
String pipeId = null;
if (INSTANTP2P_GROUPNAME.equals(currentBuddy)) {
// close myJXTA's well-known pipe id if talking to it
pipeId = INSTANTP2P_PIPEID;
} else if (PICSHARE_GROUPNAME.equals(currentBuddy)) {
// close on PicShare's well-known pipe id if talking to it
pipeId = PICSHARE_PIPEID;
}
try {
peer.close(chatIdentity, pipeId, pipeType);
connected = false;
if (DEBUG) {
System.out.println("Closed " + chatIdentity);
}
} catch (IOException ex) {
showAlert(Dialog.DIALOG_ERROR,
"Disconnect",
"Error connecting to relay: " + ex.getMessage());
}
}
// a class to increment the value of a Gauge everytime it is run
class Gauge extends Canvas {
private final static int PADDING_X = 2; // for title
private final static int PADDING_Y = 20; // for title
private final static int MAX_BARS = 10; // number of progress bars
private int ticks = MAX_BARS;
private final int height;
private final int width;
private final int barWidth;
private final int offset;
Gauge() {
height = getHeight();
width = getWidth();
barWidth = width/(MAX_BARS*2);
offset = (width - (barWidth*MAX_BARS*2))/2;
}
public void paint (Graphics g){
// Clear up the complete screen when all the bars are drawn
if (ticks == MAX_BARS) {
// initializing to zero as I don't want to compute modulo value
ticks = 0;
// clear up the whole screen
g.clearRect (0, 0, width, height);
// draw TITLE string
g.drawString ("Connecting", PADDING_X, PADDING_Y);
// draw line under the title string
g.drawLine (0, 25, width, 25);
for (int i=0; i<MAX_BARS; i++) {
// draw progress bars - unfilled
g.drawRect (i*2*barWidth+offset, height/2, barWidth, 20);
}
}
// Fill only the required bar
g.fillRect (ticks*2*barWidth+offset, height/2, barWidth, 20);
}
public void processEvent(int type, int param) {
++ticks;
repaint();
}
}
public void run() {
if (DEBUG) {
System.out.println("starting poll thread");
}
while (!stopPolling) {
if (!connected && connectInitiated) {
ShortTimer timer = null;
Gauge updater = new Gauge();
Display.setCurrent(updater);
try {
timer = ShortTimer.getShortTimer(updater, 99, 1000, true);
timer.start();
} catch (Exception e) {
timer.stop();
}
try {
connect();
if (DEBUG) {
System.out.println ("Connected...");
}
} catch (Throwable ex){
showAlert(Dialog.DIALOG_ERROR,
"run",
"Error connecting to relay: " + ex.getMessage());
}
if (sendPending) {
try {
send();
} finally {
sendPending = false;
}
}
Display.setCurrent(mainPanel);
}
try {
poll();
} catch (Throwable t) {
showAlert(Dialog.DIALOG_ERROR,
"Poll",
"Error processing message: " + t.getMessage());
}
try {
// poll interval is specified in seconds
Thread.currentThread().sleep(pollInterval * 1000);
} catch (InterruptedException ignore) {
}
}
if (DEBUG) {
System.out.println("stopped poll thread");
}
}
private void poll() {
if (peer == null || !connected) {
// not yet connected
return;
}
Message msg = null;
try {
long startTime, endTime;
if (QUANTIFY) {
startTime = System.currentTimeMillis();
}
// timeout must not be zero: zero means block forever
if (peer != null) {
msg = peer.poll(1);
}
if (QUANTIFY) {
endTime = System.currentTimeMillis();
System.out.println("poll took " +
Long.toString(endTime-startTime));
}
} catch (IOException ex) {
showAlert(Dialog.DIALOG_ERROR,
"Poll",
"Error polling relay: " + ex.getMessage());
return;
}
if (msg == null) {
return;
}
Element el = null;
String name = null;
String id = null;
for (int i=0; i < msg.getElementCount(); i++) {
el = msg.getElement(i);
if (Message.PROXY_NAME_SPACE.equals(el.getNameSpace())) {
String elementName = el.getName();
if (Message.NAME_TAG.equals(elementName)) {
name = new String(el.getData());
} else if (Message.ID_TAG.equals(elementName)) {
id = new String(el.getData());
}
}
}
if (name != null) {
if (name.indexOf(TALKNAME_PREFIX) >= 0) {
name = name.substring(TALKNAME_PREFIX.length());
}
int size = buddyList.getItemCount();
for (int i=0; i < size; i++) {
String buddy = buddyList.getItem(i);
if (buddy.equals(name) &&
id != null && !"".equals(id)) {
buddyIds.put(name, id);
break;
}
}
}
String sender = null;
String message = null;
String imageCaption = null;
String imageFileName = null;
byte[] imageData = null;
boolean isDisplayable = true;
for (int i=0; i < msg.getElementCount(); i++) {
el = msg.getElement(i);
if ("JxtaTalkSenderName".equals(el.getName())) {
sender = new String(el.getData());
} else if ("JxtaTalkSenderMessage".equals(el.getName())) {
message = new String(el.getData());
} else if ("Caption".equals(el.getName())) {
imageCaption = new String(el.getData());
} else if ("FileName".equals(el.getName())) {
imageFileName = new String(el.getData());
} else if ("DataBlock".equals(el.getName())) {
imageData = el.getData();
isDisplayable = false;
}
if (DEBUG) {
System.out.print(i + " " + el.getName());
if (isDisplayable) {
System.out.print(" " + new String(el.getData()));
}
System.out.println();
}
isDisplayable = true;
}
if (imageData != null) {
String caption =
imageCaption == null ? imageFileName : imageCaption;
//TBD imageCanvas.createImage(imageData, caption, sender);
//TBD display.setCurrent(imageCanvas);
}
if (sender != null && message != null) {
if (sender.indexOf(TALKNAME_PREFIX) >= 0) {
sender = sender.substring(TALKNAME_PREFIX.length());
}
replyBuddy = sender;
String displayedMsg = sender + "> " + message + "\n";
/* TBD this may not be required
// keep the last DEFAULT_SCROLL messages, the rest scroll off
StringItem si = new StringItem(null, displayedMsg);
if (initForm.size() >= DEFAULT_SCROLL) {
initForm.delete(0);
}
*/
tbRcvdMsg.setText(displayedMsg);
}
}
private void showAlert(int type, String title, String message) {
System.out.println ("ALERTS: " + message);
Dialog dialog = new Dialog(type, title);
dialog.setText(message);
dialog.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -