Vérification de l'existence du pseudo

This commit is contained in:
Alexandre Gonzalvez 2020-11-26 11:28:52 +01:00
parent 8c6b3a96ff
commit 9f4164d29c
230 changed files with 19645 additions and 0 deletions

View file

329
Application/.metadata/.log Normal file
View file

@ -0,0 +1,329 @@
!SESSION 2020-11-23 15:30:50.238 -----------------------------------------------
eclipse.buildId=4.7.3.M20180330-0640
java.version=1.8.0_201
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=fr_FR
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2020-11-23 15:31:12.822
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Alexandre'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.epp.logging.aeri.ide 2 17 2020-11-23 15:31:14.799
!MESSAGE Server org.eclipse.epp.logging.aeri.ide.server failed with exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $. ; version: 2.0.7.v20170906-1327
!STACK 0
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at org.eclipse.epp.internal.logging.aeri.ide.server.json.Json.deserialize(Json.java:91)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.IO.refreshConfiguration(IO.java:66)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.ServerConnection.startUp(ServerConnection.java:124)
at com.google.common.util.concurrent.AbstractIdleService$DelegateService$1.run(AbstractIdleService.java:62)
at com.google.common.util.concurrent.Callables$4.run(Callables.java:122)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
... 9 more
!ENTRY org.eclipse.jface.text 2 0 2020-11-23 15:34:54.016
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-23 15:34:54.022
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.jface.text 2 0 2020-11-23 15:34:54.151
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-23 15:34:54.160
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.m2e.logback.appender 2 0 2020-11-23 15:36:20.577
!MESSAGE Failed to download org.eclipse.recommenders:index::zip:0.0.0
!STACK 0
org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:453)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:255)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:232)
at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:303)
at org.eclipse.recommenders.models.ModelRepository.resolveInternal(ModelRepository.java:193)
at org.eclipse.recommenders.models.ModelRepository.resolve(ModelRepository.java:172)
at org.eclipse.recommenders.internal.models.rcp.EclipseModelRepository.resolve(EclipseModelRepository.java:168)
at org.eclipse.recommenders.internal.models.rcp.DownloadModelArchiveJob.run(DownloadModelArchiveJob.java:76)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:48)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:364)
at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:76)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:590)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:258)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:529)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:430)
... 8 more
!ENTRY org.eclipse.recommenders.models.rcp 1 0 2020-11-23 15:36:20.588
!MESSAGE org.eclipse.recommenders:index::zip:0.0.0 could not be resolved from the model repositories. Are you offline?
!SESSION 2020-11-26 08:07:45.698 -----------------------------------------------
eclipse.buildId=4.7.3.M20180330-0640
java.version=1.8.0_201
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=fr_FR
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.egit.ui 2 0 2020-11-26 08:08:01.925
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Alexandre'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.epp.logging.aeri.ide 2 17 2020-11-26 08:08:03.910
!MESSAGE Server org.eclipse.epp.logging.aeri.ide.server failed with exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $. ; version: 2.0.7.v20170906-1327
!STACK 0
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at org.eclipse.epp.internal.logging.aeri.ide.server.json.Json.deserialize(Json.java:91)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.IO.refreshConfiguration(IO.java:66)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.ServerConnection.startUp(ServerConnection.java:124)
at com.google.common.util.concurrent.AbstractIdleService$DelegateService$1.run(AbstractIdleService.java:62)
at com.google.common.util.concurrent.Callables$4.run(Callables.java:122)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
... 9 more
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 08:15:21.045
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 08:15:21.051
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 08:21:08.999
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 08:21:09.006
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.m2e.logback.appender 2 0 2020-11-26 08:21:09.779
!MESSAGE Failed to download org.eclipse.recommenders:index::zip:0.0.0
!STACK 0
org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:453)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:255)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:232)
at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:303)
at org.eclipse.recommenders.models.ModelRepository.resolveInternal(ModelRepository.java:193)
at org.eclipse.recommenders.models.ModelRepository.resolve(ModelRepository.java:172)
at org.eclipse.recommenders.internal.models.rcp.EclipseModelRepository.resolve(EclipseModelRepository.java:168)
at org.eclipse.recommenders.internal.models.rcp.DownloadModelArchiveJob.run(DownloadModelArchiveJob.java:76)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:48)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:364)
at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:76)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:590)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:258)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:529)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:430)
... 8 more
!ENTRY org.eclipse.recommenders.models.rcp 1 0 2020-11-26 08:21:09.787
!MESSAGE org.eclipse.recommenders:index::zip:0.0.0 could not be resolved from the model repositories. Are you offline?
!SESSION 2020-11-26 09:56:55.412 -----------------------------------------------
eclipse.buildId=4.7.3.M20180330-0640
java.version=1.8.0_201
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=fr_FR
Framework arguments: -product org.eclipse.epp.package.jee.product
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product
!ENTRY org.eclipse.core.resources 2 10035 2020-11-26 09:56:59.183
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
!ENTRY org.eclipse.egit.ui 2 0 2020-11-26 09:57:08.337
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'C:\Users\Alexandre'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
!ENTRY org.eclipse.epp.logging.aeri.ide 2 17 2020-11-26 09:57:10.263
!MESSAGE Server org.eclipse.epp.logging.aeri.ide.server failed with exception: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $. ; version: 2.0.7.v20170906-1327
!STACK 0
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at org.eclipse.epp.internal.logging.aeri.ide.server.json.Json.deserialize(Json.java:91)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.IO.refreshConfiguration(IO.java:66)
at org.eclipse.epp.internal.logging.aeri.ide.server.mars.ServerConnection.startUp(ServerConnection.java:124)
at com.google.common.util.concurrent.AbstractIdleService$DelegateService$1.run(AbstractIdleService.java:62)
at com.google.common.util.concurrent.Callables$4.run(Callables.java:122)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
... 9 more
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 10:04:24.217
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 10:04:24.225
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.m2e.logback.appender 2 0 2020-11-26 10:04:24.997
!MESSAGE Failed to download org.eclipse.recommenders:index::zip:0.0.0
!STACK 0
org.eclipse.aether.resolution.ArtifactResolutionException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:453)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:255)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:232)
at org.eclipse.aether.internal.impl.DefaultRepositorySystem.resolveArtifact(DefaultRepositorySystem.java:303)
at org.eclipse.recommenders.models.ModelRepository.resolveInternal(ModelRepository.java:193)
at org.eclipse.recommenders.models.ModelRepository.resolve(ModelRepository.java:172)
at org.eclipse.recommenders.internal.models.rcp.EclipseModelRepository.resolve(EclipseModelRepository.java:168)
at org.eclipse.recommenders.internal.models.rcp.DownloadModelArchiveJob.run(DownloadModelArchiveJob.java:76)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)
Caused by: org.eclipse.aether.transfer.ArtifactNotFoundException: Could not find artifact org.eclipse.recommenders:index:zip:0.0.0-SNAPSHOT in models (http://download.eclipse.org/recommenders/models/oxygen/)
at org.eclipse.aether.connector.basic.ArtifactTransportListener.transferFailed(ArtifactTransportListener.java:48)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$TaskRunner.run(BasicRepositoryConnector.java:364)
at org.eclipse.aether.util.concurrency.RunnableErrorForwarder$1.run(RunnableErrorForwarder.java:76)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector$DirectExecutor.execute(BasicRepositoryConnector.java:590)
at org.eclipse.aether.connector.basic.BasicRepositoryConnector.get(BasicRepositoryConnector.java:258)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.performDownloads(DefaultArtifactResolver.java:529)
at org.eclipse.aether.internal.impl.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:430)
... 8 more
!ENTRY org.eclipse.recommenders.models.rcp 1 0 2020-11-26 10:04:25.004
!MESSAGE org.eclipse.recommenders:index::zip:0.0.0 could not be resolved from the model repositories. Are you offline?
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 10:07:22.777
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.number'
!ENTRY org.eclipse.jface.text 2 0 2020-11-26 10:07:22.785
!MESSAGE Duplicate template id: 'org.eclipse.wst.xslt.templates.xpath.round'
!ENTRY org.eclipse.ui.ide 4 4 2020-11-26 11:26:25.254
!MESSAGE Problems saving workspace
!ENTRY org.eclipse.ui.ide 2 1 2020-11-26 11:26:25.280
!MESSAGE Problems occurred while trying to save the state of the workbench.
!SUBENTRY 1 org.eclipse.core.resources 2 234 2020-11-26 11:26:25.280
!MESSAGE The project description file (.project) for 'RemoteSystemsTempFiles' was missing. This file contains important information about the project. A new project description file has been created, but some information about the project may have been lost.
!ENTRY org.eclipse.recommenders.coordinates.rcp 4 9 2020-11-26 11:26:25.296
!MESSAGE Failed to write manual mappings to file C:\Users\Alexandre\Downloads\src-20201006T193227Z-001\.recommenders\caches\manual-mappings.json.
!STACK 0
java.io.FileNotFoundException: C:\Users\Alexandre\Downloads\src-20201006T193227Z-001\.recommenders\caches\manual-mappings.json (Le chemin daccès spécifié est introuvable)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.google.common.io.Files$FileByteSink.openStream(Files.java:245)
at com.google.common.io.Files$FileByteSink.openStream(Files.java:233)
at com.google.common.io.ByteSink$AsCharSink.openStream(ByteSink.java:158)
at com.google.common.io.CharSink.write(CharSink.java:105)
at com.google.common.io.Files.write(Files.java:489)
at org.eclipse.recommenders.internal.coordinates.rcp.ManualProjectCoordinateAdvisor.close(ManualProjectCoordinateAdvisor.java:102)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.recommenders.internal.rcp.RcpModule$Listener$1.executeMethod(RcpModule.java:292)
at org.eclipse.recommenders.internal.rcp.RcpModule$Listener$1.access$0(RcpModule.java:289)
at org.eclipse.recommenders.internal.rcp.RcpModule$Listener$1$1.postShutdown(RcpModule.java:308)
at org.eclipse.ui.internal.Workbench$4.run(Workbench.java:984)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.JFaceUtil.lambda$0(JFaceUtil.java:44)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:173)
at org.eclipse.ui.internal.Workbench.firePostShutdown(Workbench.java:981)
at org.eclipse.ui.internal.Workbench.shutdown(Workbench.java:3028)
at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1174)
at org.eclipse.ui.internal.Workbench.lambda$4(Workbench.java:1424)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.Workbench.close(Workbench.java:1424)
at org.eclipse.ui.internal.Workbench.close(Workbench.java:1397)
at org.eclipse.ui.internal.WorkbenchWindow.busyClose(WorkbenchWindow.java:1606)
at org.eclipse.ui.internal.WorkbenchWindow.access$18(WorkbenchWindow.java:1552)
at org.eclipse.ui.internal.WorkbenchWindow$11.run(WorkbenchWindow.java:1638)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchWindow.close(WorkbenchWindow.java:1635)
at org.eclipse.ui.internal.WorkbenchWindow.close(WorkbenchWindow.java:1649)
at org.eclipse.ui.internal.WorkbenchWindow$6.close(WorkbenchWindow.java:530)
at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer$3.shellClosed(WBWRenderer.java:615)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:99)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1103)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1088)
at org.eclipse.swt.widgets.Decorations.closeWidget(Decorations.java:316)
at org.eclipse.swt.widgets.Decorations.WM_CLOSE(Decorations.java:1729)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4845)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:359)
at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1657)
at org.eclipse.swt.widgets.Shell.windowProc(Shell.java:2199)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
at org.eclipse.swt.internal.win32.OS.DefWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.DefWindowProc(OS.java:2555)
at org.eclipse.swt.widgets.Shell.callWindowProc(Shell.java:506)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4941)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:359)
at org.eclipse.swt.widgets.Decorations.windowProc(Decorations.java:1657)
at org.eclipse.swt.widgets.Shell.windowProc(Shell.java:2199)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5178)
at org.eclipse.swt.internal.win32.OS.PeekMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.PeekMessage(OS.java:3152)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3812)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1150)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:680)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
!ENTRY org.eclipse.recommenders.coordinates.rcp 4 8 2020-11-26 11:26:25.303
!MESSAGE Failed to write cached coordinates to file C:\Users\Alexandre\Downloads\src-20201006T193227Z-001\.recommenders\caches\identified-project-coordinates.json.
!STACK 0
java.io.FileNotFoundException: C:\Users\Alexandre\Downloads\src-20201006T193227Z-001\.recommenders\caches\identified-project-coordinates.json (Le chemin daccès spécifié est introuvable)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at com.google.common.io.Files$FileByteSink.openStream(Files.java:245)
at com.google.common.io.Files$FileByteSink.openStream(Files.java:233)
at com.google.common.io.ByteSink$AsCharSink.openStream(ByteSink.java:158)
at com.google.common.io.CharSink.write(CharSink.java:105)
at com.google.common.io.Files.write(Files.java:489)
at org.eclipse.recommenders.coordinates.rcp.EclipseProjectCoordinateAdvisorService.shutDown(EclipseProjectCoordinateAdvisorService.java:220)
at com.google.common.util.concurrent.AbstractIdleService$2$2.run(AbstractIdleService.java:69)
at com.google.common.util.concurrent.Callables$3.run(Callables.java:93)
at java.lang.Thread.run(Unknown Source)
!ENTRY org.eclipse.core.resources 2 10035 2020-11-26 11:26:26.962
!MESSAGE The workspace will exit with unsaved changes in this session.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,167 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,162 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.setSoTimeout(1000);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,147 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5001,"Mike");
User usr1 = new User(5001);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,156 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,156 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12459);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,168 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
buffer = 0;
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,138 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this
);
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,177 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse[0]);
System.out.println(lstresponse[1]);
System.out.println(lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,167 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,132 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String isPseudo = new String(buffer);
System.out.println(isPseudo);
valid = isPseudo!=tmpPseudo;
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (lstresponse[2].compareTo("Mike")!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= ("Mike".compareTo("Mike")!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,168 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,10 @@
public class UserListeningThread extends Thread{
public UserListeningThread(String s){
super(s);
}
public void run(){
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,178 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse non normalisé : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse[0]);
System.out.println(lstresponse[1]);
System.out.println(lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket= null;
try {
dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo()+":testS";
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,106 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(not(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) {
int nbResponseMin = 2;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(1001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket1= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1001);
DatagramPacket outPacket2= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1002);
dgramSocket.send(outPacket1);
dgramSocket.send(outPacket2);
Socket link = new Socket(addIP,2);
return
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,147 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
}
}

View file

@ -0,0 +1,187 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid=!(sameString(lstresponse[2],tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (lstresponse[2].compareTo("Mike")!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,53 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
try {
DatagramSocket dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,149 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
activeUsr1.threadInterrupt();
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
while(true) {
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 14);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
while(true) {
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=12459;//inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, itsPort);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,60 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (Exception e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,119 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(not(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 2;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(1001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket1= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1001);
DatagramPacket outPacket2= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1002);
dgramSocket.send(outPacket1);
dgramSocket.send(outPacket2);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
System.out.println(inPacket);
//valid = inPacket.toString().equals(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
Socket link = new Socket(addIP,2);
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,119 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(not(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 2;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(1001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket1= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1001);
DatagramPacket outPacket2= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1002);
dgramSocket.send(outPacket1);
dgramSocket.send(outPacket2);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
System.out.println(inPacket);
//valid = inPacket!=tmpPseudo;
nbResponseMin--;
}
dgramSocket.close();
Socket link = new Socket(addIP,2);
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,162 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.setSoTimeout()
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,129 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String isPseudo = new String(buffer);
System.out.println(isPseudo);
valid = isPseudo!=tmpPseudo;
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,148 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,167 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
try{
response = new String(buffer);
}catch (IOException e) {
e.printStackTrace();
}
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,136 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String listPseudo = new String(buffer);
System.out.println(listPseudo);
String[] lstPseudo = listPseudo.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstPseudo));
valid=stringList.contains(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,57 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket = null;
while(this.myUser.haveToStopThread) {
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12459);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,108 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(not(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) {
int nbResponseMin = 2;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(1001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket1= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1001);
DatagramPacket outPacket2= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1002);
dgramSocket.send(outPacket1);
dgramSocket.send(outPacket2);
Socket link = new Socket(addIP,2);
return
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,137 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this.nport,this.remoteUserList);
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket= null;
try {
dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,161 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket= null;
try {
DatagramSocket dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
} catch (IOException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,163 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
while(true) {
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, itsPort);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (tmpPseudo.compareTo("Mike")!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,137 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,59 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (SocketTimeoutException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,193 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
arecu=true;
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
arecu=false;
}
buffer = inPacket.getData();
response = new String(buffer);
if(arecu) {
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
//System.out.println(tmpPseudo==lstresponse[2]);
valid= (tmpPseudo.compareTo(lstresponse[2])!=0);
if(!valid) {
System.out.println("Déjà pris");
}
}
newDate = new Date();
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}
/*
public Boolean compare(String str1, String str2) {
}
*/

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,5002);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(5001);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,181 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
Boolean arecu=true;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,150 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
activeUsr1.haveToStopThread=true;
}
}

View file

@ -0,0 +1,182 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,168 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
e.printStackTrace();
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,37 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
while(true) {
try {
}catch IOException{
}
InetAddress addIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String listPseudo = new String(buffer);
System.out.println(listPseudo);
}
}
}

View file

@ -0,0 +1,77 @@
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
System.out.println(tmpPseudo);
this.pseudo = pseudo;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,150 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
activeUsr1.haveToStopThread=true;
}
}

View file

@ -0,0 +1,136 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String listPseudo = new String(buffer);
System.out.println(listPseudo);
String[] lstPseudo = listPseudo.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstPseudo));
valid=stringList.contains(tmpPseudo);
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2050,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,161 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,14 @@
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User User) throws IOException{
super(s);
}
public void run(){
}
}

View file

@ -0,0 +1,4 @@
public class UserListeningThread {
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (oldDate.getTime()-newDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,73 @@
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo(String pseudo) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
this.pseudo = pseudo;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,100 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(not(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(1001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= newDatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 1001);
Socket link = new Socket(addIP,2);
return
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,174 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse non normalisé : "+response);
}
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
System.out.println(stringList);
valid=!(stringList.contains(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,178 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse[0]);
System.out.println(lstresponse[1]);
System.out.println(lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,147 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,181 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Reponse : "+response);
lstresponse = response.split(":");
System.out.println("RemoteUser");
System.out.println("IP : "+lstresponse[0]);
System.out.println("nPort : "+lstresponse[1]);
System.out.println("pseudo : "+lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,50 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket=null;
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,161 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage :"+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,171 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse non normalisé : "+response);
}
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
System.out.println(stringList);
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,178 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="\0") {
System.out.println("Reponse : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse[0]);
System.out.println(lstresponse[1]);
System.out.println(lstresponse[2]);
System.out.println(tmpPseudo);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2001,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(5001);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
system.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(2050,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,174 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="") {
System.out.println("Reponse non normalisé : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,129 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
public User(int nport){
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String isPseudo = new String(buffer);
System.out.println(isPseudo);
valid = isPseudo!=tmpPseudo;
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,181 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse= new String [3];
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
try{
dgramSocket.receive(inPacket);
}catch (Exception e) {
}
buffer = inPacket.getData();
response = new String(buffer);
if(response!="\0") {
System.out.println("Reponse : "+response);
}
lstresponse = response.split(":");
System.out.println(lstresponse[0]);
System.out.println(lstresponse[1]);
System.out.println(lstresponse[2]);
System.out.println(tmpPseudo);
System.out.println(tmpPseudo==lstresponse[2]);
valid=!(lstresponse[2].equals(tmpPseudo));
if(!valid) {
System.out.println("Déjà pris");
}
newDate = new Date();
buffer = new byte[256];
}
System.out.println(valid);
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,131 @@
import java.io.IOException;
import java.net.*;
import java.util.Scanner; // Import the Scanner class
public class User {
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] userList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
Boolean valid = true;
int nbResponseMin = 1;
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(2001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
while(nbResponseMin > 0 && valid) {
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String isPseudo = new String(buffer);
System.out.println(isPseudo);
valid = isPseudo!=tmpPseudo;
nbResponseMin--;
}
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) {
User activeUsr1 = new User(2001,"Mike");
User activeUsr2 = new User(2002,"Sue");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 5002);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5002,"Mike");
User usr1 = new User(5001);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,146 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public User(int nport) throws IOException{
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(5001,addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 2001);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Fin");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(5001,"Mike");
User usr1 = new User(3);
usr1.setPseudo();
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket = null;
while(!this.myUser.haveToStopThread) {
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,57 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
DatagramSocket dgramSocket = null;
while(!this.myUser.haveToStopThread) {
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
dgramSocket.close();
}
}

View file

@ -0,0 +1,161 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response;
String[] lstresponse;
ArrayList<String> stringList;
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
response = new String(buffer);
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,167 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12228);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
DatagramPacket inPacket;
String response = null;
String[] lstresponse;
ArrayList<String> stringList;
dgramSocket.setSoTimeout(1000);
System.out.println("Wait for pseudo validation ...");
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
try{
response = new String(buffer);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("Passage : "+response);
lstresponse = response.split(":");
stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=!(stringList.contains(tmpPseudo));
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12228,"Mike");
User usr1 = new User(12229);
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Do you want to stop :");
String response = myObj.nextLine(); // Read user input
if(response=="Yes") {
activeUsr1.haveToStopThread=true;
}
}
}

View file

@ -0,0 +1,54 @@
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
public class UserListeningThread extends Thread{
private User myUser;
public UserListeningThread(String s,User user) {
super(s);
this.myUser = user;
}
public void run(){
while(true) {
DatagramSocket dgramSocket = null;
try {
dgramSocket = new DatagramSocket(myUser.getNport(),myUser.getAddIP());
} catch (SocketException e) {
e.printStackTrace();
}
byte[] buffer = new byte[256];
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
try {
dgramSocket.receive(inPacket);
} catch (IOException e) {
e.printStackTrace();
}
buffer = inPacket.getData();
InetAddress itsIP=inPacket.getAddress();
int itsPort=5001;//inPacket.getPort();
String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, itsPort);
try {
dgramSocket.send(outPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,150 @@
import java.awt.List;
import java.io.IOException;
import java.net.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner; // Import the Scanner class
public class User{
protected InetAddress addIP;
protected String pseudo;
protected int nport;
protected boolean actif;
protected User[] remoteUserList;
public Boolean haveToStopThread=false;
public User(int nport) throws IOException{
//localUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.nport = nport;
this.setPseudo();
this.actif = true;
}
public User(int nport,String pseudo) {
//remoteUser
try
{
this.addIP = InetAddress.getLocalHost();
}
catch(UnknownHostException e) {
System.out.println("Could not find local address!");
}
this.pseudo = pseudo;
this.nport = nport;
this.actif = true;
UserListeningThread th = new UserListeningThread("UDP Listening thread",this);
th.start();
}
public InetAddress getAddIP() {
return addIP;
}
public void setAddIP(InetAddress addIP) {
this.addIP = addIP;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo() throws IOException {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter pseudo :");
String tmpPseudo = myObj.nextLine(); // Read user input
while(!(this.validatePseudo(tmpPseudo))) {
System.out.println("Already taken, choose another one :");
tmpPseudo = myObj.nextLine(); // Read user input
}
this.pseudo = tmpPseudo;
}
public Boolean validatePseudo(String tmpPseudo) throws IOException {
// Call broadcast
InetAddress broadcastIP = InetAddress.getLocalHost(); // change to broadcast
DatagramSocket dgramSocket = new DatagramSocket(this.nport,this.addIP);
byte[] buffer = new byte[256];
DatagramPacket outPacket= new DatagramPacket(tmpPseudo.getBytes(), tmpPseudo.length(),broadcastIP, 12458);
dgramSocket.send(outPacket);
Boolean valid = true;
Date oldDate = new Date();
Date newDate = new Date();
while( (newDate.getTime()-oldDate.getTime()) < 5000 && valid) {
System.out.println("Wait for pseudo validation ...");
DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
dgramSocket.receive(inPacket);
buffer = inPacket.getData();
String response = new String(buffer);
System.out.println(response);
String[] lstresponse = response.split(":");
ArrayList<String> stringList = new ArrayList<String>(Arrays.asList(lstresponse));
valid=stringList.contains(tmpPseudo);
newDate = new Date();
}
System.out.println("Pseudo valide");
dgramSocket.close();
return valid;
}
public int getNport() {
return nport;
}
public void setNport(int nport) {
this.nport = nport;
}
public boolean isActif() {
return actif;
}
public void setActif(boolean actif) {
this.actif = actif;
}
public static void main(String[] args) throws IOException {
User activeUsr1 = new User(12458,"Mike");
User usr1 = new User(12459);
activeUsr1.haveToStopThread=true;
}
}

Some files were not shown because too many files have changed in this diff Show more