Листинг программы(V) (1061334), страница 2
Текст из файла (страница 2)
if ((tempUser != null) && (tempUser.userNA == receivebuffer[2]))
{
String appStr = tempUser.usernick + ">> ";
this.mainForm.txtAreaChat.append(appStr);
}
}
byte[] dataBuf = new byte[15];
String text = "";
byte[] data = new byte[16];
data = this.TranslateSnap(receivebuffer);
for (int index = 0; index < 15; index++)
{
dataBuf[index] = data[index];
}
if (data[15] == (byte)0xE0)
{
text = new String((dataBuf), 0, 15 );
this.mainForm.txtAreaChat.append(text);
}
else if (data[15] == (byte)0xE1)
{
text = new String(dataBuf, 0, 15 );
this.mainForm.txtAreaChat.append(text);
this.mainForm.txtAreaChat.append(" " + '\n');
}
}
else if (receivebuffer[3] == (byte)0xFF)
{
byte[] b_UserName = new byte[10];
byte userNA = receivebuffer[2];
int index = 0;
/*for(int index = 5; index <15; index++)
{
b_UserName[index-5] = receivebuffer[index];
}*/
while (receivebuffer[index+5] != 0)
{
b_UserName[index] = receivebuffer[index+5];
index++;
}
String s_UserName = new String(b_UserName,0,index);
UserId tmpUser = new UserId();
tmpUser.userNA = userNA;
tmpUser.usernick = s_UserName;
//Исправить!!!!!
this.UserIDArray[0] = tmpUser;
this.mainForm.listAreaContacts.add(s_UserName + '\n');
}
}
public byte[] CreateInformSnap(byte[] data)
{
byte[] snap = new byte[21];
byte startByte,stopByte,DA,SA,Type,DATA;
snap[0] = (byte)0xFF;
snap[1] = (byte)0x11;
snap[2] = (byte)this.SourseAddress;
snap[3] = (byte)0xC1;
for (int index = 4; index <20; index++)
{
snap[index] = data[index-4];
}
snap[20] = (byte)0xFF;
return snap;
}
public byte[] CreateCheckSnap(byte[] data)
{
byte[] snap = new byte[21];
byte startByte,stopByte,DA,SA,Type,DATA;
snap[0] = (byte)0xFF;
snap[1] = (byte)0x11;
snap[2] = (byte)this.SourseAddress;
snap[3] = (byte)0xFF;
for (int index = 4; index <20; index++)
{
snap[index] = data[index-4];
}
snap[20] = (byte)0xFF;
return snap;
}
public byte[] TranslateSnap(byte[] buffer)
{
byte[] data = new byte[16];
for (int index = 4; index <20; index++)
{
data[index - 4] = buffer[index];
}
return data;
}
}
4. Прикладной уровень.
import java.applet.Applet;
import java.awt.*;
import java.awt.List;
import java.lang.ref.PhantomReference;
import java.util.*;
import java.awt.event.*;
public class MainForm extends Applet implements ActionListener
{
Button btnAction;
Button btnHistory;
Button btnSend;
Button btnExit;
Button btnConnect;
TextArea txtAreaChat;
TextArea txtAreaContacts;
TextField txtFld;
List listAreaContacts;
Label lblState;
int WIDTH = 9;
int HEIGHT = 8;
//PhisicalLayer PhLayer;
DataLinkLayer DlLayer;
// инициализация апплета
public void init()
{
this.DlLayer = new DataLinkLayer(this);
GridBagLayout grbl = new GridBagLayout();
setLayout(grbl);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.BOTH;
c.gridheight = HEIGHT-1;
c.gridwidth = WIDTH;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0, 0, 0, 0);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.8;
c.weighty = 1;
txtAreaChat = new TextArea("Chat");
grbl.setConstraints(txtAreaChat, c);
add(txtAreaChat);
c.weightx = 0.2;
c.weighty = 0.0;
/* txtAreaContacts = new TextArea("Contacts", 10, 10);
c.gridx = WIDTH;
c.gridwidth = 1;
grbl.setConstraints(txtAreaContacts, c);
add(txtAreaContacts);*/
listAreaContacts = new List(10, true);
c.gridx = WIDTH;
c.gridwidth = 1;
grbl.setConstraints(listAreaContacts, c);
add(listAreaContacts);
txtFld = new TextField();
c.gridx = 0;
c.gridy = HEIGHT-1;
c.gridheight = 1;
c.gridwidth = WIDTH+1;
grbl.setConstraints(txtFld, c);
add(txtFld);
btnAction = new Button("Disconnect");
c.gridwidth = 1;
c.gridy = HEIGHT;
// c.insets = new Insets(10, 10, 10, 10);
grbl.setConstraints(btnAction, c);
add(btnAction);
btnConnect = new Button("Connect");
c.gridx = 1;
grbl.setConstraints(btnConnect, c);
add(btnConnect);
btnConnect.addActionListener(this);
btnSend = new Button("Send");
c.gridx = WIDTH-1;
grbl.setConstraints(btnSend, c);
add(btnSend);
btnSend.addActionListener(this);
btnHistory = new Button("History");
c.gridx = WIDTH-2;
grbl.setConstraints(btnHistory, c);
add(btnHistory);
btnExit = new Button("Exit");
c.gridx = WIDTH;
grbl.setConstraints(btnExit, c);
add(btnExit);
btnExit.addActionListener(this);
}
//обработчики событий на кнопки
public void actionPerformed(ActionEvent e)
{
if
(e.getActionCommand().equals("Connect"))
{
this.DlLayer.ConnectToCOMPort();
this.DlLayer.CheckConnection();
this.txtAreaChat.setText("");
}
if
(e.getActionCommand().equals("Send"))
{
String message = this.txtFld.getText();
this.txtAreaChat.appendText(this.DlLayer.networkName + ">> " + message + '\n');
this.DlLayer.ReceiveDataFromAppLayer(message);
}
if
(e.getActionCommand().equals("Exit"))
{
//this.txtFld.setText("Выход");
this.stop();
}
}
public String getAppletInfo()
{
return "Name: FormDemo";
}
public boolean action(Event evt,
Object obj)
{
return false;
}
}
21















