tblSession.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
int idx = tblSession.getSelectionIndex();
IoSession session = (IoSession)tblSession.getItem(idx).getData("session");
currSession = session;
lblInfo.setText("Session ID:" + session.getId());
txtSessionID.setText("" + session.getId());
byte [] bytes = getRecvData();
String charset = cboCharsetOfRecv.getText();
String recvData = "";
try {
recvData = new String(bytes, charset);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
txtRecv.setText(recvData);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
DropTargetListener dtl = new DropTargetListener() {
@Override
public void dropAccept(DropTargetEvent event) {
}
@Override
public void drop(DropTargetEvent event) {
if (FileTransfer.getInstance().isSupportedType(
event.currentDataType)) {
String[] files = (String[]) event.data;
//for (int i = 0; i < files.length; i++) {
for (int i = 0; i < 1; i++) {
File fd = new File(files[i]);
FileInputStream is = null;
try {
is = new FileInputStream(fd);
byte byteData[] = new byte[(int) fd.length()];
is.read(byteData);
IoBuffer buff = IoBuffer.allocate(byteData.length);
buff.put(byteData);
buff.flip();
currSession.write(buff);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
@Override
public void dragOver(DropTargetEvent event) {
}
@Override
public void dragOperationChanged(DropTargetEvent event) {
}
@Override
public void dragLeave(DropTargetEvent event) {
}
@Override
public void dragEnter(DropTargetEvent event) {
if (event.detail == DND.DROP_DEFAULT) {
if ((event.operations & DND.DROP_COPY) != 0) {
event.detail = DND.DROP_COPY;
} else {
event.detail = DND.DROP_NONE;
}
}
}
};
DropTarget dropTarget = new DropTarget(txtSend, DND.DROP_COPY
| DND.DROP_DEFAULT);
Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };
dropTarget.setTransfer(transfers);
dropTarget.addDropListener(dtl);
client.setHandler(new IoHandler() {
@Override
public void sessionOpened(IoSession session) throws Exception {
}
@Override
public void sessionIdle(IoSession session, IdleStatus idle) throws Exception {
}
@Override
public void sessionCreated(final IoSession session) throws Exception {
long id = session.getId();
System.out.println("[id:" + id + "]sessionCreated");
session.setAttribute("buffer", IoBuffer.allocate(1024));
parent.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
InetSocketAddress remoteAddr = (InetSocketAddress)session.getRemoteAddress();
InetSocketAddress localAddr = (InetSocketAddress)session.getLocalAddress();
String sessionID = "" + session.getId();
String remoteIP = remoteAddr.getAddress().getHostAddress();
String remotePort = "" + remoteAddr.getPort();
String localPort = "" + localAddr.getPort();
TableItem item = new TableItem(tblSession, SWT.NONE);
item.setText(0, sessionID);
item.setText(1, remoteIP);
item.setText(2, remotePort);
item.setText(3, localPort);
item.setData("session", session);
}
});
}
@Override
public void sessionClosed(final IoSession session) throws Exception {
long id = session.getId();
System.out.println("[id:" + id + "]sessionClosed");
parent.getDisplay().syncExec(new Runnable() {
@Override
public void run() {
TableItem [] items = tblSession.getItems();
for(int i=0; i<items.length; i++){
TableItem item = items[i];
IoSession sessionOfTable = (IoSession)item.getData("session");
if(session.getId() == sessionOfTable.getId()){
tblSession.remove(i);
break;
}
}