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

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);
}
}
}
}