Make first real commit: copy of CaRMetal 4.2.8

This commit is contained in:
Glen Whitney 2018-09-04 22:51:42 -04:00
parent 002acfc88e
commit c312811084
1120 changed files with 226843 additions and 1 deletions

38
pm/Client/Client.java Normal file
View file

@ -0,0 +1,38 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import java.io.IOException;
import java.net.Socket;
/**
*
* @author PM
*/
public class Client implements Runnable {
private String IP = null, login = null;
private int PORT;
private Socket socket = null;
private ConnectionControlPanel ccp = null;
public Client(String name, String IP, int PORT, ConnectionControlPanel ccp) {
this.login = name;
this.IP = IP;
this.PORT = PORT;
this.ccp = ccp;
}
@Override
public void run() {
try {
socket = new Socket(IP, PORT);
new Thread(new Connexion(socket, login)).start();
ccp.doClose();
} catch (IOException e){
System.out.println("Erreur de connexion (Client)");
}
}
}

View file

@ -0,0 +1,269 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import eric.FileTools;
import eric.GUI.ZDialog.ZButton;
import eric.GUI.ZDialog.ZDialog;
import eric.GUI.ZDialog.ZTextFieldAndLabel;
import eric.GUI.palette.PaletteManager;
import eric.JZirkelCanvas;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import rene.gui.Global;
import rene.util.MyVector;
import rene.util.xml.XmlWriter;
import rene.zirkel.ZirkelCanvas;
import rene.zirkel.constructors.ObjectConstructor;
import rene.zirkel.objects.ConstructionObject;
import rene.zirkel.objects.FunctionObject;
import rene.zirkel.objects.PlumbObject;
import rene.zirkel.objects.PointObject;
import rene.zirkel.objects.PrimitiveCircleObject;
import rene.zirkel.objects.PrimitiveLineObject;
/**
*
* @author PM
*/
public class ClientNetworkTools extends ZDialog{
private ZTextFieldAndLabel targetslist;
private ZButton send_work, send_objects;
private Communication com;
private int FWIDTH = 240;
private boolean point, line, circle, function, real_time;
public ClientNetworkTools(Communication com){
super("Network Tools", 0, 0, 366, 70, false, false);
//BWIDTH = 80;
this.com = com;
addContent();
}
public void init(int w, int h) {
int x = (w-D_WIDTH)/2;
int y = h-D_HEIGHT-4;
setBounds(x, y, D_WIDTH, D_HEIGHT);
}
private void addContent(){
send_work = new ZButton(Global.Loc("network.client.sendwork")) {
@Override
public void action(){
try {
String str = PaletteManager.geomSelectedIcon(); //(*)
PaletteManager.deselectgeomgroup(); //(*)
com.send(FileTools.getCurrentFileSource());
this.pressed(this);
PaletteManager.setSelected_with_clic(str, true); //(*)
/* these three lines are a trick to prevent
* the cursor to be placed in the next component,
* which is the field targetslist
* A more elegant solution have to be found..
*/
} catch (Exception ex) {
System.out.println("Erreur d'envoi");
}
}
};
targetslist = new ZTextFieldAndLabel(Global.Loc("job.gui.targets"), "", 0, CHEIGHT) {
@Override
public void actionMouse() {
PaletteManager.deselectgeomgroup();
ZirkelCanvas zc = JZirkelCanvas.getCurrentZC();
zc.setTool(new SelectionTool((ClientNetworkTools) this.getParent(), this));
zc.showStatus("Select Objects");
if(!this.getText().equals("")){
String[] names = this.getText().split(";");
for(String name : names){
ConstructionObject o = zc.getConstruction().find(name);
if(o!=null){
o.setSelected(true);
}
}
}
zc.repaint();
}
@Override
public void actionKey(KeyEvent k) {
//A programmer
}
};
send_objects = new ZButton(Global.Loc("network.client.share")) {
@Override
public void action(){
if(!targetslist.getText().equals("")){
String[] names = targetslist.getText().split(";");
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
XmlWriter xml = new XmlWriter(new PrintWriter(new OutputStreamWriter(bout), true));
xml.printXml();
xml.startTagNewLine("Objects");
for(String name : names){
ConstructionObject o = JZirkelCanvas.getCurrentZC().getConstruction().find(name);
/*
Enumeration e = o.depending();
while(e.hasMoreElements()){
((ConstructionObject) e.nextElement()).save(xml);
}
o.save(xml);
*/
ArrayList<ConstructionObject> list = Collections.list(o.depending());
find_all_depending(list, xml);
o.save(xml);
o.setSelected(false);
}
JZirkelCanvas.getCurrentZC().repaint();
xml.endTagNewLine("Objects");
//cc.send(out.toString("utf-8"));
com.send("<Global>\n"+bout.toString());
targetslist.setText("");
this.pressed(this);
//System.out.println(out.toString());
}
}
};
this.add(send_work);
this.add(targetslist);
this.add(send_objects);
fixComponents();
}
public XmlWriter find_all_depending(ArrayList<ConstructionObject> list, XmlWriter xml) {
ArrayList<ConstructionObject> l ;
for(int i = 0; i<list.size(); i++) {
l = Collections.list(list.get(i).depending());
xml = find_all_depending(l, xml);
list.get(i).save(xml);
}
return xml;
}
@Override
public void fixComponents(){
send_work.setBounds((D_WIDTH-2*BWIDTH)/2, MARGINTOP1, 2*BWIDTH, CHEIGHT);
targetslist.setBounds(MARGINW, MARGINTOP2, FWIDTH, CHEIGHT);
send_objects.setBounds(FWIDTH+2*MARGINW, MARGINTOP2, BWIDTH, CHEIGHT);
}
@Override
public void doClose() {
com.close();
JZirkelCanvas.getCurrentZC().remove(this);
JZirkelCanvas.getCurrentZC().repaint();
JZirkelCanvas.getCurrentZC().set_cnt(null);
}
public void set_accepted_object(String obj, boolean bol){
if(obj.equals("point")){
point = bol;
} else if(obj.equals("line")){
line = bol;
} else if(obj.equals("circle")){
circle = bol;
} else if(obj.equals("function")){
function = bol;
}
}
public boolean get_accepted_object(String obj){
if(obj.equals("point")){
return point;
} else if(obj.equals("line")){
return line;
} else if(obj.equals("circle")){
return circle;
} else if(obj.equals("function")){
return function;
}
return false;
}
public void set_real_time(boolean bol){
real_time = bol;
}
public boolean get_real_time(){
return real_time;
}
@Override
public void send(String s){
com.send(s);
}
}
class SelectionTool extends ObjectConstructor{
private ZTextFieldAndLabel targetslist;
private ClientNetworkTools cnt = null;
public SelectionTool(ClientNetworkTools cnt, ZTextFieldAndLabel targetslist){
this.targetslist = targetslist;
this.cnt = cnt;
}
@Override
public void mousePressed(MouseEvent e, ZirkelCanvas zc){
final ConstructionObject o = zc.selectObject(e.getX(), e.getY());
if(o==null){
return;
}
if((cnt.get_accepted_object("point") && o instanceof PointObject)
|| (cnt.get_accepted_object("line") && o instanceof PrimitiveLineObject)
|| (cnt.get_accepted_object("circle") && o instanceof PrimitiveCircleObject)
|| (cnt.get_accepted_object("function") && o instanceof FunctionObject)){
if(o.selected()){
o.setSelected(false);
targetslist.setText(targetslist.getText().replaceAll(o.getName()+";", ""));
} else {
o.setSelected(true);
targetslist.setText(targetslist.getText()+o.getName()+";");
}
zc.repaint();
}
}
@Override
public void mouseMoved(final MouseEvent e, final ZirkelCanvas zc, final boolean simple) {
Enumeration en = zc.getConstruction().elements();
MyVector V = new MyVector();
while(en.hasMoreElements()){
ConstructionObject o = (ConstructionObject) en.nextElement();
if(o.nearto(e.getX(), e.getY(), zc)){
if((cnt.get_accepted_object("point") && o instanceof PointObject)
|| (cnt.get_accepted_object("line") && o instanceof PrimitiveLineObject)
|| (cnt.get_accepted_object("circle") && o instanceof PrimitiveCircleObject)
|| (cnt.get_accepted_object("function") && o instanceof FunctionObject)){
V.addElement(o);
break;
}
}
}
zc.indicate(V);
}
}

View file

@ -0,0 +1,66 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import eric.GUI.palette.PaletteManager;
import eric.JZirkelCanvas;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import rene.zirkel.ZirkelCanvas;
/**
*
* @author PM
*/
public class Communication implements Runnable {
private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;
private ClientNetworkTools cnt = null;
public Communication (Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
cnt = new ClientNetworkTools(this);
ZirkelCanvas zc = JZirkelCanvas.getCurrentZC();
zc.add(cnt);
zc.set_cnt(cnt);
zc.repaint();
zc.init_cnt();
PaletteManager.setSelected_with_clic("move", true);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new Thread(new Reception(in, cnt, this)).start();
} catch (IOException e) {
cnt.doClose();
close();
System.out.println("Erreur de communication (com_client)");
}
}
public void send(String msg){
out.println(msg+"END_MESSAGE");
out.flush();
}
public void close(){
send("<End>\n");
out.close();
try {
//socket.close();
in.close();
} catch (IOException ex) {}
}
}

View file

@ -0,0 +1,162 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import eric.GUI.ZDialog.ZButton;
import eric.GUI.ZDialog.ZDialog;
import eric.GUI.ZDialog.ZTextFieldAndLabel;
import eric.GUI.ZDialog.ZTools;
import eric.GUI.palette.PaletteManager;
import eric.JZirkelCanvas;
import java.awt.Color;
import java.awt.event.KeyEvent;
import rene.gui.Global;
/**
*
* @author PM
*/
public class ConnectionControlPanel extends ZDialog {
private ZTextFieldAndLabel name, ip, port;
private ZButton connect;
private Thread t;
private Color color = Color.RED;
private int PORT = 2357;
public ConnectionControlPanel() {
super(Global.Loc("network.client.title"), 3, 90, 220, 135, true, true);
BWIDTH = 100;
LWIDTH = 70;
addContent();
}
private void addContent() {
name = new ZTextFieldAndLabel(Global.Loc("network.client.Name"), Global.getParameter("network.name", Global.Loc("network.client.name")), LWIDTH, CHEIGHT){
@Override
public void focusGained() {
if(this.getText().equals(Global.Loc("network.client.name"))){
this.setText("");
this.setForeground(ZTools.C_TextField);
}
}
@Override
public void focusLost(){
if(this.getText().equals("")){
this.setText(Global.Loc("network.client.name"));
this.setForeground(ZTools.C_TextField_OFF);
}
}
@Override
public void actionKey(KeyEvent k){
if(k.getKeyCode()==KeyEvent.VK_ENTER){
connect.action();
} else {
this.setForeground(ZTools.C_TextField);
}
}
};
name.setForeground(name.getText().equals(Global.Loc("network.client.name"))?ZTools.C_TextField_OFF:ZTools.C_TextField);
ip = new ZTextFieldAndLabel(Global.Loc("network.client.ip"), Global.getParameter("network.ip", "ip (xxx.xxx.xxx.xxx)"), LWIDTH, CHEIGHT){
@Override
public void focusGained(){
if(this.getText().equals("ip (xxx.xxx.xxx.xxx)")){
this.setText("");
}
this.setForeground(ZTools.C_TextField);
}
@Override
public void focusLost(){
if(this.getText().equals("")){
this.setText("ip (xxx.xxx.xxx.xxx)");
this.setForeground(ZTools.C_TextField_OFF);
}
}
@Override
public void actionKey(KeyEvent k){
if(k.getKeyCode()==KeyEvent.VK_ENTER){
connect.action();
} else {
this.setForeground(ZTools.C_TextField);
}
}
};
ip.setForeground(ip.getText().equals("ip (xxx.xxx.xxx.xxx)")?ZTools.C_TextField_OFF:ZTools.C_TextField);
port = new ZTextFieldAndLabel(Global.Loc("network.client.port"), Integer.toString(PORT), LWIDTH, CHEIGHT){
@Override
public void focusGained(){
//this.setForeground(ZTools.C_TextField);
}
@Override
public void focusLost(){
}
};
port.setEditable(false);
port.setForeground(ZTools.C_TextField_OFF);
connect = new ZButton(Global.Loc("network.client.connect")){
@Override
public void action() {
Global.setParameter("network.name", name.getText());
if(can_connect(name, ip)){
Global.setParameter("network.ip", ip.getText());
t = new Thread(new Client(name.getText(), ip.getText(), PORT, (ConnectionControlPanel) this.getParent()));
t.start();
}
}
};
this.add(name);
this.add(ip);
this.add(port);
this.add(connect);
}
private boolean can_connect(ZTextFieldAndLabel name, ZTextFieldAndLabel ip){
boolean b = true;
String NAME = name.getText(), IP = ip.getText();
if(NAME.equals("") || NAME.equals(Global.Loc("network.client.name"))){
name.setText(Global.Loc("network.client.name"));
name.setForeground(color);
b = false;
}
if(IP.equals("")){
ip.setText("ip (xxx.xxx.xxx.xxx)");
ip.setForeground(color);
b = false;
}
if(!IP.matches("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$")){
ip.setForeground(color);
b = false;
}
return b;
}
@Override
public void fixComponents(){
name.setBounds(MARGINW, MARGINTOP1, D_WIDTH-2*MARGINW, CHEIGHT);
ip.setBounds(MARGINW, MARGINTOP2, D_WIDTH-2*MARGINW, CHEIGHT);
port.setBounds(MARGINW, MARGINTOP3, D_WIDTH-2*MARGINW, CHEIGHT);
connect.setBounds(D_WIDTH/2-BWIDTH/2, MARGINTOP4, BWIDTH, CHEIGHT);
}
@Override
public void doClose() {
JZirkelCanvas.getCurrentZC().remove(this);
JZirkelCanvas.getCurrentZC().repaint();
PaletteManager.ClicOn("move");
}
}

43
pm/Client/Connexion.java Normal file
View file

@ -0,0 +1,43 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @author PM
*/
public class Connexion implements Runnable {
private Socket socket = null;
private PrintWriter out = null;
private String login = null;
public Connexion(Socket socket, String login){
this.socket = socket;
this.login = login;
}
@Override
public void run() {
try {
out = new PrintWriter(socket.getOutputStream());
out.println(login);
out.flush();
new Thread(new Communication(socket)).start();
} catch (IOException e) {
out.close();
try {
socket.close();
} catch (Exception ee){}
System.out.println("Erreur de connexion (Connexion)");
}
}
}

91
pm/Client/Reception.java Normal file
View file

@ -0,0 +1,91 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Client;
import eric.FileTools;
import eric.JZirkelCanvas;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rene.zirkel.ZirkelCanvas;
/**
*
* @author PM
*/
public class Reception implements Runnable {
private BufferedReader in = null;
private String message = null;
private ClientNetworkTools cnt = null;
private Communication Com = null;
private ZirkelCanvas ZC;
public Reception(BufferedReader in, ClientNetworkTools cnt, Communication Com){
this.in = in;
this.cnt = cnt;
this.Com = Com;
ZC = JZirkelCanvas.getCurrentZC();
}
@Override
public void run() {
try {
while(true){
String src = "";
/*
while((message = in.readLine())!=null){
src += message+"\n";
}
*
*/
message = in.readLine();
while(message!=null && !message.equals("END_MESSAGE")) {
src += message+"\n";
message = in.readLine();
}
//System.out.println("src =\n"+src);
if(src.startsWith("<Accepted objects>")) {
String objs[] = src.split("\n");
String obj[];
for(int i = 1; i<objs.length-1; i++) {
obj = objs[i].split("=");
cnt.set_accepted_object(obj[0], Boolean.parseBoolean(obj[1]));
}
} else if(src.startsWith("<Real time>")) {
if(src.endsWith("true\n"+"")) {
cnt.set_real_time(true);
try {
Com.send(FileTools.getCurrentFileSource());
} catch(Exception e){
System.err.println("Erreur d'envoi (Client/Reception)");
}
} else {
cnt.set_real_time(false);
}
} else if(src.startsWith("<End>")) {
cnt.doClose();
} else if(src.contains("<CaR>")) { //receiving the whole figure
try {
//tab_main_panel.getActivePanel().getZC().setFileSource(src);
ZC.setFileSource(src);
} catch (Exception ex){}
} else if(src.startsWith("<To")) { //add, update, delete, change name
ZC.update_local(src);
} else if(src.startsWith("<Collaboration>")) {
cnt.set_real_time(src.endsWith("true\n"+""));
} else {
System.err.println("Not implemented yet");
System.out.println(src);
System.out.println("-------------------");
}
}
} catch (IOException e) {
cnt.doClose();
System.err.println("Serveur OffLine");
}
}
}

View file

@ -0,0 +1,171 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Server;
import eric.GUI.window.tab_main_panel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.SwingUtilities;
/**
*
* @author PM
*/
public class Accept_clients implements Runnable {
private ServerSocket ss = null;
private Socket socket = null;
private int client_nb = 0;
private int NB_MAX;
private BufferedReader in = null;
private String login = null, ip = null;
private String clients[][] = null;
private ServerControlPanel scp = null;
private Communication com = null;
private Communication Com[] = null;
private PrintWriter out = null;
public Accept_clients(ServerControlPanel scp, ServerSocket ss, int NB_MAX, String clients[][]){
this.ss = ss;
this.NB_MAX = NB_MAX;
this.clients = clients;
this.scp = scp;
this.Com = new Communication[NB_MAX];
}
@Override
public void run() {
while(client_nb<NB_MAX && scp.isServerRunning()){
try {
socket = ss.accept();
if(!already_logged(ip = socket.getInetAddress().toString().substring(1))){
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if((login = in.readLine())!=null){
out = new PrintWriter(socket.getOutputStream());
out.println(scp.accepted_objects()+"END_MESSAGE");
out.flush();
com = new Communication(scp, socket, login, ip);
int i = insert_client(login, ip, com);
//System.out.println("# clients = "+client_nb);
new Thread(com).start();
if(scp.get_collaboration()) {
try {
out.println(eric.FileTools.getCurrentFileSource()+"END_MESSAGE");
out.flush();
} catch (Exception e) { }
out.println("<Collaboration> = "+true+"\nEND_MESSAGE");
out.flush();
clients[i][2] = "true";
}
}
} catch (IOException e) {}
}
}
catch (IOException e) {
System.out.println("Erreur d'acceptation Client");
}
}
}
private boolean already_logged(String ip){
for (int i = 0; i<NB_MAX; i++){
//if(clients[i][1]!=null && clients[i][1].equals(ip)){
if(ip.equals(clients[i][1])){
return true;
}
}
return false;
}
private int insert_client(final String login, final String ip, Communication com){
//search for an free storage index
int i = 0;
while(clients[i][0]!=null){
i++;
}
clients[i][0] = login;
clients[i][1] = ip;
Com[i] = com;
try {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
//tab_main_panel.addBtnAndSelect(login + " ("+ip+")");
eric.GUI.window.tab_btn btn = tab_main_panel.getActiveBtn();
tab_main_panel.addBtn(login + " ("+ip+")");
tab_main_panel.initBTNS(null);
tab_main_panel.setActiveBtn(btn);
client_nb++;
}
});
} catch(Exception e){
System.err.println(e);
}
return i;
}
public void send(String msg, int client_index){
if(client_index==NB_MAX){ //send to all clients
for(int i = 0; i<client_nb; i++){
if(Com[i]!=null){
Com[i].send(msg);
}
}
} else {
Com[client_index].send(msg);
}
}
public void delete_client(String login, String ip){
//finding the number of current client
int i = 0;
while(i<clients.length && !login.equals(clients[i][0])){
i++;
}
if(i!=clients.length){
//finding the tab of that client an rename it
int n = tab_main_panel.getBTNSsize();
int k = 0;
while(k<n && !tab_main_panel.getBTN(k).getTabName().equals(login + " ("+ip+")")){
k++;
}
//tab_main_panel.removeBtnAndSelect(tab_main_panel.getBTN(k));
tab_main_panel.getBTN(k).setTabName(login + " (log. off)", login + " (log. off)");
clients[i][0] = null;
clients[i][1] = null;
clients[i][2] = "false";
Com[i] = null;
client_nb--;
}
}
public void kill(){
for(int i = 0; i<clients.length; i++){
if(Com[i]!=null){ // or clients[i][0], or client[i][1]
Com[i].kill();
delete_client((String)clients[i][0], (String)clients[i][1]);
}
}
try {
ss.close();
} catch (IOException ex) {}
}
}

View file

@ -0,0 +1,60 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @author PM
*/
public class Communication implements Runnable {
private Socket socket;
private String login, ip;
private PrintWriter out = null;
private BufferedReader in = null;
private Reception R = null;
private ServerControlPanel scp = null;
public Communication(ServerControlPanel scp, Socket socket, String login, String ip){
this.scp = scp;
this.login = login;
this.socket = socket;
this.ip = ip;
}
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
R = new Reception(scp, in, login, ip);
Thread t = new Thread(R);
t.start();
} catch (IOException e) {
System.out.println("Le client "+login+" s'est déconnecté");
}
}
public void send(String msg){
out.println(msg+"END_MESSAGE");
out.flush();
}
public void kill(){
//send("<End>");
R = null;
try {
socket.close();
} catch (IOException ex) {}
out.close();
}
}

93
pm/Server/Reception.java Normal file
View file

@ -0,0 +1,93 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Server;
import eric.GUI.window.tab_main_panel;
import java.io.BufferedReader;
import java.io.IOException;
import javax.swing.SwingUtilities;
/**
*
* @author PM
*/
public class Reception implements Runnable {
private BufferedReader in = null;
private String login = null, message = null, ip = null;
private ServerControlPanel scp = null;
public Reception(ServerControlPanel scp, BufferedReader in, String login, String ip){
this.scp = scp;
this.in = in;
this.login = login;
this.ip = ip;
}
private Runnable delete = new Runnable(){
@Override
public void run() {
scp.delete_client(login, ip);
try {
in.close();
} catch (Exception e) {}
}
};
@Override
public void run() {
try {
//while(true){
while(scp.isServerRunning()){
String src = "";
/*
while((message = in.readLine())!=null){
src += message+"\n";
}
*/
message = in.readLine();
while(scp.isServerRunning() && message!=null && !message.equals("END_MESSAGE")){
src += message+"\n";
message = in.readLine();
}
//System.out.println(src);
try {
int n = tab_main_panel.getBTNSsize();
int i = 0;
if(src.startsWith("<End>")) {
SwingUtilities.invokeLater(new Thread(delete));
} else if(src.contains("<CaR>")){ //receiving the whole figure
while(i<n && !tab_main_panel.getBTN(i).getTabName().equals(login + " ("+ip+")")){
i++;
}
tab_main_panel.getPanel(i).getZC().setFileSource(src);
} else {
String tab_name = ((src.startsWith("<To add>")
|| src.startsWith("<To delete>")
|| src.startsWith("<To update>")
|| src.startsWith("<To change name>")) && !scp.get_collaboration())
? login + " ("+ip+")" : "Global";
while(i<n && !tab_main_panel.getBTN(i).getTabName().equals(tab_name)) {
i++;
}
if(scp.get_collaboration()) {
scp.send_minus(src, ip); //transmettre aux autres clients
}
tab_main_panel.getPanel(i).getZC().update_local(src);
}
} catch (Exception e) {
System.out.println("Read error");
}
}
} catch (IOException e) {
System.err.println(login+" Offline");
SwingUtilities.invokeLater(new Thread(delete));
}
}
}

64
pm/Server/Server.java Normal file
View file

@ -0,0 +1,64 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Server;
import eric.GUI.window.tab_main_panel;
import java.io.IOException;
import java.net.ServerSocket;
/**
*
* @author PM
*/
public class Server implements Runnable {
private ServerSocket ss = null;
private Thread t = null;
private int port, NB_MAX;
private String clients[][] = null;
private ServerControlPanel scp = null;
private Accept_clients ac = null;
public Server(ServerControlPanel scp, int port, int NB_MAX, String clients[][]) {
this.port = port;
this.NB_MAX = NB_MAX;
this.clients = clients;
this.scp = scp;
}
@Override
public void run(){
try {
ss = new ServerSocket(port);
//ac = new Accept_clients(scp, ss, NB_MAX, clients, Com);
ac = new Accept_clients(scp, ss, NB_MAX, clients);
t = new Thread(ac);
tab_main_panel.getActiveBtn().setTabName("Global", "Global");
t.start();
} catch (IOException e) {
scp.setServerRunning(false);
System.out.println("Could not start server");
}
}
public void send(String msg, int client_index){
ac.send(msg, client_index);
}
public void send(String msg){
send(msg, NB_MAX);
}
public void delete_client(String login, String ip){
ac.delete_client(login, ip);
}
public void kill() {
if(ac!=null){
ac.kill();
}
//ac = null;
}
}

View file

@ -0,0 +1,438 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pm.Server;
import eric.FileTools;
import eric.GUI.ZDialog.ZButton;
import eric.GUI.ZDialog.ZCheckBox;
import eric.GUI.ZDialog.ZDialog;
import eric.GUI.ZDialog.ZSep;
import eric.GUI.ZDialog.ZTextFieldAndLabel;
import eric.GUI.ZDialog.ZTools;
import eric.GUI.window.tab_main_panel;
import eric.GUI.windowComponent;
import eric.JZirkelCanvas;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JLabel;
import rene.gui.Global;
/**
*
* @author PM
*/
public class ServerControlPanel extends ZDialog {
private JLabel Jip, Jreceive, Jsend_to;
private ZTextFieldAndLabel Zport;
private ZButton Zconnect, Zsend, Zsend_all;
private ZCheckBox Zpoint, Zline, Zcircle, Zfunction, Zreal_time_view, Zcollaborative;
private boolean Point = true, Line = true, Circle = true, Function = true;
private InetAddress LocaleAdresse ;
private int PORT = 2357, NB_MAX = 20, H;
private ZSep sep = new ZSep(75), sep2 = new ZSep(75), sep3 = new ZSep(75);
private Server server = null;
private boolean reduced = false, isServerRunning = false;
private String clients[][] = null; //clients[i][0] = login, clients[i][1] = ip, clients[i][2] = real_time_view
private boolean refresh = false; //to avoid an infinite loop un paint
private boolean collaboration = false;
public ServerControlPanel(){
super(Global.Loc("network.server.title"), 3, 90, 200, 300, true, true);
LWIDTH = 40;
BWIDTH = 80;
H = D_HEIGHT;
clients = new String[NB_MAX][3];
for(int i=0; i<NB_MAX; i++){
clients[i][2] = "false";
}
addContent();
}
@Override
public void paint(Graphics g) {
if (JZirkelCanvas.isPaintCalled()) {
Graphics2D g2d = windowComponent.getGraphics2D(g);
if (isTitleVisible()) {
// draw the title background :
g2d.setColor(ZTools.backTitleColor);
g2d.setClip(0, 0, D_WIDTH, THEIGHT);
g2d.fill(roundRect);
}
if (isCloseBoxVisible()) {
// draw the close box :
g2d.setColor(ZTools.TitleTextColor);
if (boxEnter) {
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
} else {
g2d.setStroke(new BasicStroke(1.5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
g2d.drawOval(cx, cy, cw, cw);
int e = 3;
g2d.drawLine(cx+e, cy+cw/2, cx+cw-e, cy+cw/2);
if(reduced){
g2d.drawLine(cx+cw/2, cy+e, cx+cw/2, cy+cw-e);
D_HEIGHT = 23;
roundRect=new RoundRectangle2D.Double(2, 2, D_WIDTH-4, D_HEIGHT-4, ARCCORNER, ARCCORNER);
this.remove();
} else {
D_HEIGHT = H;
roundRect=new RoundRectangle2D.Double(2, 2, D_WIDTH-4, D_HEIGHT-4, ARCCORNER, ARCCORNER);
this.add();
}
if(refresh){
JZirkelCanvas.getCurrentZC().repaint();
refresh = false;
}
}
// draw the content background :
g2d.setColor(ZTools.backMainColor);
g2d.setClip(0, THEIGHT, D_WIDTH, D_HEIGHT);
g2d.fill(roundRect);
g2d.setClip(0, 0, D_WIDTH, D_HEIGHT);
g2d.setColor(Color.black);
g2d.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
g2d.draw(roundRect);
g2d.setStroke(new BasicStroke(1f));
paintChildren(g);
}
}
private void addContent(){
Jip = new JLabel();
try {
LocaleAdresse = InetAddress.getLocalHost();
Jip.setText(Global.Loc("network.server.ip") + " : " + LocaleAdresse.toString().substring(LocaleAdresse.toString().lastIndexOf("/")+1));
} catch(UnknownHostException e){
System.out.println("Err = "+e);
}
Zport = new ZTextFieldAndLabel("Port : ", Integer.toString(PORT), LWIDTH, CHEIGHT){
};
Zport.setEditable(false);
Zport.setForeground(ZTools.C_TextField_OFF);
Zconnect = new ZButton(Global.Loc("network.server.launch")){
@Override
public void action(){
if(this.isEnabled()){
server = new Server((ServerControlPanel) this.getParent(), PORT, NB_MAX, clients);
new Thread(server).start();
this.setEnabled(false);
Zsend_all.setEnabled(true);
Zcollaborative.setEnabled(true);
isServerRunning = true;
}
}
};
Jreceive = new JLabel();
Jreceive.setText(Global.Loc("network.server.receive"));
Zpoint = new ZCheckBox(Global.Loc("palette.sizes.point"), true){
@Override
public void action(){
set_accepted_object("point", this.isSelected());
}
};
Zline = new ZCheckBox(Global.Loc("palette.sizes.line"), true){
@Override
public void action(){
set_accepted_object("line", this.isSelected());
}
};
Zcircle = new ZCheckBox(Global.Loc("network.server.circles"), true){
@Override
public void action(){
set_accepted_object("circle", this.isSelected());
}
};
Zfunction = new ZCheckBox(Global.Loc("network.server.functions"), true) {
@Override
public void action() {
set_accepted_object("function", this.isSelected());
}
};
Zreal_time_view = new ZCheckBox(Global.Loc("network.server.rtw"), false) {
@Override
public synchronized void action() {
String tab_name = tab_main_panel.getActiveBtn().getTabName();
if(!tab_name.equals("Global") && server!=null && !collaboration) {
int i = get_client_index(tab_name);
if(i!=NB_MAX) {
clients[i][2] = String.valueOf(this.isSelected());
server.send("<Real time> = "+clients[i][2]+"\n", i);
}
} else {
this.setSelected(collaboration);
}
}
};
Zreal_time_view.setEnabled(false);
Jsend_to = new JLabel();
Jsend_to.setText(Global.Loc("network.server.send"));
Zsend = new ZButton(Global.Loc("network.client.name")) {
@Override
public void action() {
if(server!=null) {
String tab_name = tab_main_panel.getActiveBtn().getTabName();
int i = get_client_index(tab_name);
try {
server.send(FileTools.getCurrentFileSource(), i);
this.pressed(this);
} catch(Exception ex){}
}
}
};
Zsend.setEnabled(false);
Zsend_all = new ZButton(Global.Loc("network.server.all")) {
@Override
public void action() {
try {
server.send(FileTools.getCurrentFileSource());
this.pressed(this);
} catch(Exception ex){}
}
};
Zsend_all.setEnabled(false);
Zcollaborative = new ZCheckBox("Travail collaboratif (β)", false) {
@Override
public synchronized void action(){
int i = 0;
if(server==null){
this.setSelected(false);
} else {
if(collaboration = this.isSelected()) { //it is an =
try {
String src = FileTools.getCurrentFileSource();
server.send(src); //the construction is sent to everyone
//on se positionne sur l'onglet Global
int n = tab_main_panel.getBTNSsize();
while(i<n && !tab_main_panel.getBTN(i).getTabName().equals("Global")) {
i++;
}
tab_main_panel.getPanel(i).getZC().setFileSource(src);
tab_main_panel.setActiveBtn(i);
} catch(Exception e){}
}
server.send("<Collaboration> = "+collaboration+"\n");
Zreal_time_view.setEnabled(!collaboration || !tab_main_panel.getActiveBtn().getTabName().equals("Global"));
Zreal_time_view.setSelected(collaboration);
for(i=0; i<NB_MAX && clients[i][0]!=null; i++) {
clients[i][2] = Boolean.toString(collaboration);
}
}
}
};
Zcollaborative.setEnabled(false);
this.add();
}
public void add() {
this.add(Jip);
this.add(Zport);
this.add(Zconnect);
this.add(sep);
this.add(Jreceive);
this.add(Zpoint);
this.add(Zline);
this.add(Zcircle);
this.add(Zfunction);
this.add(sep2);
this.add(Zreal_time_view);
this.add(sep3);
this.add(Jsend_to);
this.add(Zsend);
this.add(Zsend_all);
this.add(Zcollaborative);
//this.fixComponents();
}
public void remove() {
this.remove(Jip);
this.remove(Zport);
this.remove(Zconnect);
this.remove(sep);
this.remove(Jreceive);
this.remove(Zpoint);
this.remove(Zline);
this.remove(Zcircle);
this.remove(Zfunction);
this.remove(sep2);
this.remove(Zreal_time_view);
this.remove(sep3);
this.remove(Jsend_to);
this.remove(Zsend);
this.remove(Zsend_all);
this.remove(Zcollaborative);
}
@Override
public void fixComponents() {
Jip.setBounds(MARGINW, MARGINTOP1, D_WIDTH-2*MARGINW, CHEIGHT);
Zport.setBounds(MARGINW, MARGINTOP2, (D_WIDTH-2*MARGINW)/2, CHEIGHT);
Zconnect.setBounds(2*MARGINW+(D_WIDTH-2*MARGINW)/2, MARGINTOP2, BWIDTH, CHEIGHT);
sep.setBounds(0, MARGINTOP3, D_WIDTH, 1);
Jreceive.setBounds(MARGINW, MARGINTOP3+10, D_WIDTH-2*MARGINW, CHEIGHT);
Zpoint.setBounds(MARGINW, MARGINTOP4+10, (D_WIDTH-2*MARGINW)/2, CHEIGHT);
Zline.setBounds(2*MARGINW+(D_WIDTH-2*MARGINW)/2, MARGINTOP4+10, (D_WIDTH-2*MARGINW)/2, CHEIGHT);
Zcircle.setBounds(MARGINW, MARGINTOP5+10, (D_WIDTH-2*MARGINW)/2, CHEIGHT);
Zfunction.setBounds(2*MARGINW+(D_WIDTH-2*MARGINW)/2, MARGINTOP5+10, (D_WIDTH-2*MARGINW)/2, CHEIGHT);
int MARGINTOP6 = MARGINTOP5+26+10;
sep2.setBounds(0, MARGINTOP6, D_WIDTH, 1);
Zreal_time_view.setBounds(MARGINW, MARGINTOP6+10, D_WIDTH-2*MARGINW, CHEIGHT);
int MARGINTOP7 = MARGINTOP6+26+10;
sep3.setBounds(0, MARGINTOP7, D_WIDTH, 1);
Jsend_to.setBounds(MARGINW, MARGINTOP7+10, D_WIDTH-2*MARGINW, CHEIGHT);
Zsend.setBounds(MARGINW, MARGINTOP7+26+10, BWIDTH, CHEIGHT);
Zsend_all.setBounds(2*MARGINW+(D_WIDTH-2*MARGINW)/2, MARGINTOP7+26+10, BWIDTH, CHEIGHT);
Zcollaborative.setBounds(MARGINW, MARGINTOP7+2*26+10, D_WIDTH-2*MARGINW, CHEIGHT);
}
@Override
public void doClose() {
reduced = !reduced;
refresh = true;
JZirkelCanvas.getCurrentZC().repaint();
}
public void refresh() {
String tab_name = tab_main_panel.getActiveBtn().getTabName();
int i = get_client_index(tab_name);
if(i!=NB_MAX) { //a client
Zreal_time_view.setSelected(Boolean.parseBoolean(clients[i][2]) || collaboration);
Zreal_time_view.setEnabled(!collaboration);
Zsend.setText(clients[i][0]);
Zsend.setEnabled(true);
} else { //Global or other
Zreal_time_view.setSelected(collaboration);
Zreal_time_view.setEnabled(false);
Zsend.setText(Global.Loc("network.client.name"));
Zsend.setEnabled(false);
}
}
private int get_client_index(final String tab_name) {
if(!tab_name.contains("(") || !tab_name.contains(")")) {
return NB_MAX;
}
int i = 0;
String IP = tab_name.substring(tab_name.indexOf("(")+1, tab_name.indexOf(")"));
while(i<NB_MAX && !IP.equals(clients[i][1])) {
i++;
}
return i;
}
public String accepted_objects() {
String s = "<Accepted objects>\n";
s += "point="+Point+"\n";
s += "line="+Line+"\n";
s += "circle="+Circle+"\n";
s += "function="+Function+"\n";
s += "</Accepted objects>\n";
//s += "";
return s;
}
public void set_accepted_object(String name, boolean value) {
if(name.equals("point")){
Point = value;
} else if(name.equals("line")){
Line = value;
} else if(name.equals("circle")){
Circle = value;
} else if(name.equals("function")){
Function = value;
}
if(server!=null){
server.send(accepted_objects());
}
}
public void delete_client(String login, String ip) {
server.delete_client(login, ip);
}
public boolean isServerRunning() {
return isServerRunning;
}
public void setServerRunning(boolean b) {
isServerRunning = b;
}
public void close_and_kill_server() {
if(server!=null){
server.send("<End>");
server.kill();
}
isServerRunning = false;
//server = null;
}
public boolean get_collaboration() {
return collaboration;
}
@Override
public void send(String msg){
server.send(msg);
}
public void send_minus(String msg, String ip) {
for(int i=0; i<NB_MAX; i++){
if(clients[i][1]!=null && !clients[i][1].equals(ip)) {
server.send(msg, i);
}
}
}
}