-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridClient.java
More file actions
145 lines (136 loc) · 4.11 KB
/
GridClient.java
File metadata and controls
145 lines (136 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.net.*;
import java.util.*;
import java.io.*;
/**
* Write a description of class HTTPClient here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GridClient
{
public static String name;
public static void main(String[] args)
{
System.setProperty ("line.separator","\r\n");
System.out.println("Input your name: ");
Scanner scanName = new Scanner(System.in);
name = scanName.nextLine();
System.out.println("Connecting...");
try
{
Socket s;
if(args.length <= 1)
{
s = new Socket("localhost", 23452);
}
else
{
s = new Socket(args[0], 23452);
}
if(s.isConnected())
{
PrintWriter pWname=new PrintWriter(s.getOutputStream());
pWname.println(name);
pWname.flush();
System.out.println("Connected:");
Thread t1 = new Thread(() -> {
try {
while(s.isConnected()){
Scanner send = new Scanner(System.in);
PrintWriter pW=new PrintWriter(s.getOutputStream());
pW.println(name + ":" + send.nextLine());
pW.flush();
}
} catch(Exception e){}
});
t1.start();
while(s.isConnected()){
Scanner read=new Scanner(s.getInputStream());
while(read.hasNext()){
socketPosList = fromString(read.next());
}
}
s.close();
}
}
catch(UnknownHostException e)
{
System.out.println("Unknown server name");
}
catch(IOException e)
{
System.out.println("No connection possible");
}
}
public static ArrayList<SocketPosition> fromString (String string)
{
ArrayList<SocketPosition> returnVal = new ArrayList<SocketPosition>();
String[] elements = string.split(";");
for (int i = 0; i < elements.length-1; i++)
{
// System.out.println(elements[i]);
String[] parts = elements[i].split(":");
returnVal.add(
new SocketPosition(
name,
new Vector2(Double.parseDouble(parts[1].split(",")[0]),
Double.parseDouble(parts[1].split(",")[1])
),
new Vector2(Double.parseDouble(parts[2].split(",")[0]),
Double.parseDouble(parts[2].split(",")[1])
)
)
);
}
return returnVal;
}
public static ArrayList<SocketPosition> socketPosList = new ArrayList<SocketPosition>();
public static class Vector2
{
public final double x;
public final double y;
public Vector2()
{
x = y = 0;
}
public Vector2(double x, double y)
{
this.x = x;
this.y = y;
}
@Override
public String toString()
{
return (x + "," + y);
}
}
public static class SocketPosition
{
public final Socket socket;
public final String socketName;
public Vector2 position;
public Vector2 direction;
public SocketPosition()
{
socket = new Socket();
socketName = "";
position = new Vector2();
direction = new Vector2();
}
public SocketPosition(String newSocketName, Vector2 vector2, Vector2 dir)
{
socket = new Socket();
socketName = newSocketName;
position = vector2;
direction = dir;
}
public SocketPosition(Socket newSocket, String newSocketName, Vector2 vector2, Vector2 dir)
{
socket = newSocket;
socketName = newSocketName;
position = vector2;
direction = dir;
}
}
}