OSDN Git Service

PTPIP系の接続シーケンスで、IPアドレスの特定タイミングを変更する。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / ptpip / wrapper / command / PtpIpAsyncResponseReceiver.java
1 package net.osdn.gokigen.pkremote.camera.vendor.ptpip.wrapper.command;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import java.io.InputStream;
8 import java.net.Socket;
9
10 public class PtpIpAsyncResponseReceiver implements IPtpIpCommunication
11 {
12     private final String TAG = toString();
13     private static final int ASYNC_RESPONSE_PORT = 15741;  // ??
14     private static final int BUFFER_SIZE = 1280 + 8;
15     private static final int WAIT_MS = 250;   // 250ms
16     private static final int ERROR_LIMIT = 30;
17     private IPtpIpCommandCallback receiver = null;
18     private boolean isStart = false;
19
20     public PtpIpAsyncResponseReceiver()
21     {
22         //
23     }
24
25     public void setEventSubscriber(@NonNull IPtpIpCommandCallback receiver)
26     {
27         this.receiver = receiver;
28     }
29
30     @Override
31     public boolean connect(@NonNull String ipAddress, int portNumber)
32     {
33         start(ipAddress, portNumber);
34         return (true);
35     }
36
37     @Override
38     public void disconnect()
39     {
40         isStart = false;
41     }
42
43     public void start(@NonNull String ipAddress, int portNumber)
44     {
45         if (isStart)
46         {
47             // すでに受信スレッド動作中なので抜ける
48             return;
49         }
50         isStart = true;
51         Thread thread = new Thread(() -> {
52             try
53             {
54                 Socket socket = new Socket(ipAddress, portNumber);
55                 startReceive(socket);
56             }
57             catch (Exception e)
58             {
59                 Log.v(TAG, " IP : " + ipAddress + " port : " + portNumber);
60                 e.printStackTrace();
61             }
62         });
63         try
64         {
65             thread.start();
66         }
67         catch (Exception e)
68         {
69             e.printStackTrace();
70         }
71     }
72
73     public void stop()
74     {
75         isStart = false;
76     }
77
78     private void startReceive(Socket socket)
79     {
80         int errorCount = 0;
81         InputStream isr;
82         byte[] byte_array;
83         try
84         {
85             isr = socket.getInputStream();
86             byte_array = new byte[BUFFER_SIZE];
87
88         }
89         catch (Exception e)
90         {
91             e.printStackTrace();
92             Log.v(TAG, "===== startReceive() aborted.");
93             return;
94         }
95         Log.v(TAG, "startReceive() start.");
96         while (isStart)
97         {
98             try
99             {
100                 int read_bytes = isr.read(byte_array, 0, BUFFER_SIZE);
101                 Log.v(TAG, "RECEIVE ASYNC  : " + read_bytes + " bytes.");
102                 if (receiver != null)
103                 {
104                     try
105                     {
106                         receiver.receivedMessage(0, byte_array);
107                     }
108                     catch (Exception ee)
109                     {
110                         ee.printStackTrace();
111                     }
112                 }
113                 Thread.sleep(WAIT_MS);
114                 errorCount = 0;
115             }
116             catch (Exception e)
117             {
118                 e.printStackTrace();
119                 errorCount++;
120             }
121             if (errorCount > ERROR_LIMIT)
122             {
123                 // エラーが連続でたくさん出たらループをストップさせる
124                 isStart = false;
125             }
126         }
127         try
128         {
129             isr.close();
130             socket.close();
131         }
132         catch (Exception e)
133         {
134             e.printStackTrace();
135         }
136         Log.v(TAG, "startReceive() end.");
137     }
138 }