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

Fix/prebuild #26

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions framework/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,27 +162,36 @@ func NewService(config *BridgeConfig) (*Service, error) {
}

// setup mDNS
if config.MdnsLockerDriver != nil {
mdnslogger := logger.Named("mdns")

s.mdnsLocker.Lock()
dh := mdns.DiscoveryHandler(ctx, mdnslogger, s.ipfsCoreAPI)
mdnsService := mdns.NewMdnsService(mdnslogger, s.ipfsCoreAPI, mdns.MDNSServiceName, dh)

go func() {
mdnsNetworkManagerConfig := mdns.NetworkManagerConfig{
Logger: mdnslogger,
NetManager: s.netmanager,
Service: mdnsService,
}
mdns.NetworkManagerHandler(ctx, mdnsNetworkManagerConfig)
}()
{
if config.MdnsLockerDriver != nil {
s.mdnsLocker.Lock()

close = closeFunc(close, func() error {
mdnsService.Close()
s.mdnsLocker.Unlock()
return nil
})
close = closeFunc(close, func() error {
s.mdnsLocker.Unlock()
return nil
})
}

if config.ConnectivityDriver != nil {
mdnslogger := logger.Named("mdns")

dh := mdns.DiscoveryHandler(ctx, mdnslogger, s.ipfsCoreAPI)
mdnsService := mdns.NewMdnsService(mdnslogger, s.ipfsCoreAPI, mdns.MDNSServiceName, dh)

go func() {
mdnsNetworkManagerConfig := mdns.NetworkManagerConfig{
Logger: mdnslogger,
NetManager: s.netmanager,
Service: mdnsService,
}
mdns.NetworkManagerHandler(ctx, mdnsNetworkManagerConfig)
}()

close = closeFunc(close, func() error {
mdnsService.Close()
return nil
})
}
}

s.service, err = weshnet.NewService(weshnet.Opts{
Expand Down
6 changes: 3 additions & 3 deletions gen-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const services = ["weshnet.protocol.v1.ProtocolService"];

// Prepare Handlebars templates
const serviceClientTemplate = Handlebars.compile(`
import api from './api/index.d'
import { protocol } from "./api/index.d";
import { Unary, ResponseStream, RequestStream } from './types'

export type ServiceClientType<S> = {{#each services}} {{this}} {{/each}}never;
Expand All @@ -28,7 +28,7 @@ export type ServiceClientType<S> = {{#each services}} {{this}} {{/each}}never;
const ClientTemplate = Handlebars.compile(`
export interface {{name}}Client {
{{#each methods}}
{{this.name}}: {{this.type}}<api.{{this.svcName}}.{{this.request}}, api.{{this.svcName}}.{{this.reply}}>,
{{this.name}}: {{this.type}}<{{this.svcName}}.{{this.request}}, {{this.svcName}}.{{this.reply}}>,
{{/each}}
}
`);
Expand All @@ -37,7 +37,7 @@ export interface {{name}}Client {
let serviceData = {
services: services.map((svcType) => {
const svc = pb.lookup(svcType);
return `S extends typeof api.${svc.parent.parent.name}.${svc.name} ? ${svc.name}Client :`;
return `S extends typeof ${svc.parent.parent.name}.${svc.name} ? ${svc.name}Client :`;
}),
};

Expand Down
132 changes: 132 additions & 0 deletions ios/src/Sources/ConnectivityDriver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// ConnectivityDriver.swift
// Berty
//
// Created by u on 01/02/2023.
//

import SystemConfiguration
import CoreBluetooth
import CoreTelephony
import Foundation
import WeshnetCore
import Network

class ConnectivityDriver: NSObject, WeshnetCoreIConnectivityDriverProtocol, CBCentralManagerDelegate {
let queue = DispatchQueue.global(qos: .background)
let pathMonitor = NWPathMonitor()
var centralManager: CBCentralManager!

var state: WeshnetCoreConnectivityInfo
var handlers: [WeshnetCoreIConnectivityHandlerProtocol] = []

override init() {
self.state = WeshnetCoreConnectivityInfo()!

super.init()

self.centralManager = CBCentralManager(delegate: self, queue: nil)

self.pathMonitor.pathUpdateHandler = { [weak self] path in
self!.updateNetworkState(path)

for handler in self!.handlers {
handler.handleConnectivityUpdate(self!.state)
}
}
self.pathMonitor.start(queue: self.queue)
}

func updateNetworkState(_ info: NWPath) {
self.state.setState(info.status == .satisfied ? WeshnetCoreConnectivityStateOn : WeshnetCoreConnectivityStateOff)
self.state.setMetering(WeshnetCoreConnectivityStateUnknown)
self.state.setNetType(WeshnetCoreConnectivityNetUnknown)
self.state.setCellularType(WeshnetCoreConnectivityCellularUnknown)

if info.status != .satisfied {
return
}

if #available(iOS 13.0, *) {
self.state.setMetering(info.isConstrained ? WeshnetCoreConnectivityStateOn : WeshnetCoreConnectivityStateOff)
}

if let interface = self.pathMonitor.currentPath.availableInterfaces.first {
switch interface.type {
case .wifi:
self.state.setNetType(WeshnetCoreConnectivityNetWifi)
case .cellular:
self.state.setNetType(WeshnetCoreConnectivityNetCellular)
self.state.setCellularType(ConnectivityDriver.getCellularType())
case .wiredEthernet:
self.state.setNetType(WeshnetCoreConnectivityNetEthernet)
default:
self.state.setNetType(WeshnetCoreConnectivityNetUnknown)
}
}
}

static func getCellularType() -> Int {
let networkInfo = CTTelephonyNetworkInfo()

guard let carrierType = networkInfo.serviceCurrentRadioAccessTechnology?.first?.value else {
return WeshnetCoreConnectivityCellularNone
}

switch carrierType {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
return WeshnetCoreConnectivityCellular2G
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
return WeshnetCoreConnectivityCellular3G
case CTRadioAccessTechnologyLTE:
return WeshnetCoreConnectivityCellular4G
default:
if #available(iOS 14.1, *) {
if carrierType == CTRadioAccessTechnologyNRNSA
|| carrierType == CTRadioAccessTechnologyNR {
return WeshnetCoreConnectivityCellular5G
}
}

return WeshnetCoreConnectivityCellularUnknown
}
}

func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
self.state.setBluetooth(WeshnetCoreConnectivityStateOn)
case .poweredOff,
.unsupported,
.unauthorized,
.resetting:
self.state.setBluetooth(WeshnetCoreConnectivityStateOff)
case .unknown:
self.state.setBluetooth(WeshnetCoreConnectivityStateUnknown)
@unknown default:
self.state.setBluetooth(WeshnetCoreConnectivityStateUnknown)
}

for handler in self.handlers {
handler.handleConnectivityUpdate(self.state)
}
}

public func getCurrentState() -> WeshnetCoreConnectivityInfo? {
return self.state
}

public func register(_ handler: WeshnetCoreIConnectivityHandlerProtocol?) {
if (handler != nil) {
self.handlers.append(handler!)
}
}
}
File renamed without changes.
5 changes: 5 additions & 0 deletions ios/src/WeshnetExpoModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import WeshnetCore
public class WeshnetExpoModule: Module {
var service: WeshnetCoreService?
var appRootDir: String?
var connectivityDriver: ConnectivityDriver?


public func definition() -> ModuleDefinition {
Name("WeshnetExpo")

OnCreate {
do {
self.appRootDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).path
self.connectivityDriver = ConnectivityDriver()
} catch let error as NSError {
NSLog("Error creating app root directory: \(error.localizedDescription)")
}
Expand Down Expand Up @@ -50,6 +53,8 @@ public class WeshnetExpoModule: Module {
}
config.rootDir = self.appRootDir!

config.connectivityDriver = self.connectivityDriver

guard let service = WeshnetCoreNewService(config, &err) else {
throw WeshnetError(.coreError(err!))
}
Expand Down
46 changes: 33 additions & 13 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { default as protocolpb } from './protocoltypes.pb'
import { default as rpcmanagerpb } from './rpcmanager.pb'

export const protocol: typeof protocolpb.weshnet.protocol.v1 = (
protocolpb as any
).lookup('weshnet.protocol.v1')
export const rpcmanager: typeof rpcmanagerpb.rpcmanager = (
rpcmanagerpb as any
).lookup('rpcmanager')

// const protocol = pbp
// const rpcmanager = rpcmanager
export default { protocol, rpcmanager }
// import { weshnet as protocolpb } from "./protocoltypes.pb";
// import { rpcmanager as rpcmanagerpb } from "./rpcmanager.pb";
//
// export const protocol: typeof protocolpb.protocol.v1 = (
// protocolpb as any
// ).lookup("weshnet.protocol.v1");
// export const rpcmanager: typeof rpcmanagerpb = (rpcmanagerpb as any).lookup(
// "rpcmanager",
// );
//
// export default { protocol, rpcmanager };
// export { weshnet } from "./protocoltypes.pb";
// export { rpcmanager } from "./rpcmanager.pb";
//

import * as pbjs from "protobufjs";
// import type { rpcmanager as rpcmanagerpb } from "./index.d";
// import type { protocol as protocolpb } from "./index.d";

var protocol;
var rpcmanager;

pbjs.load("./protocoltypes.pb.js", (err, root) => {
if (!root) {
console.error(err);
return;
}

protocol = root.lookup("weshnet.protocol.v1");
rpcmanager = root.lookup("rpcmanager");
});

export { protocol, rpcmanager };
58 changes: 29 additions & 29 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import api from './api'
import types from './api/index.d'
import type { rpcmanager as rpcmanagerpb } from "./api/index.d";
import { rpcmanager } from "./api";

class GRPCError extends Error {
public EOF: boolean
public OK: boolean
public EOF: boolean;
public OK: boolean;

// public Code: beerrcode.ErrCode | beweshnet_errcode.ErrCode
public GrpcCode: types.rpcmanager.GRPCErrCode
public GrpcCode: rpcmanagerpb.GRPCErrCode;

public error: types.rpcmanager.Error
public error: rpcmanagerpb.Error;

constructor(e: types.rpcmanager.IError | null | undefined) {
constructor(e: rpcmanagerpb.IError | null | undefined) {
if (!e) {
// this should not happen, but should not break the app either.
// instead simply create a empty error and warn about this
console.warn(
`GRPCError: (${e}) grpc error provided, empty error returned`,
)
e = api.rpcmanager.Error.create({})
);
e = rpcmanager.Error.create({});
}

const error = api.rpcmanager.Error.create(e)
super(error.message)
const error = rpcmanager.Error.create(e);
super(error.message);

this.error = error
this.error = error;
// this.Code = error.errorCode
this.GrpcCode = error.grpcErrorCode
this.GrpcCode = error.grpcErrorCode;

this.OK = error.grpcErrorCode === api.rpcmanager.GRPCErrCode.OK
this.OK = error.grpcErrorCode === rpcmanager.GRPCErrCode.OK;
// error.errorCode === beerrcode.ErrCode.Undefined
this.EOF =
error.grpcErrorCode === api.rpcmanager.GRPCErrCode.CANCELED ||
(error.grpcErrorCode === api.rpcmanager.GRPCErrCode.UNKNOWN &&
error.message === 'EOF')
error.grpcErrorCode === rpcmanager.GRPCErrCode.CANCELED ||
(error.grpcErrorCode === rpcmanager.GRPCErrCode.UNKNOWN &&
error.message === "EOF");
}

// public details(): beerrcode.ErrDetails {
Expand All @@ -47,8 +47,8 @@ class GRPCError extends Error {
// return this.Code
// }

public grpcErrorCode(): types.rpcmanager.GRPCErrCode {
return this.GrpcCode
public grpcErrorCode(): rpcmanagerpb.GRPCErrCode {
return this.GrpcCode;
}

public toJSON(): any {
Expand All @@ -58,12 +58,12 @@ class GRPCError extends Error {

return {
message: this.message,
grpcErrorCode: api.rpcmanager.GRPCErrCode[this.GrpcCode],
grpcErrorCode: rpcmanager.GRPCErrCode[this.GrpcCode],
// errorCode: beerrcode.ErrCode[this.Code],
// details: details,
EOF: this.EOF,
OK: this.OK,
}
};
}

// public hasErrCode(error: beerrcode.ErrCode): boolean {
Expand All @@ -72,16 +72,16 @@ class GRPCError extends Error {
}

const newGRPCError = (code: number, message: string): GRPCError => {
const error = api.rpcmanager.Error.fromObject({
const error = rpcmanager.Error.fromObject({
message: message,
grpcErrorCode: code,
})
return new GRPCError(error)
}
});
return new GRPCError(error);
};

const EOF = new GRPCError({
grpcErrorCode: api.rpcmanager.GRPCErrCode.CANCELED,
message: 'EOF',
})
grpcErrorCode: rpcmanager.GRPCErrCode.CANCELED,
message: "EOF",
});

export { GRPCError, EOF, newGRPCError }
export { GRPCError, EOF, newGRPCError };
Loading