Skip to content

Commit 9b935a2

Browse files
committed
Initial commit
0 parents  commit 9b935a2

18 files changed

Lines changed: 568 additions & 0 deletions

‎.idea/compiler.xml‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_4.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_core_2_9_4.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/libraries/Maven__com_fasterxml_jackson_core_jackson_databind_2_9_4.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/modules.xml‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ideal-socket-client.iml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.9.4" level="project" />
15+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.9.4" level="project" />
16+
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.9.4" level="project" />
17+
</component>
18+
</module>

‎pom.xml‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.molo.socket.ideal</groupId>
8+
<artifactId>client</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-databind</artifactId>
26+
<version>LATEST</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-core</artifactId>
31+
<version>LATEST</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-annotations</artifactId>
36+
<version>LATEST</version>
37+
</dependency>
38+
</dependencies>
39+
40+
</project>
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.molo.socket.ideal.client;
2+
3+
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
8+
import java.io.DataInputStream;
9+
import java.io.DataOutputStream;
10+
import java.io.IOException;
11+
import java.net.Socket;
12+
import java.util.ArrayList;
13+
import java.util.LinkedList;
14+
import java.util.List;
15+
import java.util.Queue;
16+
17+
18+
public class Client extends RequestHandler{
19+
private Socket socket;
20+
private ObjectMapper mapper=new ObjectMapper();
21+
private DataOutputStream mDout;
22+
private DataInputStream mDin;
23+
private Thread readThread;
24+
private MessageListener listener;
25+
public String HOST;
26+
public int PORT;
27+
volatile boolean run=true;
28+
private static final int MAX_REQUEST=100;
29+
private Thread writeThread;
30+
private final BlockingQueue<String> requestQueue=new BlockingQueue<>(MAX_REQUEST);
31+
public Client(){
32+
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
33+
}
34+
public void open(String host,int port) throws IOException {
35+
this.PORT=port;
36+
this.HOST=host;
37+
this.socket=new Socket(host,port);
38+
mDin=new DataInputStream(socket.getInputStream());
39+
mDout=new DataOutputStream(socket.getOutputStream());
40+
readThread=new ReadThread();
41+
writeThread=new Thread(()->{
42+
while(run){
43+
try {
44+
String s=requestQueue.take();
45+
//Log.e("WriteThread ",s);
46+
sendMessage(s);
47+
Thread.sleep(100);
48+
} catch (InterruptedException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
});
53+
writeThread.start();
54+
}
55+
public void start(){
56+
if(readThread.getState()== Thread.State.NEW)
57+
readThread.start();
58+
}
59+
public void stop() throws IOException {
60+
close();
61+
}
62+
public void close() throws IOException {
63+
run=false;
64+
if(mDout!=null) this.mDout.close();
65+
if(mDin!=null) this.mDin.close();
66+
if(socket!=null) this.socket.close();
67+
if(writeThread.isAlive()) writeThread.interrupt();
68+
if(readThread.isAlive())
69+
readThread.interrupt();
70+
}
71+
public void sendMessage(String message){
72+
try {
73+
if(mDout!=null) this.mDout.writeUTF(message);
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
public void setListener(MessageListener listener) {
80+
this.listener = listener;
81+
}
82+
83+
class ReadThread extends Thread{
84+
@Override
85+
public void run() {
86+
run=true;
87+
while (run){
88+
try {
89+
//we read data from mDin and parse it and dispatch the message to up component
90+
String line=mDin.readUTF();
91+
if(line==null) break;
92+
JsonNode result=mapper.readTree(line);
93+
if(!result.has("hash") || !result.has("status"))
94+
continue;
95+
//Log.e("CLient.reading",result.toString());
96+
Response r=new Response(result.get("hash").asLong(),result.get("status").asInt(), result.get("data"));
97+
MediaFile[] medias=null;
98+
List<MediaFile> meds=new ArrayList<>();
99+
if(result.has("files")){
100+
medias= mapper.readValue(result.get("files").toString(),MediaFile[].class);
101+
}
102+
r.medias=medias;
103+
if (!dispatchResponse(r)) {
104+
if(listener!=null && r.hash<=0)
105+
listener.onReceiveNewMessage(r);
106+
}
107+
Thread.sleep(100);
108+
} catch (InterruptedException | IOException e) {
109+
e.printStackTrace();
110+
break;
111+
}
112+
}
113+
try {
114+
close();
115+
} catch (IOException e) {
116+
e.printStackTrace();
117+
}
118+
}
119+
}
120+
121+
@Override
122+
public void send(String s) {
123+
/*new Thread(()->{
124+
sendMessage(s);
125+
}).start();*/
126+
try {
127+
requestQueue.put(s);
128+
} catch (InterruptedException e) {
129+
e.printStackTrace();
130+
}
131+
}
132+
133+
@Override
134+
public DataOutputStream getOutputStream() {
135+
return mDout;
136+
}
137+
138+
@Override
139+
public DataInputStream getInputStream() {
140+
return mDin;
141+
}
142+
143+
/*@Override
144+
public void sendMediaFile(MediaFile file) {
145+
MFtp.ftpPut(file.absolutePath,getOutputStream());
146+
}*/
147+
148+
public static class BlockingQueue<T> {
149+
private Queue<T> queue = new LinkedList<T>();
150+
private int capacity;
151+
152+
public BlockingQueue(int capacity) {
153+
this.capacity = capacity;
154+
}
155+
156+
public synchronized void put(T element) throws InterruptedException {
157+
while(queue.size() == capacity) {
158+
wait();
159+
}
160+
161+
queue.add(element);
162+
notify(); // notifyAll() for multiple producer/consumer threads
163+
}
164+
165+
public synchronized T take() throws InterruptedException {
166+
while(queue.isEmpty()) {
167+
wait();
168+
}
169+
170+
T item = queue.remove();
171+
notify(); // notifyAll() for multiple producer/consumer threads
172+
return item;
173+
}
174+
}
175+
176+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.molo.socket.ideal.client;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
5+
6+
public class MediaFile {
7+
//File file;
8+
public String type;
9+
public long size;
10+
public String name;
11+
@JsonIgnore
12+
public String absolutePath;
13+
}

0 commit comments

Comments
 (0)