Skip to content

Commit e893567

Browse files
committed
Adding API key to the server and client
1 parent 6825dea commit e893567

8 files changed

Lines changed: 191 additions & 83 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ html
3737
/InsideOutsideTest.svg.png
3838
/Alexes_Bad.svg.png
3939
/*.jks
40+
/file.txt

src/main/java/eu/mihosoft/vrl/v3d/CSG.java

Lines changed: 76 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ public class CSG implements IuserAPI, Serializable {
147147
private static String defaultcolor = "#007956";
148148

149149
/** The color. */
150-
//private Color color = getDefaultColor();
151-
private double r= getDefaultColor().getRed();
152-
private double g=getDefaultColor().getGreen();
153-
private double b= getDefaultColor().getBlue();
154-
private double o=getDefaultColor().getOpacity();
150+
// private Color color = getDefaultColor();
151+
private double r = getDefaultColor().getRed();
152+
private double g = getDefaultColor().getGreen();
153+
private double b = getDefaultColor().getBlue();
154+
private double o = getDefaultColor().getOpacity();
155155
/** The manipulator. */
156156
private Affine manipulator;
157157
private Bounds bounds;
@@ -228,10 +228,10 @@ public Color getColor() {
228228
* @param color the new color
229229
*/
230230
public CSG setColor(Color color) {
231-
r=color.getRed();
232-
g=color.getGreen();
233-
b=color.getBlue();
234-
o=color.getOpacity();
231+
r = color.getRed();
232+
g = color.getGreen();
233+
b = color.getBlue();
234+
o = color.getOpacity();
235235
for (Polygon p : polygons)
236236
p.setColor(color);
237237
return this;
@@ -800,15 +800,16 @@ public CSG optimization(OptType type) {
800800
* @return union of this csg and the specified csg
801801
*/
802802
public CSG union(CSG csg) {
803-
if(CSGClient.isRunning()) {
804-
ArrayList<CSG> go=new ArrayList<CSG>(Arrays.asList(this));
805-
try {
806-
return CSGClient.getClient().union(go).get(0);
807-
} catch (Exception e) {
808-
// TODO Auto-generated catch block
809-
e.printStackTrace();
803+
if (this.polygons.size() > 200 || csg.polygons.size() > 200)
804+
if (CSGClient.isRunning()) {
805+
ArrayList<CSG> go = new ArrayList<CSG>(Arrays.asList(this));
806+
try {
807+
return CSGClient.getClient().union(go).get(0);
808+
} catch (Exception e) {
809+
// TODO Auto-generated catch block
810+
e.printStackTrace();
811+
}
810812
}
811-
}
812813
// triangulate();
813814
// csg.triangulate();
814815
switch (getOptType()) {
@@ -956,7 +957,13 @@ public static CSG unionAll(CSG... csgs) {
956957
}
957958

958959
public static CSG unionAll(List<CSG> csgs) {
959-
if(CSGClient.isRunning()) {
960+
boolean offload=false;
961+
for(int i=0;i<csgs.size();i++)
962+
if(csgs.get(i).polygons.size()>200) {
963+
offload=true;
964+
break;
965+
}
966+
if (CSGClient.isRunning()) {
960967
List<CSG> back;
961968
try {
962969
back = CSGClient.getClient().union(csgs);
@@ -968,7 +975,7 @@ public static CSG unionAll(List<CSG> csgs) {
968975
}
969976
CSG first = csgs.get(0);
970977
return first.union(csgs.stream().skip(1).collect(Collectors.toList()));
971-
978+
972979
}
973980

974981
public static CSG hullAll(CSG... csgs) {
@@ -1233,15 +1240,16 @@ public CSG difference(CSG... csgs) {
12331240
* @return difference of this csg and the specified csg
12341241
*/
12351242
public CSG difference(CSG csg) {
1236-
if(CSGClient.isRunning()) {
1237-
ArrayList<CSG> go=new ArrayList<CSG>(Arrays.asList(this,csg));
1238-
try {
1239-
return CSGClient.getClient().difference(go).get(0);
1240-
} catch (Exception e) {
1241-
// TODO Auto-generated catch block
1242-
e.printStackTrace();
1243+
if (this.polygons.size() > 200 || csg.polygons.size() > 200)
1244+
if (CSGClient.isRunning()) {
1245+
ArrayList<CSG> go = new ArrayList<CSG>(Arrays.asList(this, csg));
1246+
try {
1247+
return CSGClient.getClient().difference(go).get(0);
1248+
} catch (Exception e) {
1249+
// TODO Auto-generated catch block
1250+
e.printStackTrace();
1251+
}
12431252
}
1244-
}
12451253
// triangulate();
12461254
// csg.triangulate();
12471255
try {
@@ -1392,15 +1400,16 @@ private CSG _differenceNoOpt(CSG csg) {
13921400
* @return intersection of this csg and the specified csg
13931401
*/
13941402
public CSG intersect(CSG csg) {
1395-
if(CSGClient.isRunning()) {
1396-
ArrayList<CSG> go=new ArrayList<CSG>(Arrays.asList(this,csg));
1397-
try {
1398-
return CSGClient.getClient().intersect(go).get(0);
1399-
} catch (Exception e) {
1400-
// TODO Auto-generated catch block
1401-
e.printStackTrace();
1403+
if (this.polygons.size() > 200 || csg.polygons.size() > 200)
1404+
if (CSGClient.isRunning()) {
1405+
ArrayList<CSG> go = new ArrayList<CSG>(Arrays.asList(this, csg));
1406+
try {
1407+
return CSGClient.getClient().intersect(go).get(0);
1408+
} catch (Exception e) {
1409+
// TODO Auto-generated catch block
1410+
e.printStackTrace();
1411+
}
14021412
}
1403-
}
14041413
// triangulate();
14051414
// csg.triangulate();
14061415
Node a = new Node(this.clone().getPolygons());
@@ -1544,27 +1553,28 @@ public CSG triangulate(boolean fix) {
15441553
triangulated = false;
15451554
if (triangulated)
15461555
return this;
1547-
if(CSGClient.isRunning()) {
1548-
ArrayList<CSG> go=new ArrayList<CSG>(Arrays.asList(this));
1549-
try {
1550-
return CSGClient.getClient().triangulate(go).get(0);
1551-
} catch (Exception e) {
1552-
// TODO Auto-generated catch block
1553-
e.printStackTrace();
1556+
if (this.polygons.size() > 200)
1557+
if (CSGClient.isRunning()) {
1558+
ArrayList<CSG> go = new ArrayList<CSG>(Arrays.asList(this));
1559+
try {
1560+
return CSGClient.getClient().triangulate(go).get(0);
1561+
} catch (Exception e) {
1562+
// TODO Auto-generated catch block
1563+
e.printStackTrace();
1564+
}
15541565
}
1555-
}
15561566
if (providerOf3d == null && Debug3dProvider.provider != null)
15571567
providerOf3d = Debug3dProvider.provider;
15581568
IDebug3dProvider start = Debug3dProvider.provider;
15591569
Debug3dProvider.setProvider(null);
1560-
//performTriangulation();
1570+
// performTriangulation();
15611571
if (preventNonManifoldTriangles) {
1562-
//for (int i = 0; i < 1; i++)
1563-
if (isUseGPU()) {
1564-
runGPUMakeManifold();
1565-
} else {
1566-
runCPUMakeManifold();
1567-
}
1572+
// for (int i = 0; i < 1; i++)
1573+
if (isUseGPU()) {
1574+
runGPUMakeManifold();
1575+
} else {
1576+
runCPUMakeManifold();
1577+
}
15681578
}
15691579
performTriangulation();
15701580
// now all polygons are definantly triangles
@@ -1576,22 +1586,23 @@ public CSG triangulate(boolean fix) {
15761586
private void performTriangulation() {
15771587
ArrayList<Polygon> toAdd = new ArrayList<Polygon>();
15781588
int failedPolys = 0;
1579-
for(int i=0;i<polygons.size();i++) {
1589+
for (int i = 0; i < polygons.size(); i++) {
15801590
Polygon p = polygons.get(i);
15811591
CSG ret = updatePolygons(toAdd, p);
1582-
if(ret ==null)
1592+
if (ret == null)
15831593
failedPolys++;
15841594
}
1585-
if(failedPolys>0)
1586-
System.out.println("Pruned "+failedPolys+" polygons from CSG "+getName());
1595+
if (failedPolys > 0)
1596+
System.out.println("Pruned " + failedPolys + " polygons from CSG " + getName());
15871597
if (toAdd.size() > 0) {
15881598
setPolygons(toAdd);
15891599
}
15901600
}
15911601

15921602
private void runCPUMakeManifold() {
15931603
long start = System.currentTimeMillis();
1594-
//System.err.println("Cleaning up the mesh by adding coincident points to the polygons they touch");
1604+
// System.err.println("Cleaning up the mesh by adding coincident points to the
1605+
// polygons they touch");
15951606

15961607
int totalAdded = 0;
15971608
double tOL = 1.0e-11;
@@ -1653,7 +1664,7 @@ private void runCPUMakeManifold() {
16531664
}
16541665
totalAdded += 32;
16551666
threads.clear();
1656-
if (threadIndex/32 % 50 == 0 || j == polygons.size() - 1) {
1667+
if (threadIndex / 32 % 50 == 0 || j == polygons.size() - 1) {
16571668
progressMoniter.progressUpdate(j, polygons.size(),
16581669
"STL Processing Polygons for Manifold Vertex, #" + totalAdded + " added so far", this);
16591670
}
@@ -1669,7 +1680,8 @@ private void runCPUMakeManifold() {
16691680
// Auto-generated catch block
16701681
e.printStackTrace();
16711682
}
1672-
//progressMoniter.progressUpdate(polygons.size(),polygons.size(),"Manifold fix took " + (System.currentTimeMillis() - start),this);
1683+
// progressMoniter.progressUpdate(polygons.size(),polygons.size(),"Manifold fix
1684+
// took " + (System.currentTimeMillis() - start),this);
16731685
}
16741686

16751687
private void runGPUMakeManifold() {
@@ -1742,19 +1754,18 @@ private CSG updatePolygons(ArrayList<Polygon> toAdd, Polygon p) {
17421754
if (p == null)
17431755
return this;
17441756

1745-
17461757
if (p.getVertices().size() == 3) {
17471758
toAdd.add(p);
17481759
} else {
17491760

17501761
try {
1751-
if(!p.areAllPointsCollinear()) {
1762+
if (!p.areAllPointsCollinear()) {
17521763
List<Polygon> triangles = PolygonUtil.concaveToConvex(p);
17531764
for (Polygon poly : triangles) {
17541765
toAdd.add(poly);
17551766
}
1756-
}else {
1757-
System.err.println("Polygon is colinear, removing "+p);
1767+
} else {
1768+
System.err.println("Polygon is colinear, removing " + p);
17581769
return null;
17591770
}
17601771
} catch (Throwable ex) {
@@ -2404,7 +2415,7 @@ public CSG historySync(CSG dyingCSG) {
24042415
}
24052416
if (getName().length() == 0)
24062417
setName(dyingCSG.getName());
2407-
setColor( dyingCSG.getColor());
2418+
setColor(dyingCSG.getColor());
24082419
return this;
24092420
}
24102421

@@ -2533,8 +2544,9 @@ public CSG setRegenerate(IRegenerate function) {
25332544
regenerate = function;
25342545
return this;
25352546
}
2547+
25362548
public IRegenerate getRegenerate() {
2537-
return regenerate ;
2549+
return regenerate;
25382550
}
25392551

25402552
public CSG regenerate() {
@@ -3113,7 +3125,7 @@ public boolean isHole() {
31133125

31143126
public CSG syncProperties(CSG dying) {
31153127
getStorage().syncProperties(dying.getStorage());
3116-
regenerate=dying.regenerate;
3128+
regenerate = dying.regenerate;
31173129
return this;
31183130
}
31193131

src/main/java/eu/mihosoft/vrl/v3d/CSGClient.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package eu.mihosoft.vrl.v3d;
22

3-
import java.io.IOException;
4-
import java.io.ObjectInputStream;
5-
import java.io.ObjectOutputStream;
63
import java.net.Socket;
74
import java.net.UnknownHostException;
5+
import java.nio.file.Files;
86
import java.util.ArrayList;
97
import java.util.Arrays;
108
import java.util.List;
@@ -22,9 +20,13 @@ class CSGClient {
2220
private String hostname;
2321
private int port;
2422

25-
public CSGClient(String hostname, int port) {
23+
private String key=null;
24+
25+
public CSGClient(String hostname, int port, File f) throws IOException {
2626
this.hostname = hostname;
2727
this.port = port;
28+
if(f!=null)
29+
key = Files.readAllLines(f.toPath()).toArray(new String[0])[0];
2830
try {
2931
Socket socket = new Socket(hostname, port);
3032
socket.close();
@@ -113,12 +115,16 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
113115

114116
// Create and send request
115117
CSGRequest request = new CSGRequest(csgList, operation);
118+
if(key!=null)
119+
request.setAPIKEY(key);
116120
oos.writeObject(request);
117121
oos.flush();
118122

119123
// Receive response
120124
CSGResponse response = (CSGResponse) ois.readObject();
121125
socket.close();
126+
if(response.getState()!=ServerActionState.SUCCESS)
127+
throw new RuntimeException(response.getMessage());
122128
// Return results as ArrayList
123129
return new ArrayList<>(response.getCsgList());
124130

@@ -137,10 +143,10 @@ public String getServerInfo() {
137143
return hostname + ":" + port + " (connected: " + ")";
138144
}
139145

140-
public static boolean start(String hostname, int port) throws IOException {
146+
public static boolean start(String hostname, int port, File f) throws IOException {
141147
if (getClient() != null)
142148
return false;
143-
setClient(new CSGClient(hostname, port));
149+
setClient(new CSGClient(hostname, port,f));
144150
return true;
145151
}
146152

@@ -162,7 +168,8 @@ public static void main(String[] args) {
162168

163169
// Create client with try-with-resources for automatic cleanup
164170
try {
165-
CSGClient.start(hostname, port);
171+
File f = new File("file.txt");
172+
CSGClient.start(hostname, port,f);
166173
// Connect to server
167174
System.out.println("Client info: " + getClient().getServerInfo());
168175

@@ -173,14 +180,16 @@ public static void main(String[] args) {
173180
CSG d = a.difference(b);
174181
CSG t = d.triangulate(true);
175182

176-
} catch (IOException e) {
183+
} catch (Exception e) {
177184
System.err.println("Communication error: " + e.getMessage());
178185
e.printStackTrace();
179186
}
180187

181188
System.out.println("\nClient example completed.");
182189
}
183190

191+
192+
184193
public static CSGClient getClient() {
185194
return client;
186195
}

src/main/java/eu/mihosoft/vrl/v3d/CSGRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CSGRequest implements Serializable {
88
private static final long serialVersionUID = 1L;
99
private List<CSG> csgList;
1010
private CSGRemoteOperation operation;
11+
private String APIKEY;
1112

1213
public CSGRequest() {
1314
this.csgList = new ArrayList<>();
@@ -39,4 +40,12 @@ public void setOperation(CSGRemoteOperation operation) {
3940
public String toString() {
4041
return "CSGRequest{operation=" + operation + ", csgCount=" + csgList.size() + "}";
4142
}
43+
44+
public String getAPIKey() {
45+
return APIKEY;
46+
}
47+
48+
public void setAPIKEY(String aPIKEY) {
49+
APIKEY = aPIKEY;
50+
}
4251
}

0 commit comments

Comments
 (0)