Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多个Module分别引用AndServer #236

Closed
TxcA opened this issue Nov 28, 2019 · 8 comments
Closed

多个Module分别引用AndServer #236

TxcA opened this issue Nov 28, 2019 · 8 comments

Comments

@TxcA
Copy link

TxcA commented Nov 28, 2019

app和module都引用了AndServer,AndServer.serverBuilder启动了不同的Group,分配在了不同的端口。
目前的情况是:app的build.gradle annotationProcessor "com.yanzhenjie.andserver:processor:$and_server"后,访问module的Server就提示:The resource [/xxx] is not found.找不到目录。
app的build.gradle 不配置annotationProcessor,访问app的Server就提示:The resource [/xxx] is not found.找不到目录。

查看了一下,代码是生成了的。
我该如何修改能让两个module都能同时启动并不冲突呢?
https://i.loli.net/2019/11/28/ZRBs3waiqCKf9Uo.jpg
1.jpg

https://i.loli.net/2019/11/28/ZRBs3waiqCKf9Uo.jpg
2.jpg

@yanzhenjie
Copy link
Owner

代码贴一下。

@TxcA
Copy link
Author

TxcA commented Nov 29, 2019

app:

SystemManageServer.java

public class SystemManageServer {

    volatile private static SystemManageServer instance = null;

    public static SystemManageServer getInstance() {
        try {
            if (instance != null) {
            } else {
                synchronized (SystemManageServer.class) {
                    if (instance == null) {
                        instance = new SystemManageServer();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }


    private Server mServer;
    private Context mContext;
    private boolean isInit = false;
    private boolean isGettingIp = false;

    private SystemManageServer() {

    }

    /**
     * 初始化
     */
    public synchronized boolean init(Context context) {
        this.mContext = context.getApplicationContext();
        if (isInit) {
            return false;
        }
        isInit = true;
        return WifiApManagerHelper
                .openHotspot(mContext, "HYX-" + DeviceUtil.getAndroidId().substring(0, 8), WIFI_AP_PASSWORD);
    }


    private boolean isRunning() {
        return mServer != null && mServer.isRunning();
    }


    public synchronized void startServer() {
        if (isRunning() || isGettingIp) {
            LogHelper.manage("System manage server is running(startServer)...");
            return;
        }
        isGettingIp = true;
        ThreadUtils.getCpuPool().submit(new ThreadUtils.SimpleTask<String>() {
            @Override
            public String doInBackground() throws Throwable {
                String apAddress = null;
                int errorIndex = 0;
                do {
                    apAddress = WifiApManagerHelper.getIpAddressString();
                    LogHelper.manage("getHotspotLocalIpAddress: " + apAddress);
                    if (++errorIndex > 9) {
                        break;
                    }
                    Thread.sleep(3000);
                } while (StringUtils.isEmpty(apAddress));
                return apAddress;
            }

            @Override
            public void onSuccess(String result) {
                if (StringUtils.isEmpty(result)) {
                    LogHelper.manage("Start SystemManage Server fail, get ap local ip is null....");
                    return;
                }
                createServer(result);
                isGettingIp = false;
            }

            @Override
            public void onFail(Throwable t) {
                LogHelper.manage("Start SystemManage Server fail, throwable: " + t.getMessage());
                isGettingIp = false;
            }
        });
    }


    /**
     * Stop SERVER.
     */
    public void stopServer() {
        if (mServer != null && mServer.isRunning()) {
            mServer.shutdown();
        } else {
            LogHelper.manage("xHttpManageServer - The SERVER has not started yet.");
        }
    }


    /**
     * Create SERVER.
     */
    private void createServer(String apLocalIp) {
        try {
            if (isRunning()) {
                LogHelper.manage("System manage server is running(createServer)...");
                return;
            }

            InetAddress inetAddress = InetAddress.getByName(apLocalIp);
            mServer = AndServer.serverBuilder(mContext, ManageConstant.HTTP_GROUP_CONTROL_MANAGE)
                    .inetAddress(inetAddress)
                    .port(ManageConstant.MANAGE_PORT)
                    .timeout(10, TimeUnit.SECONDS)
                    .listener(new Server.ServerListener() {
                        @Override
                        public void onStarted() {
                            LogHelper.manage("xHttpManageServer -  onStarted()");
                        }

                        @Override
                        public void onStopped() {
                            LogHelper.manage("xHttpManageServer -  onStopped()");
                        }

                        @Override
                        public void onException(Exception e) {
                            LogHelper.manage("xHttpManageServer -  onException():" + e.getMessage());
                        }
                    })
                    .build();
            if (mServer == null) {
                LogHelper.manage("xHttpManageServer - mServer is  null.");
                return;
            }
            if (mServer.isRunning()) {
                mServer.shutdown();
                LogHelper.manage("xHttpManageServer - The SERVER is running.");
            }
            mServer.startup();
        } catch (Exception ex) {
            LogHelper.manage("xHttpManageServer - Init error: " + ex.getMessage());
        }
    }
}

SystemController.java

@PostMapping(ManageConstant.MAPPING_SYSTEM)
@RestController(ManageConstant.HTTP_GROUP_CONTROL_MANAGE)
public class SystemController {

    @PostMapping("/logo")
    public ResponseBody logo() {
        byte[] img = ConvertUtils.bitmap2Bytes(
                BitmapFactory.decodeResource(xIao.getAppContext().getResources(),
                        R.mipmap.logo_blue, null), Bitmap.CompressFormat.JPEG);
        ResponseBody body = new ResponseBody() {
            @Override
            public long contentLength() {
                return img.length;
            }

            @Nullable
            @Override
            public MediaType contentType() {
                return MediaType.IMAGE_JPEG;
            }

            @Override
            public void writeTo(@NonNull OutputStream output) throws IOException {
                output.write(img);
            }
        };

        return body;
    }
}

module:

AfServer.ava

public class AfServer {
    private Server mServer;

    /**
     * Create server.
     */
    public AfServer(Context context) {
        try {
            InetAddress inetAddress = InetAddress.getByName("0.0.0.0");

            mServer = AndServer.serverBuilder(context.getApplicationContext(), AfConstant.HTTP_GROUP_ASK_FACE)
                    .inetAddress(inetAddress)
                    .port(AfConstant.HTTP_PORT)
                    .timeout(10, TimeUnit.SECONDS)
                    .listener(new Server.ServerListener() {
                        @Override
                        public void onStarted() {
                            AfUtil.log("xHttpServer - AfServer onStarted()");
                        }

                        @Override
                        public void onStopped() {
                            AfUtil.log("xHttpServer - AfServer onStopped()");
                        }

                        @Override
                        public void onException(Exception e) {
                            AfUtil.log("xHttpServer - AfServer onException(): " + e.getMessage());
                        }
                    })
                    .build();
        } catch (Exception ex) {
            ex.printStackTrace();
            AfUtil.log("xHttpServer - Init error: " + ex.getMessage());
        }
    }

    /**
     * Start server.
     */
    public void startServer() {
        if (mServer.isRunning()) {
            mServer.shutdown();
            AfUtil.log("xHttpServer - The server is running.");
        }
        mServer.startup();
    }

    /**
     * Stop server.
     */
    public void stopServer() {
        if (mServer.isRunning()) {
            mServer.shutdown();
        } else {
            AfUtil.log("xHttpServer - The server has not started yet.");
        }
    }
}

HeartbeatCallFunction.java

@RestController(AfConstant.HTTP_GROUP_ASK_FACE)
public class HeartbeatCallFunction {

    private static OnAskFaceDeviceHeartbeat onAskFaceDeviceHeartbeat;

    public static void setOnAskFaceDeviceHeartbeat(OnAskFaceDeviceHeartbeat onAskFaceDeviceHeartbeat) {
        HeartbeatCallFunction.onAskFaceDeviceHeartbeat = onAskFaceDeviceHeartbeat;
    }

    @PostMapping(AfConstant.HTTP_CALL_HEARTBEAT)
    public String heartbeat(
            @RequestParam(name = "deviceKey", required = false) String deviceKey,
            @RequestParam(name = "time", required = false) String time,
            @RequestParam(name = "ip", required = false) String ip,
            @RequestParam(name = "personCount", required = false) String personCount,
            @RequestParam(name = "faceCount", required = false) String faceCount,
            @RequestParam(name = "version", required = false) String version) {

        AfUtil.log("xHttpServer heartbeat: " + deviceKey + " | " + time + " | " + ip + " | " + personCount + " | "
                + faceCount + " | " + version);
        if (onAskFaceDeviceHeartbeat != null) {
            onAskFaceDeviceHeartbeat.onHeartbeat(deviceKey, time, ip, personCount, faceCount, version);
        }
        return AfConstant.DEFAULT_SERVER_HANDLER;
    }

}

总算把代码块调对了...

@TxcA
Copy link
Author

TxcA commented Nov 29, 2019

还是就是RestController里有推荐的回调处理方式没,因为不需要自己new,回调就写静态了,总感觉不对。对了,还有就是严老大你的Nohttp下载不了Ftp文件,yanzhenjie/NoHttp#229

@yanzhenjie
Copy link
Owner

不好意思,最近做双12比较忙,很少上来,回得慢很抱歉。

现在好了吗?你代码中的常量看不到是什么值,比如group_name,port这种关键信息,代码太多了,不好判断问题,你最好简单搞个demo测试下。

如果启动/Controller都是严格按照分组的,应该不会出现这个问题。

@TxcA
Copy link
Author

TxcA commented Dec 13, 2019

不好意思,最近做双12比较忙,很少上来,回得慢很抱歉。

现在好了吗?你代码中的常量看不到是什么值,比如group_name,port这种关键信息,代码太多了,不好判断问题,你最好简单搞个demo测试下。

如果启动/Controller都是严格按照分组的,应该不会出现这个问题。

好的,谢谢严老大。我单独写个试试~

@yanzhenjie
Copy link
Owner

抱歉,2.1.1版本才彻底解决这个问题;现在你按照首页的指导,为项目添加插件依赖,并在每个module中应用这个插件就可以解决这个冲突问题了。

@TxcA
Copy link
Author

TxcA commented Apr 13, 2020

@yanzhenjie 严老大你回来了啊(。・ω・。)ノ♡,一直以为你弃坑了。好嘞,目前就单Module用着的,另一个走Socket了。还有不忙抽时间更新下NoHttp吧,虽然Retrofit也很香,但有些项目用NoHttp集成是真快~

@ichtj
Copy link

ichtj commented Nov 3, 2022

arouter使用的时候分模块,在module中使用了插件和引用了包,也会出现404 Not Found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants