Skip to content

Commit 83613a6

Browse files
committed
msg net tcp client req resp and file dir module added. some bug fixed
1 parent cdd90f1 commit 83613a6

File tree

25 files changed

+2498
-24
lines changed

25 files changed

+2498
-24
lines changed

core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<output.directory>../_output</output.directory>
2121
</properties>
2222

23+
<build>
24+
<finalName>iottree-core</finalName>
25+
<plugins>
26+
</plugins>
27+
</build>
28+
2329
<dependencies>
2430
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
2531
<dependency>

core/src/main/java/org/iottree/core/msgnet/MNMsg.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public class MNMsg implements IMNCxtPk
2828

2929
private String topic = null ;
3030

31-
private Object payload = null ;
31+
private Object payload = null ;
32+
33+
/**
34+
* byte[] is special, it will not be packed into JSON
35+
* it is be used only for one connection call,
36+
*/
37+
private byte[] bytesArray = null ;
3238

3339
public MNMsg()
3440
{
@@ -115,6 +121,7 @@ public Map<String,Object> getHeadsMap()
115121
return heads ;
116122
}
117123

124+
118125
public MNMsg asPayload(Object payload)
119126
{
120127
if(payload==null)
@@ -236,6 +243,21 @@ public String getPayloadStr()
236243
return this.payload.toString() ;
237244
}
238245

246+
247+
248+
public MNMsg asBytesArray(byte[] bs)
249+
{
250+
this.bytesArray = bs ;
251+
return this ;
252+
}
253+
254+
public byte[] getBytesArray()
255+
{
256+
return this.bytesArray ;
257+
}
258+
259+
260+
239261
public JSONObject toJO()
240262
{
241263
JSONObject jo = new JSONObject() ;
@@ -248,6 +270,11 @@ public JSONObject toJO()
248270
}
249271

250272
jo.putOpt("payload", payload) ;
273+
274+
275+
if(this.bytesArray!=null)
276+
jo.put("bytes_len",this.bytesArray.length) ;
277+
251278
return jo ;
252279
}
253280

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.iottree.core.msgnet.modules;
2+
3+
import org.iottree.core.msgnet.MNConn;
4+
import org.iottree.core.msgnet.MNMsg;
5+
import org.iottree.core.msgnet.MNNodeMid;
6+
import org.iottree.core.msgnet.RTOut;
7+
import org.json.JSONObject;
8+
9+
public class FileDir_Close extends MNNodeMid
10+
{
11+
12+
@Override
13+
public int getOutNum()
14+
{
15+
return 1;
16+
}
17+
18+
@Override
19+
public String getTP()
20+
{
21+
return "file_dir_close";
22+
}
23+
24+
@Override
25+
public String getTPTitle()
26+
{
27+
return g("file_dir_close");
28+
}
29+
30+
@Override
31+
public String getColor()
32+
{
33+
return "#e7b686";
34+
}
35+
36+
@Override
37+
public String getIcon()
38+
{
39+
return "\\uf1c3";
40+
}
41+
42+
@Override
43+
public boolean isParamReady(StringBuilder failedr)
44+
{
45+
return true;
46+
}
47+
48+
@Override
49+
public JSONObject getParamJO()
50+
{
51+
return null;
52+
}
53+
54+
@Override
55+
protected void setParamJO(JSONObject jo)
56+
{
57+
}
58+
59+
60+
@Override
61+
protected RTOut RT_onMsgIn(MNConn in_conn, MNMsg msg) throws Exception
62+
{
63+
FileDir_M mnm = (FileDir_M)this.getOwnRelatedModule() ;
64+
FileDir_M.FileItem fi = mnm.getOpenedFile() ;
65+
if(fi==null)
66+
return null ;
67+
mnm.RT_closeFile();
68+
return RTOut.createOutAll(new MNMsg().asPayload(fi.file.getAbsoluteFile())) ;
69+
}
70+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package org.iottree.core.msgnet.modules;
2+
3+
import java.io.Closeable;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
12+
import org.iottree.core.msgnet.MNModule;
13+
import org.iottree.core.store.SourceJDBC;
14+
import org.iottree.core.util.Convert;
15+
import org.json.JSONObject;
16+
17+
public class FileDir_M extends MNModule
18+
{
19+
public static class FileItem implements Closeable
20+
{
21+
File file = null ;
22+
23+
FileInputStream inputs = null ;
24+
25+
FileOutputStream outputs = null ;
26+
27+
public FileItem(File f)
28+
{
29+
this.file = f ;
30+
}
31+
32+
private synchronized OutputStream getOutputS() throws FileNotFoundException
33+
{
34+
if(outputs!=null)
35+
return outputs ;
36+
File pdir = file.getParentFile() ;
37+
if(!pdir.exists())
38+
pdir.mkdirs() ;
39+
40+
return outputs = new FileOutputStream(file) ;
41+
}
42+
43+
public void write(byte[] bs) throws IOException
44+
{
45+
write(bs,0,bs.length) ;
46+
}
47+
48+
public void write(byte[] bs,int offset,int len) throws IOException
49+
{
50+
OutputStream os = getOutputS() ;
51+
os.write(bs,offset,len);
52+
}
53+
54+
@Override
55+
public void close() throws IOException
56+
{
57+
if(inputs!=null)
58+
{
59+
try
60+
{
61+
inputs.close();
62+
inputs = null ;
63+
}
64+
catch(Exception ee)
65+
{}
66+
}
67+
68+
if(outputs!=null)
69+
{
70+
try
71+
{
72+
outputs.close();
73+
outputs = null ;
74+
}
75+
catch(Exception ee)
76+
{}
77+
}
78+
}
79+
}
80+
81+
String dirPath = null ;
82+
83+
@Override
84+
public String getTP()
85+
{
86+
return "file_dir";
87+
}
88+
89+
@Override
90+
public String getTPTitle()
91+
{
92+
return g("file_dir");
93+
}
94+
95+
@Override
96+
public String getColor()
97+
{
98+
return "#e7b686";
99+
}
100+
101+
@Override
102+
public String getIcon()
103+
{
104+
return "\\uf07b";
105+
}
106+
107+
@Override
108+
public String getPmTitle()
109+
{
110+
if(Convert.isNullOrEmpty(this.dirPath))
111+
return "no dir path set";
112+
return this.dirPath ;
113+
}
114+
115+
@Override
116+
public boolean isParamReady(StringBuilder failedr)
117+
{
118+
if(Convert.isNullOrEmpty(this.dirPath))
119+
{
120+
failedr.append("no dir path set") ;
121+
return false;
122+
}
123+
return true;
124+
}
125+
126+
@Override
127+
public JSONObject getParamJO()
128+
{
129+
JSONObject jo = new JSONObject() ;
130+
jo.putOpt("dir_path", this.dirPath) ;
131+
return jo;
132+
}
133+
134+
@Override
135+
protected void setParamJO(JSONObject jo)
136+
{
137+
this.dirPath = jo.optString("dir_path") ;
138+
}
139+
140+
public File getDirBase()
141+
{
142+
if(Convert.isNullOrEmpty(this.dirPath))
143+
return null ;
144+
return new File(this.dirPath) ;
145+
}
146+
147+
private transient FileItem curOpenFile = null ;
148+
149+
FileItem RT_openFile(String sub_fp,StringBuilder failedr) throws IOException
150+
{
151+
File dirb = this.getDirBase() ;
152+
if(dirb==null)
153+
{
154+
failedr.append("no dir base in FileDir_M") ;
155+
return null;
156+
}
157+
File f = new File(dirb,sub_fp) ;
158+
if(curOpenFile!=null)
159+
curOpenFile.close();
160+
161+
return curOpenFile = new FileItem(f) ;
162+
}
163+
164+
FileItem getOpenedFile()
165+
{
166+
return this.curOpenFile ;
167+
}
168+
169+
void RT_closeFile() throws IOException
170+
{
171+
try
172+
{
173+
if(curOpenFile!=null)
174+
curOpenFile.close();
175+
}
176+
finally
177+
{
178+
curOpenFile = null ;
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)