-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketzS.java
More file actions
219 lines (208 loc) · 5.22 KB
/
SocketzS.java
File metadata and controls
219 lines (208 loc) · 5.22 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//package OOPEuler;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Hashtable;
import java.math.BigDecimal;
import java.math.BigInteger;
public class SocketzS{
public static void main(String args[]) throws InterruptedException{
ServerSocket server=null;
try{
server = new ServerSocket(1234);
}catch(IOException ioe){
System.out.println( "Error al crear servidor");
System.exit(-1);
}
Hashtable tabla=Hash.MTable();
Socket client=null;
NewC acceptor=new NewC(server);
acceptor.start();
String line= null;
String res;
BufferedReader in=null;
PrintWriter out=null;
loop:for(;;){
boolean correct=false;
while(!correct){
System.out.println( "Ingrese cuantos digitos desea del número de euler");
Scanner cm = new Scanner(System.in);
line= cm.next();
if(line.matches(".*?[a-zA-Z].*?")){
System.out.println( "No puede ingresar letras.");
}
else{correct=true;}
}
int key=Integer.parseInt(line);
key++;
String tmp=Integer.toString(key);
while(!tabla.containsKey(tmp)){
key++;
tmp=Integer.toString(key);
}
String pres=line;
String fact=tabla.get(tmp).toString();
if(acceptor.clientList.size()<1){
System.out.println("Esperando a que se conecte al menos un cliente.");
while(acceptor.clientList.size()<1){
}
System.out.println( "Cliente(s) conectado(s)");
}
try{
acceptor.sendT(fact,pres);
while(!acceptor.getReady()){
}
res= acceptor.getRes().toString();
if(res.length()>100){
BufferedWriter archivoW= new BufferedWriter(new FileWriter("archivoF.txt"));
archivoW.write(res.toString());
archivoW.newLine();
archivoW.close();
System.out.println("Resultado escrito en achivoF.txt, Escriba finish para terminar el programa");
}else{
System.out.println( res);
System.out.println("Escriba finish para terminar el programa");
}
}catch(IOException ioe){
System.out.println( "Read fail");
}
if(line.endsWith("inish")){
acceptor.endT();
System.exit(-1);
break loop;
}
}
}
}
class NewC extends Thread{
LinkedList<ActualC> clientList;
Socket client=null;
ServerSocket server;
volatile boolean ended,ready=false;;
BigDecimal finalRes;
public NewC(ServerSocket server){
this.server=server;
clientList=new LinkedList<ActualC>();
finalRes=BigDecimal.ZERO;
ended=true;
}
public void endT(){
for(ActualC x : clientList){
x.endT();
}
ended=false;
}
public void sendT(String finale,String precision){
int abs=Integer.parseInt(finale);
int clients=clientList.size();
boolean mod0=(abs%clients==0)?true:false;
int part=abs/clients;
int ph=part;
int add=0;
if(!mod0){
add=abs-(part*clients);
part+=add;
}
int inicio=0;
int index=0;
part=ph;
ActualC tmpC=clientList.get(index);
tmpC.setParams(inicio,part+add,Integer.parseInt(precision));
tmpC.start();
inicio+=ph+add+1;
part+=ph;
index++;
for(;index<clients;index++){
tmpC=clientList.get(index);
tmpC.setParams(inicio,part,Integer.parseInt(precision));
tmpC.start();
inicio+=ph;
part+=ph;
}
LinkedList<ActualC> finished=new LinkedList<ActualC>();
while(finished.size()!=clients){
index=0;
for(ActualC x : clientList){
if(x.finish){
finalRes=finalRes.add(new BigDecimal(x.getRes()));
finished.add(clientList.remove(index));
System.out.println("finished: "+finished.size());
}else{
index++;
}
}
}
for(ActualC x: finished){
clientList.add(x);
}
ready=true;
}
public boolean getReady(){
return ready;
}
public BigDecimal getRes(){
return finalRes;
}
public void run(){
try{
while(ended==true){
client = server.accept();
clientList.add(new ActualC(client));
//System.out.println(clientList.size());
}
}catch(IOException ioe){
System.out.println( "Error em accept");
System.exit(-1);
}
}
}
class ActualC extends Thread{
Socket client;
BufferedReader in=null;
PrintWriter out=null;
volatile boolean halt=true,finish=false;
int inicio,finale,precision;
private String res;
public ActualC(Socket client){
this.client=client;
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
}catch(IOException ioe){
System.out.println( "Couldnt acces client IO");
System.exit(-1);
}
}
public void run(){
try{
while(halt){
}
out.println(Integer.toString(inicio)+","+Integer.toString(finale)+","+Integer.toString(precision)+",sucesion");
res=in.readLine();
finish=true;
halt=true;
}catch(IOException ioe){
System.out.println( "Error em accept");
System.exit(-1);
}
}
public void setParams(int inicio,int finale, int precision){
this.inicio=inicio;
this.finale=finale;
this.precision=precision;
halt=false;
}
public String getRes(){
return res;
}
public void endT(){
out.println("finish");
}
}